📜 ⬆️ ⬇️

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.

Training


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 https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && echo "deb https://dl.yarnpkg.com/debian/ 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 https://deb.nodesource.com/setup_11.x | 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' services: web: build: . command: mix phx.server volumes: - .:/home/app ports: - "4000:4000" links: - db depends_on: - db test: image: <app_dir>_web command: mix test environment: MIX_ENV: test PORT: 4001 volumes: - .:/home/app db: image: postgres:latest ports: - '5432:5432' environment: 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 phx.new . --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 http://127.0.0.1:4000 .



Tests


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 test test/<app_name>_web/controllers/page_controller_test.exs 

That's all, all good!

Source: https://habr.com/ru/post/439718/