Skip to content

Commit

Permalink
Add docker config for development env
Browse files Browse the repository at this point in the history
  • Loading branch information
francolucas authored and edance committed Oct 23, 2019
1 parent d958e74 commit d6b1a45
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 12 deletions.
13 changes: 12 additions & 1 deletion .env.example
@@ -1,8 +1,19 @@
# Run this command to import the variables
# In case you are not using Docker, run this command to import the variables
# export $(cat .env | grep -v ^# | xargs)

############### Required ENV ###############

# Postgres info
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DATABASE=squeeze_dev
POSTGRES_HOST=db

# To check your id, run "id -g" in your terminal
HOST_GROUP_GID=1000
# To check your id, run "id -u" in your terminal
HOST_USER_UID=1000

# Add your own strava information
# Create an app for strava at https://developers.strava.com
STRAVA_CLIENT_ID=
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -34,3 +34,6 @@ npm-debug.log

# Add coveralls folder
/cover

# ElixirLS Dialyzer
.elixir_ls/
14 changes: 9 additions & 5 deletions README.md
Expand Up @@ -40,11 +40,15 @@ Here are the steps to get started:
* Copy `.env.example` file to `.env` with `cp .env.example .env`
* Create an app for strava [here](https://developers.strava.com).
* Set environment variables in your `.env` file.
* Import your environment variables with `export $(cat .env | grep -v ^# | xargs)`
* 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 && yarn install`
* Start Phoenix endpoint with `mix phx.server`
* **If you are using Docker:**
* Install Docker and docker-compose in your machine
* Run `docker-compose up -d`
* **If you are not using Docker:**
* Import your environment variables with `export $(cat .env | grep -v ^# | xargs)`
* 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 && yarn install`
* Start Phoenix endpoint with `mix phx.server`

Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.

Expand Down
11 changes: 5 additions & 6 deletions config/dev.exs
Expand Up @@ -58,11 +58,10 @@ config :phoenix, :stacktrace_depth, 20
# Configure your database
config :squeeze, Squeeze.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "squeeze_dev",
hostname: "localhost",
username: System.get_env("POSTGRES_USER"),
password: System.get_env("POSTGRES_PASSWORD"),
database: System.get_env("POSTGRES_DATABASE"),
hostname: System.get_env("POSTGRES_HOST"),
pool_size: 10

config :squeeze, Squeeze.Mailer,
adapter: Bamboo.LocalAdapter
config :squeeze, Squeeze.Mailer, adapter: Bamboo.LocalAdapter
33 changes: 33 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,33 @@
version: '3'

services:
# Phoenix app
app:
build:
context: .
dockerfile: ./docker/Dockerfile
args:
HOST_USER_UID: ${HOST_USER_UID}
HOST_GROUP_GID: ${HOST_GROUP_GID}
container_name: openpace-app
volumes:
- .:/app/
env_file:
- .env
ports:
- 4000:4000
depends_on:
- db

# Postgres
db:
image: postgres:latest
container_name: openpace-db
volumes:
- pgdata:/var/lib/postgresql/data
restart: always
env_file:
- .env

volumes:
pgdata:
34 changes: 34 additions & 0 deletions docker/Dockerfile
@@ -0,0 +1,34 @@
FROM elixir:1.9

RUN curl -sL https://deb.nodesource.com/setup_12.x | bash

# Install depenencies
RUN apt-get update && \
apt-get install -y --no-install-recommends \
inotify-tools \
apt-transport-https \
apt-utils \
postgresql-client \
nodejs

# Install yarn
RUN npm install -g yarn

# Create openpace user and group from host to avoid permission issues
ARG HOST_GROUP_GID
ARG HOST_USER_UID
RUN addgroup --gid $HOST_GROUP_GID openpace && \
useradd -m -u $HOST_USER_UID -g $HOST_GROUP_GID openpace

# Set permissions to the app folder
ARG APP_DIR='/app'
RUN mkdir -p $APP_DIR && \
chown -R openpace:openpace $APP_DIR

USER openpace

RUN mix local.hex --force && mix local.rebar --force

WORKDIR $APP_DIR

ENTRYPOINT ["sh", "/app/docker/entrypoint.sh"]
28 changes: 28 additions & 0 deletions docker/entrypoint.sh
@@ -0,0 +1,28 @@
#!/bin/bash
# Docker entrypoint script.

cd $(dirname $0)/..
export APP_DIR=$(pwd)
export ASSETS_DIR=${APP_DIR}/assets

# Install new dependencies
mix deps.get

# Wait until Postgres is ready
while ! pg_isready -q -h $POSTGRES_HOST -p 5432 -U $POSTGRES_USER
do
echo "$(date) - waiting for database to start."
sleep 2
done

# Create and migrate database
mix ecto.create
mix ecto.migrate

# Install yarn packages
cd ${ASSETS_DIR}
yarn install
cd ${APP_DIR}

echo "Starting Phoenix server..."
exec mix phx.server

0 comments on commit d6b1a45

Please sign in to comment.