Create a Phoenix app using Docker

This tutorial is a follow up on my first post. This time we’ll create a Phoenix app running in a container. Basically the same thing as the official getting started guide does, just containerized. As usual I’m keeping it briefly, just to guet you startet, without all the fuzz.

I assume you have your environment ready with Docker running. As there isn’t an official Phoenix Docker image, our first step is to build one. Create a directory and an empty file with the name “Dockerfile”. Open it in VS Code and copy the following content into the file:

# we're starting from the official Elixir image, version 1.10
FROM elixir:1.10

# set the working dir inside our new image
WORKDIR /usr/src

# install Phoenix
RUN mix local.hex --force
RUN mix local.rebar --force
RUN mix archive.install hex phx_new 1.5.1 --force

# install node, which is a dependency from Phoenix
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt-get install -y nodejs

# install inotify-tools for live reload
RUN apt-get install -y inotify-tools

# expose default Phoenix port
EXPOSE 4000

The Dockerfile now contains all the instructions needed to create our own Phoenix Docker image. Just open the terminal in VS Code, execute the following command, and all the magic will happen automatically:

docker build --tag phoenix:1.5.1 .

Now after a short waiting time we’ve got our Phoenix image, and we can use it to create our sample app with the following command:

docker run -it -v ${pwd}:/usr/src phoenix:1.5.1 mix phx.new hello --no-ecto

-it will open an interactive shell
-v ${pwd}:/usr/src mounts our current directory to /usr/src inside the image

Note: The ${pwd} is PowerShell specific, it might differ if you use another shell.

Besides your Dockerfile now you’ve got a new folder “hello”, containing the newly created app. It’s already ready to compile and run by just one more command:

docker run -p 4000:4000 -it -v ${pwd}/hello:/usr/src/ phoenix:1.5.1 mix phx.server

-p 4000:4000 maps our systems port 4000 to the port 4000 of the image.

Our app schould now be up and running, and accessible through the browser at http://localhost:4000

That’s already quite nice, isn’t it? 😉
But there’s more we can do: We can make another image containing our app, ready to run. Create a new Dockerfile inside of the “hello” directory with the following content:

# as base image we're using our own phoenix image
FROM phoenix:1.5.1

# create a directory for our app, copy the sources to there and compile it
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app/
RUN mix compile

EXPOSE 4000

# default command to be executed on image run
CMD mix phx.server

Almost there, just some more commands in your shell:

cd hello
docker build --tag hello .
docker run -it -p 4000:4000 hello

Et voilà: Your application is running in its own Docker image.

I hope this worked for you, and you’ll join me in my next tutorial (coming eventually) where we’ll deploy our app to Microsoft Azure.

Leave a Reply

Your email address will not be published. Required fields are marked *