From 3ef9ceaeab9ad808816bd742ddb8e9e7990c474f Mon Sep 17 00:00:00 2001 From: Alex Lubbock Date: Wed, 2 Oct 2024 12:54:32 +0100 Subject: [PATCH] build: refactor multistage docker builds (#87) Move dev builds for frontend/backend in main Dockerfiles to reduce duplication --- .github/workflows/containers.yml | 2 ++ backend/Dockerfile | 22 +++++++++++++++++++++- backend/Dockerfile-dev | 19 ------------------- docker-compose.yml | 4 ++-- frontend/Dockerfile | 8 ++++++-- frontend/Dockerfile-dev | 13 ------------- 6 files changed, 31 insertions(+), 37 deletions(-) delete mode 100644 backend/Dockerfile-dev delete mode 100644 frontend/Dockerfile-dev diff --git a/.github/workflows/containers.yml b/.github/workflows/containers.yml index 214a957..cc36ab9 100644 --- a/.github/workflows/containers.yml +++ b/.github/workflows/containers.yml @@ -30,6 +30,7 @@ jobs: uses: docker/build-push-action@v3 with: context: backend + target: prod platforms: linux/amd64 push: ${{ github.event_name != 'pull_request' }} tags: quay.io/rosalindfranklininstitute/antigen-app-backend:latest @@ -38,6 +39,7 @@ jobs: uses: docker/build-push-action@v3 with: context: frontend + target: prod platforms: linux/amd64 push: ${{ github.event_name != 'pull_request' }} tags: quay.io/rosalindfranklininstitute/antigen-app-frontend:latest diff --git a/backend/Dockerfile b/backend/Dockerfile index a677f90..a9a0138 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -16,7 +16,7 @@ COPY antigenapi /usr/src/antigenapi RUN DJANGO_CI=true .venv/bin/python manage.py collectstatic --noinput -FROM python:3.11-slim-bullseye +FROM python:3.11-slim-bullseye AS prod RUN apt-get update && apt-get install -y \ libxml2 \ @@ -39,3 +39,23 @@ RUN adduser --uid 10191 --gid 10191 --system --no-create-home nonroot USER nonroot CMD [".venv/bin/uwsgi", "--ini", "uwsgi.ini"] + +FROM python:3.11-bullseye AS dev + +RUN python -m pip install wheel pipenv + +COPY Pipfile Pipfile.lock pyproject.toml setup.cfg /usr/src/ +WORKDIR /usr/src + +RUN python -m pipenv install --system --verbose +RUN python -m pipenv install --dev + +COPY manage.py /usr/src/ +COPY antigendjango /usr/src/antigendjango +COPY antigenapi /usr/src/antigenapi + +RUN DJANGO_CI=true python manage.py collectstatic --noinput \ + && mkdir /api_data/ \ + && mv ./static/ /api_data/static/ + +CMD ["python", "manage.py", "runserver", "0.0.0.0:8080"] diff --git a/backend/Dockerfile-dev b/backend/Dockerfile-dev deleted file mode 100644 index 722fdb9..0000000 --- a/backend/Dockerfile-dev +++ /dev/null @@ -1,19 +0,0 @@ -FROM python:3.11-bullseye AS dev - -RUN python -m pip install wheel pipenv - -COPY Pipfile Pipfile.lock pyproject.toml setup.cfg /usr/src/ -WORKDIR /usr/src - -RUN python -m pipenv install --system --verbose -RUN python -m pipenv install --dev - -COPY manage.py /usr/src/ -COPY antigendjango /usr/src/antigendjango -COPY antigenapi /usr/src/antigenapi - -RUN DJANGO_CI=true python manage.py collectstatic --noinput \ - && mkdir /api_data/ \ - && mv ./static/ /api_data/static/ - -CMD ["python", "manage.py", "runserver", "0.0.0.0:8080"] diff --git a/docker-compose.yml b/docker-compose.yml index 61993bc..8692192 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ services: container_name: antigenapp_app build: context: frontend - dockerfile: Dockerfile-dev + target: dev restart: always ports: - 3000:3000 @@ -15,7 +15,7 @@ services: container_name: antigenapp_api build: context: backend - dockerfile: Dockerfile-dev + target: dev restart: always volumes: - ./backend:/usr/src diff --git a/frontend/Dockerfile b/frontend/Dockerfile index fd88014..8bc32ea 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -1,4 +1,4 @@ -FROM registry.hub.docker.com/library/node:18-alpine as build-stage +FROM registry.hub.docker.com/library/node:18-alpine AS dev ENV ANTIGENAPP_DIR /antigenapp @@ -10,9 +10,13 @@ RUN npm install COPY . ${ANTIGENAPP_DIR} +CMD ["npm", "start"] + +FROM dev AS build-stage + RUN npm run build -FROM nginx:stable-alpine as prod +FROM nginx:stable-alpine AS prod COPY --from=build-stage /antigenapp/build/ /usr/share/nginx/html COPY nginx-check-env.sh /docker-entrypoint.d/nginx-check-env.sh diff --git a/frontend/Dockerfile-dev b/frontend/Dockerfile-dev deleted file mode 100644 index 483b51b..0000000 --- a/frontend/Dockerfile-dev +++ /dev/null @@ -1,13 +0,0 @@ -FROM registry.hub.docker.com/library/node:18-alpine as dev - -ENV ANTIGENAPP_DIR /antigenapp - -COPY package-lock.json ${ANTIGENAPP_DIR}/package-lock.json -COPY package.json ${ANTIGENAPP_DIR}/package.json -WORKDIR ${ANTIGENAPP_DIR} - -RUN npm install - -COPY . ${ANTIGENAPP_DIR} - -CMD ["npm", "start"]