How do I run Elixir Phoenix Docker in production mode using example from Phoenix Guides? How do I run Elixir Phoenix Docker in production mode using example from Phoenix Guides? docker docker

How do I run Elixir Phoenix Docker in production mode using example from Phoenix Guides?


There is a good sample repo is configured for production-ready build and deploy cycle. It contains an ansible setup that will maintain docker image, build phoenix app in docker image, and do automated versioned releases on your production server.

And I recommend you to read the blog post guide


Define database url from ENV variable

config :hello_world, HelloWorld.Repo,  url: "${DATABASE_URL}"

Then use the database url with pool_size as you pass it to container

DATABASE_URL=ecto://postgres@db/hello_world?pool_size=10

Add ENV REPLACE_OS_VARS=true to your Dockerfile (may only work with Distillery, otherwise substitute "${DATABASE_URL}" with correct form to get ENV variable at runtime, not on compiling stage).

Here is example how to create simple build with Distillery:

FROM elixir:1.9.0-alpineRUN mix local.hex --force && \    mix local.rebar --force && \    mix archive.install --force hex phx_new 1.4.8RUN apk add --update nodejs nodejs-npmENV MIX_ENV=prodWORKDIR /srv/appCOPY ./platform/ /srv/app/# install dependencies (production only)RUN mix local.rebar --forceRUN mix deps.get --only prodRUN mix compile# RUN npm install --global webpackRUN cd assets && npm install && ./node_modules/webpack/bin/webpack.js --mode productionRUN mix phx.digestRUN mix distillery.release# alpine version should be the same as buildFROM alpine:3.9RUN apk add --update bashENV REPLACE_OS_VARS=trueWORKDIR /srv/appCOPY --from=0  /srv/app/_build/prod/ .CMD rel/platform/bin/platform migrate && rel/platform/bin/platform foreground

Also, keep in mind since you have postgres running locally (not on the same network as container), you need to take it into accout and use docker.for.mac.localhost as the host name for postgres or --net=host for docker run and localhost as the host for postgres.


Looks like the problem appears because you did not set the config correctly.

exception: key :database not found

In order to use the database that you injected using environment variables you need to explicitly get it. Depending on how you organized your config, you should have a config for your Repo. By default the config for repo is:

config :yourserver_api, YourServer.Repo,  username: "postgres",  password: "postgres",  database: "yourserver_api_prod",  hostname: "localhost",  pool_size: 10

If you know for sure that for example database will be injected, then you need to change it to:

database: System.get_env("DATABASE_URL")