Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker-Multi Stage Build #770

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion backend/Dockerfile.production
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.13-slim
FROM python:3.13-slim AS builder

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

Expand All @@ -23,3 +23,21 @@ COPY settings settings
COPY static static
COPY templates templates
COPY manage.py wsgi.py ./

FROM python:3.13-slim

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

RUN groupadd owasp && \
useradd --create-home --home-dir /home/owasp -g owasp owasp && \
apt-get update && \
apt-get install -y libpq-dev && \
apt-get clean -y && rm -rf /var/lib/apt/lists/*

ENV PYTHONUNBUFFERED=1

WORKDIR /home/owasp

USER owasp

COPY --from=builder /home/owasp /home/owasp
21 changes: 19 additions & 2 deletions backend/Dockerfile.staging
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.13-slim
FROM python:3.13-slim AS builder

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

Expand All @@ -10,7 +10,6 @@ RUN groupadd owasp && \
python -m pip install --no-cache-dir poetry

ENV PYTHONUNBUFFERED=1

WORKDIR /home/owasp

USER owasp
Expand All @@ -23,3 +22,21 @@ COPY settings settings
COPY static static
COPY templates templates
COPY manage.py wsgi.py ./

FROM python:3.13-slim

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

RUN groupadd owasp && \
useradd --create-home --home-dir /home/owasp -g owasp owasp && \
apt-get update && \
apt-get install -y libpq-dev && \
apt-get clean -y && rm -rf /var/lib/apt/lists/*

ENV PYTHONUNBUFFERED=1

WORKDIR /home/owasp

USER owasp

COPY --from=builder /home/owasp /home/owasp
23 changes: 22 additions & 1 deletion backend/Dockerfile.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.13-slim
FROM python:3.13-slim AS builder

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

Expand Down Expand Up @@ -26,3 +26,24 @@ COPY settings settings
COPY static static
COPY templates templates
COPY tests tests

FROM python:3.13-slim

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

RUN groupadd owasp && \
useradd --create-home --home-dir /home/owasp -g owasp owasp && \
apt-get update && \
apt-get install -y libpq-dev && \
apt-get clean -y && rm -rf /var/lib/apt/lists/*

ENV FORCE_COLOR=1
ENV PYTHONUNBUFFERED=1

WORKDIR /home/owasp

USER owasp

COPY --from=builder /home/owasp /home/owasp

CMD ["/bin/bash", "-c", "VENV_DIR=$(ls -d /home/owasp/.cache/pypoetry/virtualenvs/*) && $VENV_DIR/bin/pytest"]
2 changes: 1 addition & 1 deletion backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ sync-data: update-data enrich-data index-data

test-backend:
@docker build -f backend/Dockerfile.test backend -t nest-test-backend
@docker run -e DJANGO_CONFIGURATION=Test nest-test-backend poetry run pytest
@docker run -e DJANGO_CONFIGURATION=Test nest-test-backend

update-data: \
github-update-owasp-organization \
Expand Down
33 changes: 26 additions & 7 deletions frontend/Dockerfile.test
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
FROM node:22-slim
FROM node:22-slim AS builder

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

ENV FORCE_COLOR=1

RUN groupadd owasp && \
useradd --create-home --home-dir /home/owasp -g owasp owasp && \
apt-get update && apt-get upgrade -y && \
apt-get clean -y && rm -rf /var/lib/apt/lists/*

WORKDIR /home/owasp
WORKDIR /app

COPY package.json package-lock.json ./
RUN npm install
Expand All @@ -21,5 +16,29 @@ COPY jest.config.ts jest.config.ts
COPY tsconfig.json tsconfig.json
COPY __tests__ __tests__

FROM node:22-slim

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

ENV FORCE_COLOR=1

RUN groupadd owasp && \
useradd --create-home --home-dir /home/owasp -g owasp owasp && \
apt-get update && apt-get upgrade -y && \
apt-get clean -y && rm -rf /var/lib/apt/lists/*

WORKDIR /home/owasp

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./

COPY --from=builder /app/src ./src
COPY --from=builder /app/public ./public
COPY --from=builder /app/jest.setup.ts ./jest.setup.ts
COPY --from=builder /app/jest.config.ts ./jest.config.ts
COPY --from=builder /app/tsconfig.json ./tsconfig.json
COPY --from=builder /app/__tests__ ./__tests__

USER owasp

CMD ["npm" , "run" , "test"]
2 changes: 1 addition & 1 deletion frontend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ shell-frontend:

test-frontend:
@docker build -f frontend/Dockerfile.test frontend -t nest-test-frontend
@docker run --env-file frontend/.env.example nest-test-frontend npm run test
@docker run --env-file frontend/.env.example nest-test-frontend
Loading