Skip to content

Commit

Permalink
Fix docker-compose start up order
Browse files Browse the repository at this point in the history
Add db.env --> store shared database environment variables
Add wait-for-postgres.sh --> let crm container run django server after
database being ready to connect
  • Loading branch information
KingCSharp committed Mar 28, 2019
1 parent 30b08e2 commit 34d0b9b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
10 changes: 6 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ COPY . /app/

RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - && \
apt update && \
apt install -y git ruby-dev nodejs && \
apt install -y git ruby-dev nodejs postgresql-client && \
apt clean && \
gem install compass sass && \
npm -g install less && \
pip install -r requirements.txt && \
pip install redis coveralls
pip install --no-cache-dir -r requirements.txt && \
pip install --no-cache-dir redis

RUN chmod +x /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh \
/app/wait-for-postgres.sh
ENTRYPOINT ["/app/entrypoint.sh"]
6 changes: 6 additions & 0 deletions db.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Database environment variables
POSTGRES_USER=postgres
POSTGRES_PASSWORD=root
POSTGRES_DB=dj_crm
DB_HOST=db
DB_PORT=5432
11 changes: 4 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ services:
image: postgres
# volumes:
# - /home/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: "root"
POSTGRES_USER: "postgres"
POSTGRES_DB: "dj_crm"
env_file:
- ./db.env

web:
build: .
image: micropyramid/django-crm:1
environment:
DB_HOST: db
DB_PORT: 5432
env_file:
- ./db.env
ports:
- "8001:8000"
depends_on:
Expand Down
1 change: 1 addition & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

# Execute startup scripts
./wait-for-postgres.sh $DB_HOST
python manage.py collectstatic --noinput
python manage.py migrate
python manage.py runserver 0.0.0.0:8000
18 changes: 18 additions & 0 deletions wait-for-postgres.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
# wait-for-postgres.sh

set -e

host="$1"
shift
cmd="$@"

echo "Testing Postgres connection with psql ..."
until PGPASSWORD=$POSTGRES_PASSWORD psql -h "$host" -U "postgres" -c '\q'; do
#~ >&2 echo "Postgres is unavailable - sleeping"
>&2 echo -n .
sleep 1
done

>&2 echo "Postgres is up - executing command"
exec $cmd

0 comments on commit 34d0b9b

Please sign in to comment.