Dockerise umbrella app with distillery and Docker

Hi everyone,

I am building an Elixir Umbrella app and I want to “dockerise” it for later deployment.

For now it just consist of a single, simple Phoenix API with a postgre DB. But it will grow with multiple apps in the future.

I followed this tutorial in order to use Docker but it does not provide me with an example on how to setup a DB in my Docker image & container.

Hence, I eventualy ran into this error when trying to run my Docker image:

15:57:24.782 [error] Postgrex.Protocol (#PID<0.1353.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused

15:57:24.784 [error] Postgrex.Protocol (#PID<0.1352.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused

15:57:24.785 [error] Postgrex.Protocol (#PID<0.1358.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused

15:57:24.786 [info] Running ComitiaWeb.Endpoint with cowboy 2.6.1 at :::4000 (http)

15:57:24.786 [error] Postgrex.Protocol (#PID<0.1357.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused

etc.

How can I change my Dockerfile (and other files if required) to create a DB in my Docker containers ?

Here is my Dockerfile:

# Alias this container as builder:
FROM bitwalker/alpine-elixir-phoenix as builder

WORKDIR /political_project

ENV MIX_ENV=prod

# Umbrella
# Copy mix files so we use distillery:
COPY mix.exs mix.lock ./
COPY config config

COPY apps apps

RUN mix do deps.get, deps.compile

WORKDIR /political_project
COPY rel rel
RUN mix release --env=prod --verbose


FROM alpine:3.9

RUN apk upgrade --no-cache && \
    apk add --no-cache bash openssl
    # we need bash and openssl for Phoenix

EXPOSE 4000

ENV PORT=4000 \
    MIX_ENV=prod \
    REPLACE_OS_VARS=true \
    SHELL=/bin/bash

WORKDIR /political_project

COPY --from=builder /political_project/_build/prod/rel/political_project/releases/0.1.0/political_project.tar.gz .

RUN tar zxf political_project.tar.gz && rm political_project.tar.gz

RUN chown -R root ./releases

USER root

CMD ["/political_project/bin/political_project", "foreground"]
1 Like

You do not create a database in that container, but you connect it with another one that provides the database.

How you do this depends on how you are using docker.

If you start everything by hand, this can get complicated, if you use docker-compose it’s just adding a service and configuring your repo to use the database from that service, and if you are using other abstractions or even histers, you need to look up their documentation.

1 Like

Hi NobbZ,

Thank you very much for your reply ! didn’t know such thing like docker-compose exists, I’m quite new to Docker.

I’ll give it a try that way. :smile:

We use Docker config for aviacommerce umbrella app (multiple phoenix/elixir apps). So may be our development docker setup can help you.

2 Likes

thank you @pkrawat1 I just had a look at it, I’m probably gonna get a lot of inspiration from your config.

just curious where do you deploy your app ?

I deploy on aws. This is the blog we referenced.

2 Likes