A Dockerized, zero-dependency Phoenix 1.3 setup which should be used as a sample for future setups. Based on Gist by jswny.
Includes everything needed to build, deploy and run a default Phoenix application (Nginx and PostgreSQL).
Assuming you just want to build and run this application with everything already configured properly:
# Locally
./build.sh
# On the server
docker network create nginx-network
docker-compose up # in app diretory
docker-compose up # in nginx directory
For CI (and building with Docker in general), we need the following environment variables set:
DOCKNIX_RELEASE_COOKIE
for the distributed Erlang cookieDOCKNIX_SECRET_KEY_BASE
for Phoenix secret key baseDOCKNIX_DB_USER
for PostgreSQL usernameDOCKNIX_DB_PASS
for PostgreSQL password
Additionally, for CircleCI:
APP_HOST
to know where to deploy to with CIAPP_USER
to define which user to deploy as with CI
Below is a quick outline of the steps we needed to take to achieve this setup.
Lines with *
signify files that you might need to change (e.g. app names).
- Add Distillery to dependencies in
mix.exs
:{:distillery, "~> 1.5", runtime: false}
- Run
mix do deps.get, compile
to fetch and compile Distillery - Run
mix release.init
to initialize a Distillery release - Configure if we want to include erts in
rel/config.exs
* - Create release tasks module in
lib/docknix/release_tasks.ex
* - Add release command script at
rel/commands/migrate.sh
* - Add release command script to list of commands in
rel/config.exs
(to be able to run migrations withbin/docknix migrate
)
- Add
.dockerignore
to avoid including unnecessary files - Create
Dockerfile.build
to specify how to build the release * - Create
build.sh
to manage the building of the app * - You can now run
./build.sh
to build your application in Docker - Create
Dockerfile.run
for running the built release * - Add
docker-compose.yml
to provision and manage the containers *
- Adjust
config/prod.exs
to follow Distillery basics * - Adjust
config/prod.secret.exs
for production secrets *
- Create a Docker network with
docker network create nginx-network
- Create directories
nginx
andnginx/conf.d
and placedocker-compose.yml
and Nginx configs there
- Assume we have already run
docker network create nginx-network
- Run
docker-compose up
in application directory - Run
docker-compose up
in nginx directory
To start your Phoenix server:
- Install dependencies with
mix deps.get
- Create and migrate your database with
mix ecto.create && mix ecto.migrate
- Install Node.js dependencies with
cd assets && npm install
- Start Phoenix endpoint with
mix phx.server
Now you can visit localhost:4000
from your browser.
Ready to run in production? Please check our deployment guides.
- Official website: http://www.phoenixframework.org/
- Guides: http://phoenixframework.org/docs/overview
- Docs: https://hexdocs.pm/phoenix
- Mailing list: http://groups.google.com/group/phoenix-talk
- Source: https://github.com/phoenixframework/phoenix