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

[FEAT] merge prod and dev Dockerfile #1194

Merged
merged 8 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
70 changes: 35 additions & 35 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,44 +1,25 @@
# [Node Stage to get node_modolues and node dependencies]
FROM node:8.16.0-buster-slim as node_stage
FROM node:8.16.0-buster-slim as node_base
# [Python Stage for Django web server]
FROM python:3.10.14-slim-bullseye as python_base
mattwang44 marked this conversation as resolved.
Show resolved Hide resolved

FROM node_base as node_deps
COPY ./yarn.lock yarn.lock
COPY ./package.json package.json

RUN apt-get update
RUN apt-get install python-pip -y

RUN npm install -g yarn
RUN yarn install --dev --frozen-lockfile \
mattwang44 marked this conversation as resolved.
Show resolved Hide resolved
&& rm -rf $HOME/.cache/yarn/*


# [Python Stage for Django web server]
FROM python:3.10.14-slim-bullseye as python_stage

ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1

ENV BASE_DIR /usr/local
ENV APP_DIR $BASE_DIR/app

COPY --from=node_stage /node_modules $APP_DIR/node_modules
COPY --from=node_stage /usr/local/bin/node /usr/local/bin/node

# make nodejs accessible and executable globally
ENV NODE_PATH $APP_DIR/node_modules/
ENV PATH /usr/local/bin:$PATH
RUN yarn install --dev --frozen-lockfile && yarn cache clean

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

# Infrastructure tools
# gettext is used for django to compile .po to .mo files.
mattwang44 marked this conversation as resolved.
Show resolved Hide resolved
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y \
libpq-dev \
gcc \
Expand All @@ -49,24 +30,43 @@ RUN apt-get install -y \
libxml2-dev \
libxslt-dev

# APP directory setup
RUN adduser --system --disabled-login docker \
&& mkdir -p "$BASE_DIR" "$APP_DIR" "$APP_DIR/src/assets" "$APP_DIR/src/media" \
&& chown -R docker:nogroup "$BASE_DIR" "$APP_DIR"
ENV PYTHONUNBUFFERED=1 \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_VIRTUALENVS_IN_PROJECT=true

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

# Install Python dependencies (only main dependencies)
COPY --chown=docker:nogroup pyproject.toml poetry.lock ./
RUN poetry install --no-root && \
# Install Python dependencies
COPY pyproject.toml poetry.lock ./
RUN poetry install --no-root --only main && \
yes | poetry cache clear --all pypi

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

# Finally, copy all the project files along with source files
# Make nodejs accessible and executable globally
ENV NODE_PATH $APP_DIR/node_modules/

FROM python_deps as dev
RUN poetry install --no-root --only dev

COPY --from=node_deps /node_modules $APP_DIR/node_modules
COPY --from=node_deps /usr/local/bin/node /usr/local/bin/node

FROM python_deps as build
RUN mkdir -p "$APP_DIR" "$APP_DIR/src/assets" "$APP_DIR/src/media"

FROM python_deps as prod
# APP directory setup
RUN adduser --system --disabled-login docker
# Use COPY --chown instead of RUN chown -R directly to avoid increasing image size
# https://github.com/pycontw/pycon.tw/pull/1194#discussion_r1593319742
COPY --chown=docker:nogroup --from=build $APP_DIR $APP_DIR
COPY --chown=docker:nogroup --from=node_deps /node_modules $APP_DIR/node_modules
COPY --chown=docker:nogroup --from=node_deps /usr/local/bin/node /usr/local/bin/node
COPY --chown=docker:nogroup ./ $APP_DIR

USER docker
Expand Down
52 changes: 0 additions & 52 deletions dev.Dockerfile

This file was deleted.

9 changes: 5 additions & 4 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ services:
container_name: pycontw_dev
build:
context: .
dockerfile: dev.Dockerfile
dockerfile: Dockerfile
target: dev
volumes:
- ./src:/app/src
- ./logs:/app/logs
- ./src:/usr/local/app/src
- ./logs:/usr/local/app/logs
ports:
- "8000:8000"
depends_on:
Expand All @@ -26,5 +27,5 @@ services:
- DJANGO_SUPERUSER_USERNAME=admin
- DJANGO_SUPERUSER_PASSWORD=1234
- [email protected]
working_dir: /app/src
working_dir: /usr/local/app/src
command: tail -f /dev/null
9 changes: 6 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
version: "3.5"
services:
web:
build: .
build:
context: .
dockerfile: Dockerfile
target: prod
container_name: pycontw-2024
hostname: pycontw-2024
entrypoint: ""
Expand Down Expand Up @@ -37,9 +40,9 @@ services:
volumes:
- ${MEDIA_ROOT}:/usr/local/app/src/media
networks:
- network
- network-2024
mattwang44 marked this conversation as resolved.
Show resolved Hide resolved

networks:
network:
network-2024:
external: true
name: network-2024
Loading