Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d671417
init commit of efficient python dockerfile
Dec 18, 2024
6808db6
fix: unnecessary functionality for leadspotr example
Jan 17, 2025
a7d7a48
add: final good dockerfile
Jan 17, 2025
1940d2f
fix: update dependencies
Jan 17, 2025
dc37862
fix: add development stage
Jan 17, 2025
50e60ea
fix: update name of container to app
Jan 17, 2025
31e1af1
fix: update dependency
Jan 17, 2025
7d11e1a
fix: remove unneccesary dependency
Jan 17, 2025
88a75ca
fix: rename main module
Jan 17, 2025
fd744fd
add: the different dockerfile versions
Jan 17, 2025
37bb15f
fix: final docker image
Jan 17, 2025
fd73a65
fix: readme to be easier to follow and add the no-cache flag
Jan 17, 2025
2b5c26a
Code formatted with Ruff
Jan 17, 2025
5cf9ce6
WiP on improving dockerfile code example.
egges Jan 30, 2025
dc198b8
fix: Add comment on top of 01 dockerfile
Jan 31, 2025
be95dbb
fix: rename step 9
Jan 31, 2025
55637f6
fix: Working UV setup
Jan 31, 2025
49a8599
fix: spelling error
Jan 31, 2025
dd1636d
fix: multistage build example
Jan 31, 2025
6079ba4
fix: bettercopy example
Jan 31, 2025
2c5d8bd
fix: renmae mount secrets dockerfile
Jan 31, 2025
b772a46
fix: remove debug file
Jan 31, 2025
d2d9860
fix: mount secrets step
Jan 31, 2025
19a3f20
fix: final image with development stage
Jan 31, 2025
eab64df
fix: readme
Jan 31, 2025
ba9f976
fix: add cache flag
Jan 31, 2025
d52878b
fix: add no cache command and remove irrelevant comments
Jan 31, 2025
e2e8134
Example cleanup
egges Feb 4, 2025
6d7d059
Fixes in README file
egges Feb 4, 2025
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
40 changes: 40 additions & 0 deletions 2025/efficient-python-dockerfile/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Dockerfile
# Hidden files
.dockerignore
.git
.DS_Store
.vscode

# Build artifacts
*.pyc
*.so
*.egg
*.whl

# Local settings
.env
.env.local
.env.development
.env.test
.env.production
local.properties

# IDE files
.idea
*.iml
.classpath
.project

# Log files
*.log
*.err

# Test directories
test
tests
coverage
.pytest_cache

# Virtual environment
.venv
venv
4 changes: 4 additions & 0 deletions 2025/efficient-python-dockerfile/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__
.venv
.env*
service-account.json
1 change: 1 addition & 0 deletions 2025/efficient-python-dockerfile/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
12 changes: 12 additions & 0 deletions 2025/efficient-python-dockerfile/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[python]": {
"editor.defaultFormatter": null
},
"editor.formatOnSaveMode": "file",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "always"
},
"python.languageServer": "Pylance"
}
38 changes: 38 additions & 0 deletions 2025/efficient-python-dockerfile/Dockerfile.01_original
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM ubuntu

RUN apt-get update && apt-get install -y \
build-essential \
python3 \
curl \
ca-certificates && \
apt-get clean

RUN curl -sSL https://install.python-poetry.org | python3 -

# Set up the Poetry environment path correctly
ENV PATH="/root/.local/bin:${PATH}"

# Set environment variables for database and secrets
ENV DB_PASSWORD=${DB_PASSWORD}
ENV DB_USER=${DB_USER}
ENV DB_NAME=${DB_NAME}
ENV DB_HOST=${DB_HOST}
ENV ACCESS_TOKEN_SECRET_KEY=${ACCESS_TOKEN_SECRET_KEY}

WORKDIR /app

# Copy the application files into the container
COPY . .

# Install dependencies with Poetry
RUN poetry config virtualenvs.in-project true
RUN poetry install --no-interaction --no-root

# Set up the virtual environment path correctly for the appuser
ENV PATH="/app/.venv/bin:${PATH}"

# Expose the application port
EXPOSE $PORT

# Start the FastAPI application using Uvicorn
CMD ["uvicorn", "src.main:app", "--log-level", "info", "--host", "0.0.0.0" , "--port", "8080"]
37 changes: 37 additions & 0 deletions 2025/efficient-python-dockerfile/Dockerfile.02_betterimg
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM python:slim-bookworm

RUN apt-get update && apt-get install -y \
build-essential \
curl \
ca-certificates && \
apt-get clean

RUN curl -sSL https://install.python-poetry.org | python3 -

# Set up the Poetry environment path correctly
ENV PATH="/root/.local/bin:${PATH}"

# Set environment variables for database and secrets
ENV DB_PASSWORD=${DB_PASSWORD}
ENV DB_USER=${DB_USER}
ENV DB_NAME=${DB_NAME}
ENV DB_HOST=${DB_HOST}
ENV ACCESS_TOKEN_SECRET_KEY=${ACCESS_TOKEN_SECRET_KEY}

WORKDIR /app

# Copy the application files into the container
COPY . .

# Install dependencies with Poetry
RUN poetry config virtualenvs.in-project true
RUN poetry install --no-interaction --no-root

# Set up the virtual environment path correctly for the appuser
ENV PATH="/app/.venv/bin:${PATH}"

# Expose the application port
EXPOSE $PORT

# Start the FastAPI application using Uvicorn
CMD ["uvicorn", "src.main:app", "--log-level", "info", "--host", "0.0.0.0" , "--port", "8080"]
40 changes: 40 additions & 0 deletions 2025/efficient-python-dockerfile/Dockerfile.03_imgtag
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM python:3.13-slim-bookworm

RUN apt-get update && apt-get install -y \
build-essential \
curl \
ca-certificates && \
apt-get clean

RUN curl -sSL https://install.python-poetry.org | python3 -

# Set up the Poetry environment path correctly
ENV PATH="/root/.local/bin:${PATH}"

# Set environment variables for database and secrets
ENV DB_PASSWORD=${DB_PASSWORD}
ENV DB_USER=${DB_USER}
ENV DB_NAME=${DB_NAME}
ENV DB_HOST=${DB_HOST}
ENV ACCESS_TOKEN_SECRET_KEY=${ACCESS_TOKEN_SECRET_KEY}

WORKDIR /app

# Copy the application files into the container
COPY . .

# Install dependencies with Poetry
RUN poetry config virtualenvs.in-project true
RUN poetry install --no-interaction --no-root

# Set up the virtual environment path correctly for the appuser
ENV PATH="/app/.venv/bin:{$PATH}"

# Expose the specified port for FastAPI
EXPOSE $PORT

# Start the application with Uvicorn in production mode, using environment variable references
CMD ["uvicorn", "src.main:app", "--log-level", "info", "--host", "0.0.0.0" , "--port", "8080"]



38 changes: 38 additions & 0 deletions 2025/efficient-python-dockerfile/Dockerfile.04_limitdeps
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM python:3.13-slim-bookworm

RUN apt-get update && apt-get install --no-install-recommends -y \
build-essential \
curl \
ca-certificates && \
apt-get clean

RUN curl -sSL https://install.python-poetry.org | python3 -

# Set up the Poetry environment path correctly
ENV PATH="/root/.local/bin:${PATH}"

# Set environment variables for database and secrets
ENV DB_PASSWORD=${DB_PASSWORD}
ENV DB_USER=${DB_USER}
ENV DB_NAME=${DB_NAME}
ENV DB_HOST=${DB_HOST}
ENV ACCESS_TOKEN_SECRET_KEY=${ACCESS_TOKEN_SECRET_KEY}

WORKDIR /app

# Copy the application files into the container
COPY . .

# Install dependencies with Poetry
RUN poetry config virtualenvs.in-project true
RUN poetry install --no-interaction --no-root

ENV PATH="/app/.venv/bin:{$PATH}"

# Expose the specified port for FastAPI
EXPOSE $PORT

# Start the application with Uvicorn in production mode, using environment variable references
CMD ["uvicorn", "src.main:app", "--log-level", "info", "--host", "0.0.0.0" , "--port", "8080"]


37 changes: 37 additions & 0 deletions 2025/efficient-python-dockerfile/Dockerfile.05_cleandeps
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM python:3.13-slim-bookworm

RUN apt-get update && apt-get install --no-install-recommends -y \
build-essential \
curl \
ca-certificates && \
apt-get clean && rm -rf /var/lib/apt/lists/*


RUN curl -sSL https://install.python-poetry.org | python3 -

# Set up the Poetry environment path correctly
ENV PATH="/root/.local/bin:${PATH}"

# Set environment variables for database and secrets
ENV DB_PASSWORD=${DB_PASSWORD}
ENV DB_USER=${DB_USER}
ENV DB_NAME=${DB_NAME}
ENV DB_HOST=${DB_HOST}
ENV ACCESS_TOKEN_SECRET_KEY=${ACCESS_TOKEN_SECRET_KEY}

WORKDIR /app

# Copy the application files into the container
COPY . .

# Install dependencies with Poetry
RUN poetry config virtualenvs.in-project true
RUN poetry install --no-interaction --no-root

ENV PATH="/app/.venv/bin:{$PATH}"

# Expose the specified port for FastAPI
EXPOSE $PORT

# Start the application with Uvicorn in production mode, using environment variable references
CMD ["uvicorn", "src.main:app", "--log-level", "info", "--host", "0.0.0.0" , "--port", "8080"]
33 changes: 33 additions & 0 deletions 2025/efficient-python-dockerfile/Dockerfile.06_uv
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FROM python:3.13-slim-bookworm

RUN apt-get update && apt-get install --no-install-recommends -y \
build-essential && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# Download the latest installer, install it and then remove it
ADD https://astral.sh/uv/install.sh /install.sh
RUN chmod -R 755 /install.sh && /install.sh && rm /install.sh

# Set environment variables for database and secrets
ENV DB_PASSWORD=${DB_PASSWORD}
ENV DB_USER=${DB_USER}
ENV DB_NAME=${DB_NAME}
ENV DB_HOST=${DB_HOST}
ENV ACCESS_TOKEN_SECRET_KEY=${ACCESS_TOKEN_SECRET_KEY}

# Set up the UV environment path correctly
ENV PATH="/root/.local/bin:${PATH}"

WORKDIR /app

COPY . .

RUN uv sync

ENV PATH="/app/.venv/bin:{$PATH}"

# Expose the specified port for FastAPI
EXPOSE $PORT

# Start the application with Uvicorn in production mode, using environment variable references
CMD ["uvicorn", "src.main:app", "--log-level", "info", "--host", "0.0.0.0" , "--port", "8080"]
43 changes: 43 additions & 0 deletions 2025/efficient-python-dockerfile/Dockerfile.07_multi
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## ------------------------------- Builder Stage ------------------------------ ##
FROM python:3.13-bookworm AS builder

RUN apt-get update && apt-get install --no-install-recommends -y \
build-essential && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# Download the latest installer, install it and then remove it
ADD https://astral.sh/uv/install.sh /install.sh
RUN chmod -R 655 /install.sh && /install.sh && rm /install.sh

# Set up the UV environment path correctly
ENV PATH="/root/.local/bin:${PATH}"

WORKDIR /app

COPY ./pyproject.toml .

RUN uv sync

## ------------------------------- Production Stage ------------------------------ ##
FROM python:3.13-slim-bookworm AS production

# Set environment variables for DB and access token
ENV DB_PASSWORD=${DB_PASSWORD}
ENV DB_USER=${DB_USER}
ENV DB_NAME=${DB_NAME}
ENV DB_HOST=${DB_HOST}
ENV ACCESS_TOKEN_SECRET_KEY=${ACCESS_TOKEN_SECRET_KEY}

WORKDIR /app

COPY . .
COPY --from=builder /app/.venv .venv

# Set up environment variables for production
ENV PATH="/app/.venv/bin:{$PATH}"

# Expose the specified port for FastAPI
EXPOSE $PORT

# Start the application with Uvicorn in production mode, using environment variable references
CMD ["uvicorn", "src.main:app", "--log-level", "info", "--host", "0.0.0.0" , "--port", "8080"]
43 changes: 43 additions & 0 deletions 2025/efficient-python-dockerfile/Dockerfile.08_bettercopy
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## ------------------------------- Builder Stage ------------------------------ ##
FROM python:3.13-bookworm AS builder

RUN apt-get update && apt-get install --no-install-recommends -y \
build-essential && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# Download the latest installer, install it and then remove it
ADD https://astral.sh/uv/install.sh /install.sh
RUN chmod -R 655 /install.sh && /install.sh && rm /install.sh

# Set up the UV environment path correctly
ENV PATH="/root/.local/bin:${PATH}"

WORKDIR /app

COPY ./pyproject.toml .

RUN uv sync

## ------------------------------- Production Stage ------------------------------ ##
FROM python:3.13-slim-bookworm AS production

# Set environment variables for DB and access token
ENV DB_PASSWORD=${DB_PASSWORD}
ENV DB_USER=${DB_USER}
ENV DB_NAME=${DB_NAME}
ENV DB_HOST=${DB_HOST}
ENV ACCESS_TOKEN_SECRET_KEY=${ACCESS_TOKEN_SECRET_KEY}

WORKDIR /app

COPY /src src
COPY --from=builder /app/.venv .venv

# Set up environment variables for production
ENV PATH="/app/.venv/bin:{$PATH}"

# Expose the specified port for FastAPI
EXPOSE $PORT

# Start the application with Uvicorn in production mode, using environment variable references
CMD ["uvicorn", "src.main:app", "--log-level", "info", "--host", "0.0.0.0" , "--port", "8080"]
Loading