Skip to content

Instantly share code, notes, and snippets.

@girorme
Last active February 10, 2020 02:51
Show Gist options
  • Select an option

  • Save girorme/a88d7dabdda9399831dceded716855c7 to your computer and use it in GitHub Desktop.

Select an option

Save girorme/a88d7dabdda9399831dceded716855c7 to your computer and use it in GitHub Desktop.
Elixir docker skeleton
# Version of docker-compose
version: '3'
# Containers we are going to run
services:
# Our Phoenix container
phoenix:
# The build parameters for this container.
build:
# Here we define that it should build from the current directory
context: .
environment:
# Variables to connect to our Postgres server
PGUSER: postgres
PGPASSWORD: postgres
PGDATABASE: database_name
PGPORT: 5432
# Hostname of our Postgres container
PGHOST: db
ports:
# Mapping the port to make the Phoenix app accessible outside of the container
- "4000:4000"
depends_on:
# The db container needs to be started before we start this container
- db
db:
# We use the predefined Postgres image
image: postgres:9.6
environment:
# Set user/password for Postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
# Set a path where Postgres should store the data
PGDATA: /var/lib/postgresql/data/pgdata
restart: always
volumes:
- pgdata:/var/lib/postgresql/data
# Define the volumes
volumes:
pgdata:
# Use an official Elixir runtime as a parent image
FROM elixir:latest
RUN apt-get update && \
apt-get install -y postgresql-client
# Create app directory and copy the Elixir projects into it
RUN mkdir /app
COPY . /app
WORKDIR /app
# Install hex package manager
RUN mix local.hex --force
RUN mix deps.get
RUN mix local.rebar --force
# Compile the project
RUN mix do compile
CMD ["/app/entrypoint.sh"]
# entrypoint.sh
#!/bin/bash
# Docker entrypoint script.
# Wait until Postgres is ready
while ! pg_isready -q -h $PGHOST -p $PGPORT -U $PGUSER
do
echo "$(date) - waiting for database to start"
sleep 2
done
# Create, migrate, and seed database if it doesn't exist.
if [[ -z `psql -Atqc "\\list $PGDATABASE"` ]]; then
echo "Database $PGDATABASE does not exist. Creating..."
createdb -E UTF8 $PGDATABASE -l en_US.UTF-8 -T template0
mix ecto.migrate
mix run priv/repo/seeds.exs
echo "Database $PGDATABASE created."
fi
exec mix phx.server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment