Use Docker to create an Elixir/Phoenix development environment

Cesar William Alvarenga
Jun 13 · 2 min read
Image for post
Image for post

What is Elixir/Phoenix?

In a nutshell, Phoenix is a web framework that is written in the Elixir programming language and runs on top of the Erlang virtual machine. We use Elixir for building low-latency, fault-tolerant, distributed systems, which are increasingly necessary qualities of modern web applications.

For more information, click here.

Let’s create our development environment!

It’s the simplest way to create your Elixir/Phoenix development environment using Docker.

The first file is the Docker file, which specifies the Elixir image and its version. In this file, I’m using the latest version, but you can use another version supported by the Elixir image. This Dockerfile declares all the dependencies necessary to build a Phoenix application.

The next file is the docker-compose.yml file. This file specifies the Phoenix and the DB Postgres services. Phoenix configures applications to use the Postgres database by default. In this file, you can configure the database user and password. Using the default configuration it will set the user to “Postgres”, the password to “Postgres” and store all the data in the ./pgdata directory.

$ docker-compose build

The “src” directory will contain the Phoenix application.

$ mkdir src

Create the “mix” alias to run the Elixir commands.

$ alias mix="docker-compose run --rm phoenix mix"

The following command will create a new Phoenix application called “hello” under the “src/” directory, which is mounted inside the container under “/app” (the default work directory).

$ mix phx.new . --app hello

Change the database hostname configuration on file src/config/dev.exs to point to the db container.

...
# Configure your database
...
hostname: "db",
...

Now you will initialize the database with Ecto.

$ cd src
$ mix ecto.create

If you copied and existing application, now would be the time to run your database migrations.

$ mix ecto.migrate

Great! Now your application is ready to be run. Starting your application is easy, back to the project directory and start up your application.

$ cd ..
$ docker-compose up

Once up, it will be available under http://localhost:4000

Image for post
Image for post

The Startup

Medium's largest active publication, followed by +721K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store