Last active
April 7, 2025 01:02
-
-
Save petelacey/b8a1aacdc33c8718ba9366733e16a8c2 to your computer and use it in GitHub Desktop.
Docker Compose setup for Elixir, Phoenix, and Postgres
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/dev.exs | |
config :yourapp, YourApp.Repo, | |
username: System.get_env("PGUSER"), | |
password: System.get_env("PGPASSWORD"), | |
database: System.get_env("PGDATABASE"), | |
hostname: System.get_env("PGHOST"), | |
port: System.get_env("PGPORT"), | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3.8' | |
services: | |
web: | |
build: . | |
ports: | |
- "4000:4000" | |
volumes: | |
- .:/app | |
depends_on: | |
- db | |
env_file: | |
- some_app.env | |
db: | |
image: postgres:latest | |
environment: | |
- POSTGRES_USER=some_user | |
- POSTGRES_PASSWORD=some_password | |
volumes: | |
- ./postgres-data:/var/lib/postgresql/data | |
ports: | |
- "5432:5432" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM elixir:latest | |
# Install debian packages | |
RUN apt-get update && \ | |
apt-get install --yes build-essential inotify-tools postgresql-client git && \ | |
apt-get clean | |
ADD . /app | |
# Install Phoenix packages | |
RUN mix local.hex --force && \ | |
mix local.rebar --force && \ | |
mix archive.install --force hex phx_new 1.5.1 | |
# Install node | |
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash - && apt-get install -y nodejs | |
WORKDIR /app | |
RUN mix deps.get | |
RUN npm install --prefix ./assets | |
EXPOSE 4000 | |
CMD ["/app/entrypoint.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Docker entrypoint script. | |
# Wait until Postgres is ready | |
echo "Testing if Postgres is accepting connections. {$PGHOST} {$PGPORT} ${PGUSER}" | |
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..." | |
mix ecto.create | |
mix ecto.migrate | |
mix run priv/repo/seeds.exs | |
echo "Database $PGDATABASE created." | |
fi | |
exec mix phx.server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PGUSER=some_user | |
PGPASSWORD=some_password | |
PGDATABASE=some_database | |
PGPORT=5432 | |
PGHOST=db |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you share me what is expected folder structure? I was doing little near but always stuck at
RUN mix deps.get
due toCould not find a Mix.Project
error.