Skip to content

Commit

Permalink
configure entrypoint to create_db on startup of flask service via doc…
Browse files Browse the repository at this point in the history
…ker-compose
  • Loading branch information
kcrane3576 committed Jun 12, 2022
1 parent a9288e6 commit 1ef4368
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
10 changes: 6 additions & 4 deletions .env.dev-sample
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Flask
FLASK_APP=project/__init__.py
FLASK_ENV=development
# db is a specific reference to the service in the docker-compose.yml file
DATABASE_URL=postgresql://<user>:<password>@db:5432/<db>

# Postgres
POSTGRES_USER=<user>
POSTGRES_PASSWORD=<password>
POSTGRES_DB=<db>
SQL_HOST=<db>
SQL_HOST=db
SQL_PORT=5432
DATABASE=postgres
DATABASE=postgres

# SQLAlchemy
# db is a specific reference to the service in the docker-compose.yml file
DATABASE_URL=postgresql://<POSTGRES_USER>:<POSTGRES_PASSWORD>@<SQL_HOST>:5432/<POSTGRES_DB>
10 changes: 8 additions & 2 deletions services/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
# install system dependencies
RUN apt-get update && apt-get install -y netcat

# install app dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

# copy project
COPY . /usr/src/app/
COPY . /usr/src/app/

# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
18 changes: 18 additions & 0 deletions services/web/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
# Verify Postgres is up and healthy
# Note: File permissions requirement
# - chmod +x services/web/entrypoint.sh
if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."

while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done

echo "PostgreSQL started"
fi

python manage.py create_db

exec "$@"

0 comments on commit 1ef4368

Please sign in to comment.