Developing Elixir / Phoenix Applications with Docker

Under the cat is a small and simple guide that shows how to use Docker Compose to configure and launch the Elixir / Phoenix + PostgreSQL application.


Create an empty <app_dir> directory and go to it:

mkdir <app_dir> && cd <app_dir>

Create a Dockerfile and fill it with content.

<app_dir> / dockerfile
FROM elixir:latest
RUN apt-get update -qq && apt-get install -y libpq-dev && apt-get install -y build-essential inotify-tools erlang-dev erlang-parsetools apt-transport-https ca-certificates && apt-get update && curl -sS | apt-key add - && echo"deb stable main" |  tee /etc/apt/sources.list.d/yarn.list &&  apt-get update &&  apt-get install --no-install-recommends yarn
RUN mix local.hex --force && mix local.rebar --force
RUN mix archive.install hex phx_new 1.4.0 --force
RUN curl -sL | bash - && apt-get install -y nodejs
WORKDIR /home/app

Create docker-compose.yml and fill it with content.

<app_dir> /docker-compose.yml
version: '3'
    build: .
    command: mix phx.server  
      - .:/home/app
      - "4000:4000"
      - db
      - db
    image: <app_dir>_web
    command: mix test
      MIX_ENV: test
      PORT: 4001
      - .:/home/app 
    image: postgres:latest
      - '5432:5432'
      POSTGRES_USER: <db_user>
      POSTGRES_PASSWORD: <db_user_password>

Project creation

We generate a new application using docker-compose, where <app_name> is the name of the application:

docker-compose run web mix . --app <app_name>

If you are using Docker on Linux, then the created files belong to root, since the container is launched on its behalf. To avoid difficulties in the future, you need to change the owner:

sudo chown -R $USER:$USER .

Database connection

To connect to the database in the settings of our application, you need to change two files:

<app_dir> /config/dev.exs
# Configure your database
config :<app_name>, <App_name>.Repo,
  username: <db_user>,
  password: <db_user_password>,
  database: <app_name>_dev,
  hostname: "db",
  pool_size: 10

<app_dir> /config/test.exs
# Configure your database
config :<app_name>, <App_name>.Repo,
  username: <db_user>,
  password: <db_user_password>,
  database: <app_name>_test,
  hostname: "db",
  pool_size: 10

After the settings are adjusted, we create a base for each environment:

docker-compose run web mix ecto.create

docker-compose run test mix ecto.create

Application launch

Well, it remains only to run our application:

docker-compose up -d

That's all. Now our application is available at .


And in the end about testing.

In order to perform all the tests, you need to run:

docker-compose run test

To perform a specific test:

docker-compose run test mix testtest/<app_name>_web/controllers/page_controller_test.exs

That's all, all good!

Also popular now: