Accessing localhost within a Docker Image
Tags: learning, postgres, thisweekilearned, docker
Published: 2020-03-28 01:35:51

I learned a lot this week, potentially the most interesting being this:

`docker.for.mac.localhost`

The TL;DR is this: If you need to run a command through a docker container requiring access your machine's `localhost`, you can use that.

If you want the whole spiel, here it is. I needed to take a `pg_dump` file that I had created from my local environment and dump it to a database that I had access to via an ssh tunnel. 

I had some ssh configurations setup, including a `LocalForward`, so my first attempt at executing the data seed looked like this:

`psql -h localhost -p 5433 -U <username> -d <database> < dev_to_prod`

But that returned an error: 

{% gist https://gist.github.com/noelworden/b2d06ed4b05af02cb50e850de41c8e68 %}

I have never seen this error, I assume something about the `libreadline.7` I have locally is out of date. A quick internet search didn't turn up an obvious, timely fix.


- _side note:_ If you're curious about the `psql` command, a full rundown of the it and the flags can be found [here](https://www.postgresql.org/docs/9.3/app-psql.html). Basically, its a command that starts an interactive terminal, which I'm using to dump the data from the file `dev_to_prod` to another database. A quick overview of the flags I'm using:

`-h : hostname`
`-p : port`
`-U : username`
`-d : database name`


With this being a relatively new project, all the packages in it would be more up to date than my machine. Instead of digging into the problem on my local machine, I figured it would be quicker to simply execute the command through the docker container of the database I had running, so my next attempt looked like this:

`docker-compose exec -T db psql -h localhost -p 5433 -U <username> -d <database> < dev_to_prod`

But that returned the following error: 

{% gist https://gist.github.com/noelworden/2fef98ed026057804f226356ce1a05f6 %}

Basically, the docker container couldn't access my `localhost`.

- _another side note:_ The `-T` is necessary in the previous command to disable pseudo-tty allocation from docker when using `exec`, without it I was getting the error: 

{% gist https://gist.github.com/noelworden/c8a7ed1f0d666ab0539d78f1ca1d15f3 %}

I was at a fork in the road, running the command both locally and in my docker container returned errors; I had to choose one error and start 
down that rabbit hole. I didn't have a solution to either at my fingertips, both would require some internet searching and tinkering. I ran it by a colleague and we decided to go down the path of getting the docker container to recognize `localhost`. My colleague was running the the Stack Overflow searching, and giving me potential commands, and the solution was found [here](https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container). There are a lot of potential solutions in that post, but we went with the straightforward `docker.for.mac.localhost`.

My final, and successful, command looked like this:

`docker-compose exec -T db psql -h docker.for.mac.localhost -p 5433 -U <username> -d <database> < dev_to_prod`

------

This post is the first of an ongoing *This Week I Learned* [series](https://dev.to/noelworden/beginning-of-a-blog-series-5aj3). I welcome any critique, feedback, or suggestions in the comments.