From 63bb00838846146acbefc5edcfdf3a9a06c2e5ce Mon Sep 17 00:00:00 2001 From: yu23ki14 Date: Tue, 30 Jul 2024 18:46:20 +0900 Subject: [PATCH] modify docker file for dev env --- api/Dockerfile.dev | 52 ++++++++++++++++++++++++++++++++++++++++++ compose.yml | 19 ++++++++------- migrate/Dockerfile.dev | 52 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 api/Dockerfile.dev create mode 100644 migrate/Dockerfile.dev diff --git a/api/Dockerfile.dev b/api/Dockerfile.dev new file mode 100644 index 0000000..1c439b9 --- /dev/null +++ b/api/Dockerfile.dev @@ -0,0 +1,52 @@ +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 api/pyproject.toml api/README.md ./ +COPY api/birdxplorer_api/__init__.py ./birdxplorer_api/ + +RUN if [ "${ENVIRONMENT}" = "prod" ]; then \ + apt-get update && apt-get install -y --no-install-recommends \ + postgresql-client-15 \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/*; \ + fi + +RUN python -m pip install --no-cache-dir --upgrade pip && \ + pip install --no-cache-dir -e ".[${ENVIRONMENT}]" + +COPY ../common ./common +RUN if [ "${ENVIRONMENT}" = "dev" ]; then \ + pip install -e ./common; \ + fi + +FROM python:${PYTHON_VERSION_CODE}-slim-bookworm as runner +ARG PYTHON_VERSION_CODE +ARG ENVIRONMENT + +WORKDIR /app + +RUN if [ "${ENVIRONMENT}" = "prod" ]; then \ + apt-get update && apt-get install -y --no-install-recommends \ + libpq5 \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/*; \ + fi + +RUN groupadd -r app && useradd -r -g app app +RUN chown -R app:app /app +USER app + +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 api ./ +COPY ../common ./common + +ENTRYPOINT ["python", "-m", "uvicorn", "birdxplorer_api.main:app", "--host", "0.0.0.0"] diff --git a/compose.yml b/compose.yml index 7d36098..df2676f 100644 --- a/compose.yml +++ b/compose.yml @@ -1,4 +1,4 @@ -version: '3.1' +version: "3.1" services: db: @@ -14,7 +14,7 @@ services: timeout: 5s retries: 5 ports: - - '5432:5432' + - "5432:5432" volumes: - postgres_data:/var/lib/postgresql/data app: @@ -24,17 +24,17 @@ services: build: args: - ENVIRONMENT=dev - context: ./api - dockerfile: Dockerfile + context: ./ + dockerfile: ./api/Dockerfile.dev env_file: - .env ports: - - '8000:8000' + - "8000:8000" develop: watch: - action: rebuild - path: ./api - target: /app/api + path: ./ + target: /app migrate: depends_on: db: @@ -42,13 +42,12 @@ services: build: args: - ENVIRONMENT=dev - context: ./migrate - dockerfile: Dockerfile + context: ./ + dockerfile: ./migrate/Dockerfile.dev environment: - WAIT_HOSTS=db:5432 env_file: - .env - volumes: postgres_data: diff --git a/migrate/Dockerfile.dev b/migrate/Dockerfile.dev new file mode 100644 index 0000000..3057101 --- /dev/null +++ b/migrate/Dockerfile.dev @@ -0,0 +1,52 @@ +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 migrate/pyproject.toml migrate/README.md ./ +COPY migrate/birdxplorer_migration/__init__.py ./birdxplorer_migration/ + +RUN if [ "${ENVIRONMENT}" = "prod" ]; then \ + apt-get update && apt-get install -y --no-install-recommends \ + postgresql-client-15 \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/*; \ + fi + +RUN python -m pip install --no-cache-dir --upgrade pip && \ + pip install --no-cache-dir -e ".[${ENVIRONMENT}]" + +COPY ../common ./common +RUN if [ "${ENVIRONMENT}" = "dev" ]; then \ + pip install -e ./common; \ + fi + +FROM python:${PYTHON_VERSION_CODE}-slim-bookworm as runner +ARG PYTHON_VERSION_CODE +ARG ENVIRONMENT + +WORKDIR /app + +RUN if [ "${ENVIRONMENT}" = "prod" ]; then \ + apt-get update && apt-get install -y --no-install-recommends \ + libpq5 \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/*; \ + fi + +RUN groupadd -r app && useradd -r -g app app +RUN chown -R app:app /app +USER app + +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 migrate ./ +COPY ../common ./common + +ENTRYPOINT ["python", "birdxplorer_migration/scripts/migrate_all.py", "birdxplorer_migration/data/appv1/"]