loading...

Accessing localhost within a Docker Image

noelworden profile image Noel Worden Updated on ・2 min read

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:

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. 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:

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:

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. 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. I welcome any critique, feedback, or suggestions in the comments.

Posted on by:

noelworden profile

Noel Worden

@noelworden

Software Engineer in Boulder, CO - Writing code and getting strategically lost in the mountains

Discussion

pic
Editor guide