DEV Community

loading...
Cover image for Dockerizing Phoenix Development (4) The Dockerfile

Dockerizing Phoenix Development (4) The Dockerfile

Alastair Measures
Freelance developer working with Rails and Phoenix on PostgreSQL and SQL Server.
Updated on ・3 min read

This step assembles a Dockerfile in order to develop a Phoenix web server. It lists the Dockerfile and then explains key lines. You will probably need to make some adjustments for your own use, so check the explanations.

The Dockerfile (./myp_dev/Dockerfile.alpine)

FROM elixir:1.12-alpine

LABEL maintainer="me_myself_i@work"
LABEL version_date="2021-05-20"

RUN apk add --no-cache --update \
        git \
        inotify-tools \
        libpq postgresql-client \
        nodejs npm yarn

ENV  USER="devuser"  UID="1000"  GID="1000"

RUN addgroup  -g $GID  -S $USER  && \
    adduser  -h /apps  -g ""  -G $USER  -S  -D  -u $UID "$USER"

USER $UID:$GID

ENV PATH="/usr/local/lib/elixir/bin:/usr/local/lib/erlang/bin:$PATH"
ENV PATH="/apps/.cache/rebar3/bin:$PATH"

VOLUME /apps
WORKDIR /apps
EXPOSE 4000/tcp

CMD sleep 50000
Enter fullscreen mode Exit fullscreen mode

Lines Explained

FROM elixir:1.12-alpine
Enter fullscreen mode Exit fullscreen mode
  • Note this means use of the apk package manager for Alpine Linux rather than the apt utility used with Debian and Ubuntu. There is a full searchable library of packages available for Alpine Linux.
  • By the time you read this, later editions may be available so adjust as necessary.
RUN apk add --no-cache --update \
        git \
        inotify-tools \
        libpq postgresql-client \
        nodejs npm yarn
Enter fullscreen mode Exit fullscreen mode

Packages as follows:

  • git is useful for automated download of libraries, as well as uploading code to github et cetera.

  • inotify-tools adds into the container kernel so that changes to files can be detected by a running program within the container.

  • libpq is the shared object/ DLL for access to PostgreSQL.

  • postgresql-client adds in the PostgreSQL command line tools such as psql. This is not strictly needed and adds ~9MB to the image, but it is convenient to have on hand.

  • nodejs Javascript interpreter.

  • npm & yarn package managers for JavaScript.

ENV  USER="devuser"  UID="1000"  GID="1000"
Enter fullscreen mode Exit fullscreen mode
  • Set some environment variables for use in setting up a user account for you as developer.

  • Matching the UID & GID between your host user account and the developer account in the image is key to using IDE's and other tools. You MUST use your own UID and GID here!

RUN addgroup  -g $GID  -S $USER && \
RUN adduser  -h /apps  -g ""  -G $USER  -S  -D  -u $UID "$USER"
Enter fullscreen mode Exit fullscreen mode
  • These lines add a new group and a new user within that group.

  • Alpine Linux uses the BusyBox tools to do this; which are different to the Debian/ Ubuntu ones! Documentation is available.

USER $UID:$GID
Enter fullscreen mode Exit fullscreen mode
  • The remaining few lines are completed as the devuser. So signing in as devuser will have the $PATH, and start in the WORKDIR that you would expect.
ENV PATH="/usr/local/lib/elixir/bin:/usr/local/lib/erlang/bin:$PATH"
ENV PATH="/apps/.cache/rebar3/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode
  • Adds Elixir and Erlang into the $PATH.
VOLUME /apps
WORKDIR /apps
Enter fullscreen mode Exit fullscreen mode
  • The /apps/ folder within the container will be the $HOME folder of the devuser account.

  • The same folder will also be mapped as a volume to the ./myp_dev/source/ folder of the host user account (configured via the docker-compose.yml).

EXPOSE 4000/tcp
Enter fullscreen mode Exit fullscreen mode
  • This allows the default Phoenix web server port to be mapped to a known port number of the host machine. Precisely which port number is configured in the docker-compose.yml.
CMD sleep 50000
Enter fullscreen mode Exit fullscreen mode
  • Once a container runs, it wants to run to completion and then shutdown in an orderly fashion. We need it to hang around so we can attach a console and carry out our development work!

Remarks

  • An equivalent Dockerfile based on Debian or Ubuntu can readily be put together. Note however that the image will be near to twice the size(!).
  • The inotify-tools capability will pick up changes originating inside the container, but may not notice changes originating outside the container (e.g. your IDE). It does however need to be present as Phoenix squeals if it isn't.

Discussion (0)