Skip to content

Instantly share code, notes, and snippets.

@kofiasare
Last active April 17, 2024 13:58
Show Gist options
  • Save kofiasare/05adac12705967b369082f9f8ae112e3 to your computer and use it in GitHub Desktop.
Save kofiasare/05adac12705967b369082f9f8ae112e3 to your computer and use it in GitHub Desktop.
Phoenix with docker compose
PGHOST=db
PGUSER=postgres
PGPASSWORD=postgres
PGDATABASE=hello_dev
PGPORT=5432
config :hello, Hello.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"),
...
config :hello, HelloWeb.Endpoint,
http: [ip: {0, 0, 0, 0}, port: 4000],
version: '3.9'
services:
db:
image: postgres:14.4-alpine
volumes:
- pgdata:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
app:
build: .
environment:
- MIX_ENV=dev
env_file:
- .env
depends_on:
- db
ports:
- 4000:4000
volumes:
- .:/app
volumes:
pgdata:
FROM elixir:1.14-alpine
# install debian packages
RUN apk add --no-cache &&\
apk add --virtual build-dependencies\
build-base\
inotify-tools\
postgresql-client\
vim\
git
WORKDIR /app
COPY mix.exs .
COPY mix.lock .
ADD . /app
# install phoenix packages
RUN mix local.hex --force && \
mix local.rebar --force && \
mix archive.install --force hex phx_new
RUN mix deps.get
EXPOSE 4000
CMD ["/app/entrypoint.sh"]
#!/bin/sh
# 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
Head over to (installation)['https://hexdocs.pm/phoenix/installation.html') if you don't have elixir, hex or phoenix application generator installed
1. Run the phoenix generator with your app name
$ mix phx.new hello
if prompted on cli to install app dependencies type Y
2. Copy the above files to the application you just generated.
3. make the entrypoint.sh an executable
$ sudo chmod a+x entrypoint.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment