Skip to content

Commit 4a92b9d

Browse files
committed
docker image size reduced by 50%
1 parent 565ada2 commit 4a92b9d

6 files changed

Lines changed: 555 additions & 8 deletions

File tree

.github/workflows/ci.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: CI/CD Pipeline
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
paths:
7+
- 'microservices/**'
8+
pull_request:
9+
branches: [ "main" ]
10+
paths:
11+
- 'microservices/**'
12+
13+
jobs:
14+
build-and-test:
15+
runs-on: ubuntu-latest
16+
defaults:
17+
run:
18+
working-directory: ./microservices/fastapi/monolith
19+
20+
steps:
21+
- uses: actions/checkout@v3
22+
23+
- name: Set up Python 3.10
24+
uses: actions/setup-python@v4
25+
with:
26+
python-version: "3.10"
27+
28+
- name: Install Poetry
29+
run: |
30+
pip install poetry
31+
32+
- name: Install dependencies
33+
run: |
34+
poetry install
35+
36+
- name: Run Tests
37+
# For now, we just check if the code is syntactically correct
38+
run: |
39+
poetry run python -m compileall app/
40+
41+
- name: Build Docker Image
42+
run: |
43+
docker build -t monolith-service-alpine .

.gitignore

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,50 @@
1-
fastapi_service/venv
2-
microservices/fastapi/monolith/myvenv
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
microservices/fastapi/monolith/app/__pycache__
8+
microservices/fastapi/monolith/app/services/__pycache__
9+
microservices/fastapi/monolith/app/routers/__pycache__
10+
11+
12+
13+
microservices/fastapi/monolith/app/__pycache__
14+
15+
# Virtual Environments
16+
venv/
17+
env/
18+
ENV/
19+
.venv
20+
myvenv/
21+
22+
microservices/fastapi/monolith/myvenv
23+
24+
# IDEs
25+
.vscode/
26+
.idea/
27+
*.swp
28+
*.swo
29+
*~
30+
31+
# Environment variables
32+
.env
33+
.env.local
34+
35+
# OS
36+
.DS_Store
37+
Thumbs.db
38+
39+
# Poetry (do NOT ignore poetry.lock!)
40+
# poetry.lock should be committed
41+
42+
# Testing
43+
.pytest_cache/
44+
.coverage
45+
htmlcov/
46+
47+
# Build artifacts
48+
dist/
49+
build/
50+
*.egg-info/
Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,72 @@
1-
FROM python:3.10-slim
1+
# Stage 1: Builder
2+
FROM python:3.10-alpine as builder
3+
4+
# Set environment variables
5+
ENV PYTHONDONTWRITEBYTECODE=1 \
6+
PYTHONUNBUFFERED=1
27

38
WORKDIR /code
49

5-
COPY ./requirements.txt /code/requirements.txt
10+
# Install system dependencies required for building python packages
11+
# libffi-dev and build-base are often needed for python wheels on alpine
12+
RUN apk add --no-cache libffi-dev build-base
13+
14+
# Install poetry and export plugin
15+
RUN pip install poetry poetry-plugin-export
16+
17+
# Copy poetry configuration
18+
COPY pyproject.toml poetry.lock .
19+
20+
# Generate lock file and export requirements.txt
21+
RUN poetry lock && \
22+
poetry export -f requirements.txt --output requirements.txt --without-hashes
23+
24+
# Install python dependencies
25+
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /code/wheels -r requirements.txt
626

7-
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
27+
# Stage 2: Runner
28+
FROM python:3.10-alpine as runner
29+
30+
# Metadata
31+
LABEL maintainer="Monolith Team" \
32+
description="Monolithic FastAPI Service" \
33+
version="1.0"
34+
35+
ENV PYTHONDONTWRITEBYTECODE=1 \
36+
PYTHONUNBUFFERED=1
37+
38+
WORKDIR /code
839

40+
# Create a non-root user for security
41+
# Alpine uses adduser -D (no password)
42+
RUN addgroup -S appuser && adduser -S appuser -G appuser
43+
44+
# Install system dependencies (curl for healthcheck)
45+
RUN apk add --no-cache curl
46+
47+
# Copy wheels from builder stage
48+
COPY --from=builder /code/wheels /code/wheels
49+
COPY --from=builder /code/requirements.txt .
50+
51+
# Install dependencies from wheels
52+
RUN pip install --no-cache-dir --no-index --find-links=/code/wheels -r requirements.txt && \
53+
rm -rf /code/wheels
54+
55+
# Copy application code
956
COPY ./app /code/app
1057

58+
# Change ownership to non-root user
59+
RUN chown -R appuser:appuser /code
60+
61+
# Switch to non-root user
62+
USER appuser
63+
64+
# Expose the port
65+
EXPOSE 8000
66+
67+
# Healthcheck to ensure the service is running
68+
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
69+
CMD curl -f http://localhost:8000/ || exit 1
70+
71+
# Command to run the application
1172
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

0 commit comments

Comments
 (0)