Skip to content

Commit 98fb351

Browse files
committed
feat: update Docker setup with new base image, enhance build workflow, and improve error logging in main application
1 parent 696363c commit 98fb351

File tree

5 files changed

+49
-91
lines changed

5 files changed

+49
-91
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.venv

.github/workflows/build.yaml

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,27 @@
1-
name: Docker Publish
1+
name: Docker Build
22

33
on:
44
push:
5-
branches: ["main"]
6-
# Publish semver tags as releases.
7-
tags: ["v*.*.*"]
8-
9-
env:
10-
# Use docker.io for Docker Hub if empty
11-
REGISTRY: ghcr.io
12-
# github.repository as <account>/<repo>
13-
IMAGE_NAME: ${{ github.repository }}
5+
branches:
6+
- main
7+
- develop
8+
- "feature/**"
9+
- "hotfix/**"
10+
tags:
11+
- "v*.*.*"
12+
pull_request:
13+
branches:
14+
- main
15+
- develop
1416

1517
jobs:
16-
build:
18+
docker:
1719
runs-on: ubuntu-latest
18-
permissions:
19-
contents: read
20-
packages: write
21-
2220
steps:
23-
- name: Checkout repository
24-
uses: actions/checkout@v3
25-
26-
# Install the cosign tool except on PR
27-
28-
# Set up BuildKit Docker container builder to be able to build
29-
# multi-platform images and export cache
30-
# https://github.com/docker/setup-buildx-action
31-
- name: Set up Docker Buildx
32-
uses: docker/setup-buildx-action@v3 # v3.0.0
33-
34-
# Login against a Docker registry except on PR
35-
# https://github.com/docker/login-action
36-
- name: Log into registry ${{ env.REGISTRY }}
37-
if: github.event_name != 'pull_request'
38-
uses: docker/login-action@v3 # v3.0.0
21+
- uses: pnstack/actions/docker-publish@main
3922
with:
40-
registry: ${{ env.REGISTRY }}
41-
username: ${{ github.actor }}
42-
password: ${{ secrets.GITHUB_TOKEN }}
43-
44-
# Extract metadata (tags, labels) for Docker
45-
# https://github.com/docker/metadata-action
46-
- name: Extract Docker metadata
47-
id: meta
48-
uses: docker/metadata-action@v5 # v5.0.0
49-
with:
50-
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
51-
52-
# Build and push Docker image with Buildx (don't push on PR)
53-
# https://github.com/docker/build-push-action
54-
- name: Build and push Docker image
55-
id: build-and-push
56-
uses: docker/build-push-action@v5 # v5.0.0
57-
with:
58-
context: .
5923
platforms: linux/amd64,linux/arm64
60-
push: ${{ github.event_name != 'pull_request' }}
61-
tags: ${{ steps.meta.outputs.tags }}
62-
labels: ${{ steps.meta.outputs.labels }}
63-
cache-from: type=gha
64-
cache-to: type=gha,mode=max
24+
dockerfile: Dockerfile
25+
context: .
26+
push_enabled: true
27+
registry: ghcr.io

Dockerfile

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,38 @@
1-
FROM python:3.11-slim AS base
1+
# Use a Python image with uv pre-installed
2+
FROM ghcr.io/astral-sh/uv:python3.11-bookworm
23

3-
# Set Python environment variables
4-
ENV PYTHONDONTWRITEBYTECODE=1 \
5-
PYTHONUNBUFFERED=1 \
6-
PYTHONFAULTHANDLER=1 \
7-
LANG=C.UTF-8 \
8-
LC_ALL=C.UTF-8
9-
10-
# Install basic dependencies
11-
RUN apt-get update && \
12-
apt-get install -y --no-install-recommends \
13-
gcc \
14-
&& rm -rf /var/lib/apt/lists/*
15-
16-
# Upgrade pip
17-
RUN pip install --no-cache-dir --upgrade pip
4+
# Install the project into `/app`
5+
WORKDIR /app
186

19-
FROM base AS builder
7+
# Enable bytecode compilation
8+
ENV UV_COMPILE_BYTECODE=1
209

21-
# Create and activate virtual environment
22-
RUN python -m venv /.venv
23-
ENV PATH="/.venv/bin:$PATH"
10+
# Copy from the cache instead of linking since it's a mounted volume
11+
ENV UV_LINK_MODE=copy
2412

25-
# Install dependencies
26-
COPY pyproject.toml .
27-
RUN pip install --no-cache-dir .
13+
# Install the project's dependencies using the lockfile and settings
14+
RUN --mount=type=cache,target=/root/.cache/uv \
15+
--mount=type=bind,source=uv.lock,target=uv.lock \
16+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
17+
uv sync --frozen --no-install-project --no-dev
2818

29-
FROM base as runtime
19+
# Then, add the rest of the project source code and install it
20+
# Installing separately from its dependencies allows optimal layer caching
21+
ADD . /app
22+
RUN --mount=type=cache,target=/root/.cache/uv \
23+
uv sync --frozen --no-dev
3024

31-
# Set working directory
32-
WORKDIR /app
25+
# Place executables in the environment at the front of the path
26+
ENV PATH="/app/.venv/bin:$PATH"
3327

34-
# Copy virtual environment from builder
35-
COPY --from=builder /.venv /.venv
36-
ENV PATH="/.venv/bin:$PATH"
28+
# Reset the entrypoint, don't invoke `uv`
29+
ENTRYPOINT []
3730

38-
# Copy application code
39-
COPY . .
31+
# Run setup.py
4032

41-
# Expose port (adjust if needed)
42-
EXPOSE 8000
33+
RUN python setup.py
4334

44-
# Run the application
45-
CMD ["python", "main.py"]
35+
# Run the FastAPI application by default
36+
# Uses `fastapi dev` to enable hot-reloading when the `watch` sync occurs
37+
# Uses `--host 0.0.0.0` to allow access from outside the container
38+
CMD ["python", "main.py"]

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def main() -> None:
1313
import src.modules.api
1414

1515
except Exception as e:
16-
print(f"Application failed to start: {e}", exc_info=True)
16+
logging.error(f"Application failed to start: {e}", exc_info=True)
1717
raise
1818

1919

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Setup.py")

0 commit comments

Comments
 (0)