From 0e60ad1a864c351e9ef1c354552821675c01aad2 Mon Sep 17 00:00:00 2001 From: silentsilas Date: Sat, 31 Aug 2024 13:17:45 -0400 Subject: [PATCH] attempt to get phoenix dockerized --- .tool-versions | 2 ++ Dockerfile | 26 ++++++++++++++++++++++ config/config.exs | 2 +- config/dev.exs | 3 ++- docker-compose.yml | 55 ++++++++++++++++++++-------------------------- entrypoint.sh | 19 ++++++++++++++++ 6 files changed, 74 insertions(+), 33 deletions(-) create mode 100644 .tool-versions create mode 100644 Dockerfile create mode 100755 entrypoint.sh diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..35e71c4 --- /dev/null +++ b/.tool-versions @@ -0,0 +1,2 @@ +erlang 27.0.1 +elixir 1.17.2 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0ee76d6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +# Use an official Elixir runtime as a parent image. +FROM elixir:latest + +RUN apt-get update && \ + apt-get install -y postgresql-client inotify-tools nodejs npm + +# 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 + +# Compile the project. +RUN mix deps.get + +WORKDIR /app/assets +RUN npm install --verbose + +WORKDIR /app +RUN mix assets.setup +RUN mix assets.build +RUN mix do compile + +CMD ["/app/entrypoint.sh"] \ No newline at end of file diff --git a/config/config.exs b/config/config.exs index 9f7ed6e..2fccd69 100644 --- a/config/config.exs +++ b/config/config.exs @@ -27,7 +27,7 @@ config :poex, Poex.Repo, username: "postgres", password: "postgres", hostname: "localhost", - port: 5560 + port: 5432 # Configures the mailer # diff --git a/config/dev.exs b/config/dev.exs index a5526e9..d2b34b7 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -4,8 +4,9 @@ import Config config :poex, Poex.Repo, username: "postgres", password: "postgres", - hostname: "localhost", + hostname: System.get_env("PGHOST") || "localhost", database: "poex_dev", + port: System.get_env("PGPORT") |> String.to_integer() || 5432, stacktrace: true, show_sensitive_data_on_connection_error: true, pool_size: 10 diff --git a/docker-compose.yml b/docker-compose.yml index 27a4862..b3e06b8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,43 +1,36 @@ -version: '3.9' +version: "3.9" services: + 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: poex_dev + PGPORT: 5432 + # Hostname of our Postgres container. + PGHOST: pg-master + 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. + - pg-master + pg-master: image: kartoza/postgis:16-3.4 - restart: 'always' - ports: - - '5560:5432' + restart: "always" environment: - ALLOW_IP_RANGE: '0.0.0.0/0' - REPLICATION_USER: 'replicator' - REPLICATION_PASS: 'replicator' - REPLICATION: 'true' - POSTGRES_USER: 'postgres' - POSTGRES_PASS: 'postgres' + POSTGRES_USER: "postgres" + POSTGRES_PASS: "postgres" POSTGRES_MULTIPLE_EXTENSIONS: postgis volumes: - pg-master:/var/lib/postgresql healthcheck: test: "exit 0" - pg-replica1: - image: kartoza/postgis:16-3.4 - restart: 'always' - ports: - - '5561:5432' - environment: - ALLOW_IP_RANGE: '0.0.0.0/0' - REPLICATION_USER: 'replicator' - REPLICATION_PASS: 'replicator' - REPLICATION: 'true' - POSTGRES_USER: 'postgres' - POSTGRES_PASS: 'postgres' - POSTGRES_MULTIPLE_EXTENSIONS: postgis - REPLICATE_FROM: pg-master - DESTROY_DATABASE_ON_RESTART: 'True' - volumes: - - pg-replica1:/var/lib/postgresql - depends_on: - pg-master: - condition: service_healthy volumes: pg-master: - pg-replica1: \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..febe881 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,19 @@ +#!/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 setup + echo "Database $PGDATABASE created." +fi + +exec mix phx.server \ No newline at end of file