Use Docker to create an Elixir/Phoenix development environment
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.
Step 1: create those files below to your project directory
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
Step 2: build the image
$ docker-compose build
Step 3: create the “src” directory and the command “mix” alias.
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"
Step 4: Initialize and configure a new Phoenix application
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
# Configure your database
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
Step 5: Start the application
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