From ccd3a8c5c675a23329e9bb1b91d7c3595ee22f1e Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Fri, 9 May 2025 17:33:15 -0500 Subject: [PATCH 1/2] Add docker init --- .dockerignore | 5 +++++ Dockerfile | 47 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 26 +++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..deb6cf0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +_build +deps +node_modules +assets/node_modules +.env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..76f1a91 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,47 @@ +# Dockerfile + +# Base image +FROM elixir:1.16-alpine AS build + +# Install hex + rebar +RUN mix local.hex --force && \ + mix local.rebar --force + +# Set environment +ENV MIX_ENV=prod \ + LANG=C.UTF-8 + +# Install Node and dependencies for assets +RUN apk add --no-cache build-base git npm + +# Create build dir +WORKDIR /app + +# Copy files +COPY mix.exs mix.lock ./ +COPY config config +RUN mix deps.get --only prod +RUN mix deps.compile + +COPY assets assets +COPY priv priv +COPY lib lib + +# Compile and digest assets +RUN mix compile +RUN mix assets.deploy + +# Release the app +RUN mix release + +# ─── FINAL IMAGE ──────────────────────────────────────────── + +FROM alpine:3.18 AS app + +RUN apk add --no-cache openssl ncurses-libs libstdc++ + +WORKDIR /app + +COPY --from=build /app/_build/prod/rel/crumb ./ + +CMD ["bin/crumb", "start"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..43cc18f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +services: + crumb: + build: + context: . + dockerfile: Dockerfile + environment: + DATABASE_URL: ecto://postgres:postgres@db/crumb_prod + SECRET_KEY_BASE: a_very_secret_key_that_you_generate + PHX_HOST: localhost + PORT: 4000 + ports: + - "4000:4000" + depends_on: + - db + + db: + image: postgres:15 + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: crumb_prod + volumes: + - pgdata:/var/lib/postgresql/data + +volumes: + pgdata: From 0e4e17eed16c35a767b3abd02370f7c1133db714 Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Fri, 9 May 2025 17:45:40 -0500 Subject: [PATCH 2/2] Docker running --- .env.example | 1 - .gitignore | 1 - Dockerfile | 2 -- config/runtime.exs | 5 +++-- docker-compose.yml | 4 +++- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index b2f50ae..f13e7f4 100644 --- a/.env.example +++ b/.env.example @@ -13,4 +13,3 @@ SECRET_KEY_BASE= # Destination API keys FORWARD_WEBHOOK_URL=https://webhook.site/your-test-url MIXPANEL_TOKEN= - diff --git a/.gitignore b/.gitignore index e7c80b7..f409fa4 100644 --- a/.gitignore +++ b/.gitignore @@ -13,7 +13,6 @@ crumb-*.tar ## Environment .env config/dev.secret.exs -config/runtime.exs ## JS SDKs /sdk/js/dist diff --git a/Dockerfile b/Dockerfile index 76f1a91..2a6aa39 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,13 +23,11 @@ COPY config config RUN mix deps.get --only prod RUN mix deps.compile -COPY assets assets COPY priv priv COPY lib lib # Compile and digest assets RUN mix compile -RUN mix assets.deploy # Release the app RUN mix release diff --git a/config/runtime.exs b/config/runtime.exs index d801f7f..9e9e762 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -40,9 +40,9 @@ end if config_env() == :prod do database_url = - System.get_env("DATABASE_URL") || + System.get_env("DATABASE") || raise """ - environment variable DATABASE_URL is missing. + environment variable DATABASE is missing. For example: ecto://USER:PASS@HOST/DATABASE """ @@ -81,6 +81,7 @@ if config_env() == :prod do ip: {0, 0, 0, 0, 0, 0, 0, 0}, port: port ], + server: true, secret_key_base: secret_key_base # ## SSL Support diff --git a/docker-compose.yml b/docker-compose.yml index 43cc18f..fc23a0f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,9 @@ services: context: . dockerfile: Dockerfile environment: - DATABASE_URL: ecto://postgres:postgres@db/crumb_prod + DATABASE: ecto://postgres:postgres@db:5432/crumb_prod + USERNAME: postgres + PASSWORD: postgres SECRET_KEY_BASE: a_very_secret_key_that_you_generate PHX_HOST: localhost PORT: 4000