From 3c77c58e85d65d11caa3513584acce1070f733b4 Mon Sep 17 00:00:00 2001 From: sushi-chaaaan Date: Mon, 13 May 2024 14:23:41 +0900 Subject: [PATCH] feat(docker): execute db migration on docker compose --- compose.yml | 10 ++++++++++ migrate/Dockerfile | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 migrate/Dockerfile diff --git a/compose.yml b/compose.yml index e68f920..f2900e7 100644 --- a/compose.yml +++ b/compose.yml @@ -29,6 +29,16 @@ services: - action: rebuild path: ./api target: /app/api + migrate: + depends_on: + - db + build: + args: + - ENVIRONMENT=dev + context: ./migrate + dockerfile: Dockerfile + env_file: + - .env volumes: diff --git a/migrate/Dockerfile b/migrate/Dockerfile new file mode 100644 index 0000000..646f3a5 --- /dev/null +++ b/migrate/Dockerfile @@ -0,0 +1,34 @@ +ARG PYTHON_VERSION_CODE=3.10 +ARG ENVIRONMENT="dev" +# ENVIRONMENT: dev or prod, refer to project.optional-dependencies in pyproject.toml + +FROM python:${PYTHON_VERSION_CODE}-bookworm as builder +ARG PYTHON_VERSION_CODE +ARG ENVIRONMENT + +WORKDIR /app +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +COPY pyproject.toml README.md ./ +COPY birdxplorer_migration/__init__.py ./birdxplorer_migration/ +RUN pip install --no-cache-dir -e ".[${ENVIRONMENT}]" + +COPY --from=ghcr.io/ufoscout/docker-compose-wait:latest /wait /wait + +FROM python:${PYTHON_VERSION_CODE}-slim-bookworm as runner +ARG PYTHON_VERSION_CODE + +WORKDIR /app + +RUN groupadd -r app && useradd -r -g app app +RUN chown -R app:app /app +USER app + + +COPY --from=builder /wait /wait +COPY --from=builder /usr/local/lib/python${PYTHON_VERSION_CODE}/site-packages /usr/local/lib/python${PYTHON_VERSION_CODE}/site-packages +COPY --chown=app:app . ./ + +ENV WAIT_COMMAND="python birdxplorer_migration/scripts/migrations/migrate_all.py birdxplorer_migration/data/appv1/" +ENTRYPOINT ["/wait"]