Skip to content

Commit f13fa0d

Browse files
committed
optimize docker build process
1 parent 9014fff commit f13fa0d

3 files changed

Lines changed: 79 additions & 45 deletions

File tree

.dockerignore

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,56 @@
1-
*.env
2-
git/
1+
# Git
2+
.git
3+
.github
4+
.gitignore
35

4-
build
5-
dist
6+
# Python
7+
.venv
8+
venv
9+
__pycache__
10+
*.pyc
11+
*.pyo
12+
*.pyd
13+
.Python
14+
*.so
15+
*.egg
616
*.egg-info
7-
*.egg/
17+
dist
18+
build
19+
.pytest_cache
20+
.coverage
21+
htmlcov
22+
.tox
23+
.mypy_cache
24+
.ruff_cache
25+
26+
# Environment files
27+
.env
28+
.env.*
29+
!.env.example
30+
31+
# IDE
32+
.vscode
33+
.idea
834
*.swp
35+
*.swo
36+
*~
937

10-
.tox
11-
.coverage
12-
html/*
13-
**/__pycache__
14-
**/*.pyc
15-
16-
# Development files - should not be in production
17-
.dev/
18-
src/.dev/
19-
src/.dev
20-
**/.dev/
21-
**/.dev
22-
*.sqlite3
23-
*.db
24-
db.sqlite3
25-
src/db.sqlite3
26-
**/db.sqlite3
27-
28-
# Test artifacts
29-
.pytest_cache/
30-
src/.pytest_cache/
31-
**/.pytest_cache/
32-
.coverage
33-
htmlcov/
38+
# Logs
39+
*.log
40+
41+
# Documentation
42+
*.md
43+
!README.md
44+
docs/
45+
46+
# Testing
47+
tests/
48+
*.test
49+
50+
# macOS
51+
.DS_Store
52+
53+
# Temporary files
54+
tmp/
55+
temp/
56+
*.tmp

.github/workflows/ci.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ jobs:
172172

173173
docker-build-push:
174174
name: Build and Push Docker Image
175-
runs-on: ubuntu-latest
175+
runs-on: ubuntu-24.04-arm64
176176
# Run on push to main (build+push) and on PRs (build only)
177177
if: github.event_name == 'push' || github.event_name == 'pull_request'
178178
# For main/PR, wait for CI checks to pass
@@ -260,7 +260,7 @@ jobs:
260260
uses: aws-actions/amazon-ecr-login@v2
261261

262262
- name: Build and push Docker image
263-
uses: docker/build-push-action@v5
263+
uses: docker/build-push-action@v6
264264
with:
265265
context: .
266266
target: runtime
@@ -269,8 +269,14 @@ jobs:
269269
tags: |
270270
${{ steps.docker-tag.outputs.image }}
271271
provenance: false
272-
cache-from: type=gha
273-
cache-to: type=gha,mode=max
272+
cache-from: |
273+
type=gha,scope=arm64-${{ github.head_ref || github.ref_name }}
274+
type=gha,scope=arm64-main
275+
type=registry,ref=633607774026.dkr.ecr.us-east-2.amazonaws.com/back-end-cache:${{ github.head_ref || github.ref_name }}
276+
type=registry,ref=633607774026.dkr.ecr.us-east-2.amazonaws.com/back-end-cache:main
277+
cache-to: |
278+
type=gha,mode=max,scope=arm64-${{ github.head_ref || github.ref_name }}
279+
type=registry,ref=633607774026.dkr.ecr.us-east-2.amazonaws.com/back-end-cache:${{ github.head_ref || github.ref_name }},mode=max
274280
275281
- name: Output image URI
276282
if: steps.can-push.outputs.push == 'true'

Dockerfile

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
FROM python:3.12-slim AS builder
77

88
# Install build dependencies required for compiling Python packages
9-
RUN apt-get update && apt-get install -y --no-install-recommends \
9+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
10+
--mount=type=cache,target=/var/lib/apt,sharing=locked \
11+
rm -f /etc/apt/apt.conf.d/docker-clean && \
12+
apt-get update && apt-get install -y --no-install-recommends \
1013
build-essential \
1114
libpq-dev \
12-
curl \
13-
&& rm -rf /var/lib/apt/lists/*
15+
curl
1416

1517
# Install Poetry
1618
ENV POETRY_VERSION=2.3.0 \
@@ -20,7 +22,8 @@ ENV POETRY_VERSION=2.3.0 \
2022
POETRY_VIRTUALENVS_CREATE=1 \
2123
POETRY_CACHE_DIR=/tmp/poetry_cache
2224

23-
RUN curl -sSL https://install.python-poetry.org | python3 - && \
25+
RUN --mount=type=cache,target=/root/.cache \
26+
curl -sSL https://install.python-poetry.org | python3 - && \
2427
ln -s /opt/poetry/bin/poetry /usr/local/bin/poetry
2528

2629
WORKDIR /app
@@ -52,13 +55,14 @@ LABEL org.opencontainers.image.description="Operation Code Backend - Development
5255
LABEL org.opencontainers.image.licenses="MIT"
5356

5457
# Install runtime dependencies
55-
RUN apt-get update && apt-get install -y --no-install-recommends \
58+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
59+
--mount=type=cache,target=/var/lib/apt,sharing=locked \
60+
rm -f /etc/apt/apt.conf.d/docker-clean && \
61+
apt-get update && apt-get install -y --no-install-recommends \
5662
libpq5 \
5763
curl \
5864
wget \
59-
&& apt-get upgrade -y \
60-
&& rm -rf /var/lib/apt/lists/* \
61-
&& apt-get clean
65+
&& apt-get upgrade -y
6266

6367
# Create non-root user for security
6468
RUN groupadd -r appuser && \
@@ -100,13 +104,14 @@ LABEL org.opencontainers.image.description="Operation Code Backend - Django API"
100104
LABEL org.opencontainers.image.licenses="MIT"
101105

102106
# Install only runtime dependencies (no build tools)
103-
RUN apt-get update && apt-get install -y --no-install-recommends \
107+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
108+
--mount=type=cache,target=/var/lib/apt,sharing=locked \
109+
rm -f /etc/apt/apt.conf.d/docker-clean && \
110+
apt-get update && apt-get install -y --no-install-recommends \
104111
libpq5 \
105112
curl \
106113
wget \
107-
&& apt-get upgrade -y \
108-
&& rm -rf /var/lib/apt/lists/* \
109-
&& apt-get clean
114+
&& apt-get upgrade -y
110115

111116
# Create non-root user for security
112117
RUN groupadd -r appuser && \

0 commit comments

Comments
 (0)