DEV Community

Noel Worden
Noel Worden

Posted on • Updated on

Docker: Connecting an Elixir/Phoenix App Container and Database Container

While spinning up an Elixir/Phoenix side project I ended up being blocked for longer than I'd like to admit trying to get my app container and database containers to communicate with each other. The fix ended up to not having anything to do with the Dockerfile or docker-compose.yml, but in the dev.exs config file.

The default database configuration from the phx.new generation looked like this:

Which works fine when running the app locally, without Docker. But, when starting the app and database containers in Docker, I would get the following error:

What's happening is that the project container is looking for a database named localhost, but the database is no longer running locally nor by the name localhost. It is now running as a container by the name declared in the docker-compose.yml file. In my case I named it db, so I needed to update the hostname field of the database config to this:

Now that I have solved the problem, and know what to look for, I see that it is mentioned in the Docker docs. In hindsight I may have been too rabbit-holed in finding the solution to this problem in blog posts or step-by-steps that were specific to Elixir/Phoenix and Docker, although I am a bit surprised that this step was not mentioned in any of those sources. But, it is documented now, so hopefully I can help save someone else a a bit of time debugging their dockerized Elixir/Phoenix app.


This post is part of an ongoing This Week I Learned series. I welcome any critique, feedback, or suggestions in the comments.

Top comments (0)