-
-
Save kofiasare/05adac12705967b369082f9f8ae112e3 to your computer and use it in GitHub Desktop.
Phoenix with docker compose
This file contains hidden or 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
PGHOST=db | |
PGUSER=postgres | |
PGPASSWORD=postgres | |
PGDATABASE=hello_dev | |
PGPORT=5432 |
This file contains hidden or 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 :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], |
This file contains hidden or 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.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: |
This file contains hidden or 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: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"] |
This file contains hidden or 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/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 |
This file contains hidden or 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
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