Skip to content

Commit 208b988

Browse files
authored
Merge pull request #1194 from pycontw/feature/merge-dockerfile-into-one
[FEAT] merge prod and dev Dockerfile
2 parents b1301c9 + 0fa2454 commit 208b988

File tree

4 files changed

+46
-94
lines changed

4 files changed

+46
-94
lines changed

Dockerfile

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,25 @@
11
# [Node Stage to get node_modolues and node dependencies]
2-
FROM node:8.16.0-buster-slim as node_stage
2+
FROM node:8.16.0-buster-slim as node_base
3+
# [Python Stage for Django web server]
4+
FROM python:3.10.14-slim-bullseye as python_base
35

6+
FROM node_base as node_deps
47
COPY ./yarn.lock yarn.lock
58
COPY ./package.json package.json
69

710
RUN apt-get update
811
RUN apt-get install python-pip -y
912

1013
RUN npm install -g yarn
11-
RUN yarn install --dev --frozen-lockfile \
12-
&& rm -rf $HOME/.cache/yarn/*
13-
14-
15-
# [Python Stage for Django web server]
16-
FROM python:3.10.14-slim-bullseye as python_stage
17-
18-
ENV PYTHONUNBUFFERED=1 \
19-
PYTHONDONTWRITEBYTECODE=1 \
20-
PIP_DISABLE_PIP_VERSION_CHECK=on \
21-
PIP_DEFAULT_TIMEOUT=100 \
22-
POETRY_HOME="/opt/poetry" \
23-
POETRY_VIRTUALENVS_IN_PROJECT=true \
24-
POETRY_NO_INTERACTION=1
25-
26-
ENV BASE_DIR /usr/local
27-
ENV APP_DIR $BASE_DIR/app
28-
29-
COPY --from=node_stage /node_modules $APP_DIR/node_modules
30-
COPY --from=node_stage /usr/local/bin/node /usr/local/bin/node
31-
32-
# make nodejs accessible and executable globally
33-
ENV NODE_PATH $APP_DIR/node_modules/
34-
ENV PATH /usr/local/bin:$PATH
14+
RUN yarn install --dev --frozen-lockfile && yarn cache clean
3515

36-
# Add bin directory used by `pip install --user`
37-
ENV PATH /home/docker/.local/bin:$PATH
16+
FROM python_base as python_deps
17+
ENV APP_DIR /usr/local/app
3818

3919
# Infrastructure tools
4020
# gettext is used for django to compile .po to .mo files.
4121
RUN apt-get update
22+
RUN apt-get upgrade -y
4223
RUN apt-get install -y \
4324
libpq-dev \
4425
gcc \
@@ -49,24 +30,43 @@ RUN apt-get install -y \
4930
libxml2-dev \
5031
libxslt-dev
5132

52-
# APP directory setup
53-
RUN adduser --system --disabled-login docker \
54-
&& mkdir -p "$BASE_DIR" "$APP_DIR" "$APP_DIR/src/assets" "$APP_DIR/src/media" \
55-
&& chown -R docker:nogroup "$BASE_DIR" "$APP_DIR"
33+
ENV PYTHONUNBUFFERED=1 \
34+
PIP_DISABLE_PIP_VERSION_CHECK=on \
35+
PIP_DEFAULT_TIMEOUT=100 \
36+
POETRY_VIRTUALENVS_IN_PROJECT=true
5637

5738
# Install Poetry
5839
RUN pip install --no-cache-dir pip==23.3.2 && \
5940
pip install --no-cache-dir poetry==1.8.2
6041

61-
# Install Python dependencies (only main dependencies)
62-
COPY --chown=docker:nogroup pyproject.toml poetry.lock ./
63-
RUN poetry install --no-root && \
42+
# Install Python dependencies
43+
COPY pyproject.toml poetry.lock ./
44+
RUN poetry install --no-root --only main && \
6445
yes | poetry cache clear --all pypi
6546

6647
# Add poetry bin directory to PATH
6748
ENV PATH="${WORKDIR}/.venv/bin:$PATH"
6849

69-
# Finally, copy all the project files along with source files
50+
# Make nodejs accessible and executable globally
51+
ENV NODE_PATH $APP_DIR/node_modules/
52+
53+
FROM python_deps as dev
54+
RUN poetry install --no-root --only dev
55+
56+
COPY --from=node_deps /node_modules $APP_DIR/node_modules
57+
COPY --from=node_deps /usr/local/bin/node /usr/local/bin/node
58+
59+
FROM python_deps as build
60+
RUN mkdir -p "$APP_DIR" "$APP_DIR/src/assets" "$APP_DIR/src/media"
61+
62+
FROM python_deps as prod
63+
# APP directory setup
64+
RUN adduser --system --disabled-login docker
65+
# Use COPY --chown instead of RUN chown -R directly to avoid increasing image size
66+
# https://github.com/pycontw/pycon.tw/pull/1194#discussion_r1593319742
67+
COPY --chown=docker:nogroup --from=build $APP_DIR $APP_DIR
68+
COPY --chown=docker:nogroup --from=node_deps /node_modules $APP_DIR/node_modules
69+
COPY --chown=docker:nogroup --from=node_deps /usr/local/bin/node /usr/local/bin/node
7070
COPY --chown=docker:nogroup ./ $APP_DIR
7171

7272
USER docker

dev.Dockerfile

Lines changed: 0 additions & 52 deletions
This file was deleted.

docker-compose-dev.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ services:
1414
container_name: pycontw_dev
1515
build:
1616
context: .
17-
dockerfile: dev.Dockerfile
17+
dockerfile: Dockerfile
18+
target: dev
1819
volumes:
19-
- ./src:/app/src
20-
- ./logs:/app/logs
20+
- ./src:/usr/local/app/src
21+
- ./logs:/usr/local/app/logs
2122
ports:
2223
- "8000:8000"
2324
depends_on:
@@ -26,5 +27,5 @@ services:
2627
- DJANGO_SUPERUSER_USERNAME=admin
2728
- DJANGO_SUPERUSER_PASSWORD=1234
2829
- DJANGO_SUPERUSER_EMAIL=admin@pycon.tw
29-
working_dir: /app/src
30+
working_dir: /usr/local/app/src
3031
command: tail -f /dev/null

docker-compose.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
version: "3.5"
22
services:
33
web:
4-
build: .
4+
build:
5+
context: .
6+
dockerfile: Dockerfile
7+
target: prod
58
container_name: pycontw-2024
69
hostname: pycontw-2024
710
entrypoint: ""
@@ -37,9 +40,9 @@ services:
3740
volumes:
3841
- ${MEDIA_ROOT}:/usr/local/app/src/media
3942
networks:
40-
- network
43+
- network-2024
4144

4245
networks:
43-
network:
46+
network-2024:
4447
external: true
4548
name: network-2024

0 commit comments

Comments
 (0)