Skip to content

chore: optimize dockerfile #61

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
92 changes: 76 additions & 16 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,21 +1,81 @@
# repo and project folders
.devcontainer
.docs
# Version control
.git
.github
.gitignore
.gitmodules
.idea
.vscode

# docker
Dockerfile
.dockerignore
docker-compose.yaml

# python related
.pyright_cache
.pytest_cache
.gitattributes

# Development containers
.devcontainer/
.vscode/

# Documentation
docs/
*.txt

# Python cache and temporary files
__pycache__/
*.pyc
*.pyo
*.pyd
.Python
.pytest_cache/
.coverage
.coverage.*
.cache
.mypy_cache/
.ruff_cache/
.dmypy.json
dmypy.json

# Virtual environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Build artifacts
build/
dist/
*.egg-info/
.eggs/

# IDE files
.idea/
.vscode/
*.swp
*.swo
*~

# OS files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# Scripts (if not needed in container)
scripts/

# Log files
*.log
logs/

# Temporary files
tmp/
temp/
.tmp/
.temp/

# Test artifacts
.tox/
.nox/
htmlcov/
.coverage
coverage.xml
*.cover
*.py,cover
.hypothesis/
17 changes: 13 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ ARG USER=appuser

ENV RUNTIME_PACKAGES=libpq-dev
# These packages will be deleted from the final image, after the application is packaged
ENV BUILD_PACKAGES=gcc
ENV BUILD_PACKAGES="gcc build-essential python3-dev"

RUN apt-get update \
&& apt-get install -y ${BUILD_PACKAGES} ${RUNTIME_PACKAGES} \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
&& apt-get install -y --no-install-recommends ${BUILD_PACKAGES} ${RUNTIME_PACKAGES} \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /tmp/* /var/tmp/*

RUN mkdir -p /opt/app/${PROJECT_NAME}

Expand All @@ -32,15 +34,20 @@ RUN pip install --upgrade pip \
&& pip install --user uv==${UV_VERSION}

WORKDIR /opt/app/${PROJECT_NAME}
COPY --chown=${USER}:${USER} . .

# Only copy files needed to install dependencies so it can be cached
# (changes to source files won't invalidate cache at this point)
COPY --chown=${USER}:${USER} pyproject.toml uv.lock ./
RUN uv sync --frozen --no-cache --no-install-project --no-default-groups

# Then copy the rest of the application
COPY --chown=${USER}:${USER} . .

# ----
# Devcontainer adds extra tools for development
FROM base AS devcontainer

ARG USER=appuser
USER root

# Add any other tool usefull during development to the following list, this won't be included
Expand Down Expand Up @@ -80,6 +87,8 @@ RUN uv build --wheel
# Deployment stage to run in cloud environments. This must be the last stage, which is used to run the application by default
FROM base AS deployment

ARG PROJECT_NAME=python-template

# root is needed to remove build dependencies
USER root
RUN apt-get purge -y ${BUILD_PACKAGES} \
Expand Down