juhalehtonen / docknix

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Docknix

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).

Quickstart

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 cookie
  • DOCKNIX_SECRET_KEY_BASE for Phoenix secret key base
  • DOCKNIX_DB_USER for PostgreSQL username
  • DOCKNIX_DB_PASS for PostgreSQL password

Additionally, for CircleCI:

  • APP_HOST to know where to deploy to with CI
  • APP_USER to define which user to deploy as with CI

All the steps

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).

Steps for Phoenix and Distillery

  1. Add Distillery to dependencies in mix.exs: {:distillery, "~> 1.5", runtime: false}
  2. Run mix do deps.get, compile to fetch and compile Distillery
  3. Run mix release.init to initialize a Distillery release
  4. Configure if we want to include erts in rel/config.exs *
  5. Create release tasks module in lib/docknix/release_tasks.ex *
  6. Add release command script at rel/commands/migrate.sh *
  7. Add release command script to list of commands in rel/config.exs (to be able to run migrations with bin/docknix migrate)

Steps for Docker

  1. Add .dockerignore to avoid including unnecessary files
  2. Create Dockerfile.build to specify how to build the release *
  3. Create build.sh to manage the building of the app *
  4. You can now run ./build.sh to build your application in Docker
  5. Create Dockerfile.run for running the built release *
  6. Add docker-compose.yml to provision and manage the containers *

More steps for Phoenix

  1. Adjust config/prod.exs to follow Distillery basics *
  2. Adjust config/prod.secret.exs for production secrets *

Steps for Nginx @ Docker

  1. Create a Docker network with docker network create nginx-network
  2. Create directories nginx and nginx/conf.d and place docker-compose.yml and Nginx configs there

Steps for running everything

  1. Assume we have already run docker network create nginx-network
  2. Run docker-compose up in application directory
  3. Run docker-compose up in nginx directory

Phoenix locally

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.

Learn more

About

License:MIT License


Languages

Language:Elixir 61.7%Language:HCL 16.5%Language:JavaScript 9.0%Language:Shell 8.3%Language:HTML 4.4%Language:CSS 0.1%