Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
chore: enable wait for it via env var (#31)
Browse files Browse the repository at this point in the history
* chore: add wait-for-it to dockerfile

* feat: add and document wait-for-it behaviour

* chore: fix use of wait-for-it

* chore: raise timeout to 120 seconds

* chore: add prints to entrypoint
  • Loading branch information
sralloza authored Jan 28, 2022
1 parent 28a03ad commit 3d73017
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ WORKDIR /code
ENV PYTHONPATH "${PYTHONPATH}:/code"
ENV PORT=8000
ENV GET_POETRY https://raw.github.com/python-poetry/poetry/master/get-poetry.py
ENV WAIT_FOR_IT_URL https://raw.github.com/vishnubob/wait-for-it/master/wait-for-it.sh

EXPOSE ${PORT}

RUN apk update && \
apk upgrade && \
apk add curl gcc musl-dev build-base
apk add curl gcc musl-dev build-base bash

# Install Poetry
RUN curl -sSL ${GET_POETRY} | POETRY_HOME=/opt/poetry python && \
Expand All @@ -23,6 +24,10 @@ COPY ./pyproject.toml ./poetry.lock* /code/

RUN poetry install --no-root --no-dev

# Download wait-for-it.sh
ADD ${WAIT_FOR_IT_URL} /
RUN chmod +x /wait-for-it.sh

COPY ./scripts /code/scripts
COPY ./alembic.ini .
COPY ./alembic ./alembic
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ You need to supply the following environment variables (required ones are marked
- 🚩 **MYSQL_PASSWORD** (`str`): mysql password.
- 🚩 **MYSQL_PORT** (`str`): mysql port.
- 🚩 **MYSQL_USER** (`str`): mysql user.
- **WAIT_FOR_IT_ADDRESS** (`str`): if is set, it will wait for the database to be ready for max 120 seconds. Must be set to `$MYSQL_HOST:$MYSQL_PORT`. This switch should not be used in Kubernetes deployments, as initContainers are designed to cover this exact use case.

### Other

Expand Down
10 changes: 10 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#! /usr/bin/env sh
set -e

if [[ -z "${WAIT_FOR_IT_ADDRESS}" ]]; then
echo "Skipping wait-for-it ($$WAIT_FOR_IT_ADDRESS is not defined)"
else
echo "running wait-for-it.sh -t 120 ${WAIT_FOR_IT_ADDRESS}"
/wait-for-it.sh -t 120 "${WAIT_FOR_IT_ADDRESS}"
fi

echo "Ensuring database existence"
python ./scripts/ensure-database.py

echo "Upgrading database"
alembic upgrade head

echo "Launching uvicorn server"
exec uvicorn app.main:app --host 0.0.0.0 --port ${PORT}

0 comments on commit 3d73017

Please sign in to comment.