Skip to content

Instantly share code, notes, and snippets.

@petelacey
Last active June 13, 2024 16:47
Show Gist options
  • Save petelacey/b8a1aacdc33c8718ba9366733e16a8c2 to your computer and use it in GitHub Desktop.
Save petelacey/b8a1aacdc33c8718ba9366733e16a8c2 to your computer and use it in GitHub Desktop.
Docker Compose setup for Elixir, Phoenix, and Postgres
# 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"),
...
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"
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"]
#!/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
PGUSER=some_user
PGPASSWORD=some_password
PGDATABASE=some_database
PGPORT=5432
PGHOST=db
@rikusen0335
Copy link

Could you share me what is expected folder structure? I was doing little near but always stuck at RUN mix deps.get due to Could not find a Mix.Project error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment