How To Dockerize Elixir Phoenix API Application

How to Dockerize Elixir Phoenix API Application

With this blog, we hope you will be able to gain knowledge on building a container in Docker that runs a Phoenix release, and makes containerization much more convenient.


For this tutorial, you will need an understanding of the workings of the Elixir, MySQL, Docker, Postman software, and have these installed in your working environment.

Step 1 – Create API Application

Run the following command and crate Phoenix application:

mix sample_api –database mysql –no-html

Next, as instructed in the command output, set the DB configurations and start the server.

If everything works well, you should then be able to see the following output when you enter the application URL on the browser.

docker blog post by wavenet










Step 2 – Create Simple API for User CRUD Operations

  • Run the following command create API for User CRUD operations:

mix phx.gen.json Users User users name:string age:integer

  • Then follow the instructions of the command output, and configure the route, and run the following command to create database tables:

mix ecto.migrate

  • Then start the server by running the following command:

mix phx.server

  • Now, using Postman, make an API request to the user’s endpoint, and check whether everything works well.

If everything works fine, you should be able to see the following output;

Wavenet blog post - doker

Step 3 – Modify the Application Configuration to Accept Environment Variables

Once you build a Docker image, you cannot change the application configuration manually. Instead, it should be able to pass those values via the environment variables.

Let us see how to modify the application for this below:

  • Open config/dev.exs file and modify database configurations as follows:

wavenet blog post - docker

  • Once you set this up, it will take the default value given, or the one available on the environment variable. 


Step 4 – Create Dockerfile and Build the Image

Now we are ready to dockerize your application.

  • Go to the Root directory and create a file called “Dockerfile” and enter the following details:

Waevnet blog post

  • Next, create another file called “” and enter the following details on it: 

Wavnet blog

  • Next, run following docker command to build the image: 

docker build -t sample_api . 

The above command will create a docker image with sample_api. If all successful, then you will be able to see output similar to the following: Wavnet blog

If you run the Docker images command, you will be able to see the image created.

Step 5 – Run Build Image

Now we have created the docker image. Let us try to run it. Run the following command from the command line.

docker run -p 4000:4000 –env DB_HOST=”″ sample_api

The above command will start your application on port 4000 and connect to the local MySQL database. You are passing database host as an environment variable.

Similarly, you can pass any other parameters to the application via environment variables.

You can obtain the host machine IP by running the ipconfig command.

If you are getting an error such as, cannot connect to the DB, then execute the following command from your SQL command line:

GRANT ALL ON *.* to root@’%’ IDENTIFIED BY ‘password’;

This command allows access to your MySQL DB from any host.

Call the same API endpoint from your Postman and check whether it works well. If it works fine, you will be able to see the same the Postman output similar to the above.




Written by:

Aruna Attanayake – Associate Software Architect,  Global Wavenet

14 July 2020



More from our blog

See all posts