Skip to content

Commit

Permalink
chore(docker): set up docker-compose toolchain
Browse files Browse the repository at this point in the history
- config: move DATABASE_URL to runtime env var
  • Loading branch information
tmkontra committed Jul 6, 2020
1 parent f2fe423 commit 3d62762
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
./bullion/_build
./bullion/deps
./bullion/deps
./bullion/assets/node_modules
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
.idea
14 changes: 7 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ WORKDIR /app
# By using --force, we don’t need to type “Y” to confirm the installation
RUN mix local.hex --force

# Compile the project
ARG database_url
ENV DATABASE_URL=${database_url}

ARG secret_key
ENV SECRET_KEY_BASE=${secret_key}

ENV MIX_ENV prod
ENV PORT 4001
ARG mix_env
ENV MIX_ENV=${mix_env}

RUN mix local.rebar --force
RUN mix deps.get --only prod
ARG deps_postfix
RUN mix deps.get ${deps_postfix}

RUN mix do compile

RUN mix phx.digest

CMD mix phx.server
7 changes: 1 addition & 6 deletions bullion/config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ use Mix.Config

# Configure your database
config :bullion, Bullion.Repo,
username: "postgres",
password: "postgres",
database: "bullion",
hostname: "localhost",
show_sensitive_data_on_connection_error: true,
pool_size: 10,
port: 54320
pool_size: 10

# For development, we disable any cache and enable
# debugging and code reloading.
Expand Down
8 changes: 0 additions & 8 deletions bullion/config/prod.secret.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@
# remember to add this file to your .gitignore.
use Mix.Config

database_url =
System.get_env("DATABASE_URL") ||
raise """
environment variable DATABASE_URL is missing.
For example: ecto://USER:PASS@HOST/DATABASE
"""

config :bullion, Bullion.Repo,
# ssl: true,
url: database_url,
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")

secret_key_base =
Expand Down
6 changes: 6 additions & 0 deletions bullion/lib/bullion/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ defmodule Bullion.Repo do
use Ecto.Repo,
otp_app: :bullion,
adapter: Ecto.Adapters.Postgres

def init(_, config) do
config = config
|> Keyword.put(:url, System.get_env("DATABASE_URL"))
{:ok, config}
end
end
29 changes: 29 additions & 0 deletions docker-compose.prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: "3"

services:
web:
build:
context: .
args:
mix_env: prod
secret_key: ${SECRET_KEY}
deps_postfix: "--only prod"
image: "bullion:0.1.0"
ports:
- "4000:4000"
environment:
- PORT=4001
- DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db/${POSTGRES_DB}
db:
container_name: "bullion-db"
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
- bullion-db:/var/lib/postgresql/data
expose:
- "5432"
volumes:
bullion-db:
driver: local
23 changes: 15 additions & 8 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
version: "3"

services:
# Create a service named db.
web:
build:
context: .
args:
mix_env: dev
image: "bullion:0.1.0.dev"
ports:
- "4000:4000"
environment:
- PORT=4001
- DATABASE_URL=postgresql://postgres:postgres@db/bullion
db:
# Use the Docker Image postgres. This will pull the newest release.
image: "postgres"
# Give the container the name my_postgres. You can changes to something else.
image: "postgres:12"
container_name: "bullion-db"
# Setup the username, password, and database name. You can changes these values.
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=bullion
# Maps port 54320 (localhost) to port 5432 on the container. You can change the ports to fix your needs.
ports:
- "54320:5432"
expose:
- "5432"
3 changes: 3 additions & 0 deletions rsync-files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docker-compose.yaml
docker-compose.prod.yaml
.env

0 comments on commit 3d62762

Please sign in to comment.