DEV Community

Noel Worden
Noel Worden

Posted on • Edited on

2

Accessing localhost within a Docker Image

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:

dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib

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:

psql: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5433?

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:
the input device is not a TTY

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.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay