Skip to content

Commit 47a9a34

Browse files
committed
Don't install poetry into the built image
1 parent 4745cf4 commit 47a9a34

File tree

4 files changed

+576
-79
lines changed

4 files changed

+576
-79
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ repos:
55
- id: check-merge-conflict
66
- id: check-toml
77
- id: check-yaml
8+
args: [--unsafe] # Needed for << lines in docker-compose.yml
89
- id: end-of-file-fixer
910
- id: trailing-whitespace
1011
args: [--markdown-linebreak-ext=md]

Dockerfile

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,23 @@
1-
FROM --platform=linux/amd64 python:3.9-slim as base
1+
FROM --platform=linux/amd64 python:3.11-slim as base
22

3-
# Set pip to have no saved cache
4-
ENV PIP_NO_CACHE_DIR=false \
5-
POETRY_VIRTUALENVS_IN_PROJECT=true \
6-
PYTHONUNBUFFERED=1 \
7-
PIP_DISABLE_PIP_VERSION_CHECK=on \
8-
POETRY_HOME="/opt/poetry" \
9-
INSTALL_DIR="/opt/dependencies" \
10-
APP_DIR="/app" \
11-
POETRY_NO_INTERACTION=1
12-
13-
ENV PATH="$POETRY_HOME/bin:$INSTALL_DIR/.venv/bin:$PATH"
3+
# Define Git SHA build argument for sentry
4+
ARG git_sha="development"
5+
ENV GIT_SHA=$git_sha
146

157
RUN groupadd -g 61000 codejam_management \
16-
&& useradd -g 61000 -l -r -u 61000 codejam_management
17-
18-
FROM base as builder
19-
RUN apt-get update \
20-
&& apt-get -y upgrade \
21-
&& apt-get install --no-install-recommends -y \
22-
curl \
23-
&& apt-get clean && rm -rf /var/lib/apt/lists/*
24-
25-
RUN curl -sSL https://install.python-poetry.org | python -
26-
27-
WORKDIR $INSTALL_DIR
28-
COPY "pyproject.toml" "poetry.lock" ./
29-
RUN poetry install --no-dev
30-
8+
&& useradd -g 61000 -l -r -u 61000 codejam_management
319

32-
FROM base as development
33-
34-
# Create the working directory
35-
WORKDIR $APP_DIR
36-
COPY --from=builder $INSTALL_DIR $INSTALL_DIR
37-
38-
# Copy the source code in last to optimize rebuilding the image
39-
COPY . .
10+
# Install project dependencies
11+
WORKDIR /app
12+
COPY main-requirements.txt ./
13+
RUN pip install -r main-requirements.txt
4014

15+
EXPOSE 8000
4116
USER codejam_management
42-
# Run a single uvicorn worker
43-
# Multiple workers are managed by kubernetes, rather than something like gunicorn
44-
CMD ["sh", "-c", "alembic upgrade head && uvicorn api.main:app --host 0.0.0.0 --port 8000 --reload"]
45-
17+
# Pull the uvicorn_extra build arg and ave it as an env var.
18+
# The CMD instruction is ran at execution time, so it also needs to be an env var, so that it is available at that time.
19+
ARG uvicorn_extras=""
20+
ENV uvicorn_extras=$uvicorn_extras
4621

47-
FROM base as production
48-
COPY --from=builder $INSTALL_DIR $INSTALL_DIR
49-
WORKDIR $APP_DIR
50-
COPY . .
51-
RUN python -m compileall api/
52-
53-
USER codejam_management
54-
CMD ["sh", "-c", "alembic upgrade head && uvicorn api.main:app --host 0.0.0.0 --port 8000"]
22+
ENTRYPOINT ["/bin/bash", "-c"]
23+
CMD ["alembic upgrade head && uvicorn api.main:app --host 0.0.0.0 --port 80 $uvicorn_extras"]

docker-compose.yml

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,50 @@
1-
version: "3.7"
1+
x-logging: &logging
2+
logging:
3+
driver: "json-file"
4+
options:
5+
max-file: "5"
6+
max-size: "10m"
7+
8+
x-restart-policy: &restart_policy
9+
restart: unless-stopped
10+
11+
x-secure-containers: &secure_containers
12+
privileged: false
13+
read_only: true # Prod uses a read-only fs, override this locally if it helps with debugging
14+
user: "65534" # Prod uses a non-root user, override this locally if it helps with debugging
215

316
services:
4-
postgres:
5-
image: postgres:13-alpine
6-
ports:
7-
- "127.0.0.1:7777:5432"
8-
environment:
9-
POSTGRES_DB: codejam_management
10-
POSTGRES_PASSWORD: codejam_management
11-
POSTGRES_USER: codejam_management
12-
healthcheck:
13-
test: [ "CMD-SHELL", "pg_isready -U codejam_management" ]
14-
interval: 2s
15-
timeout: 1s
16-
retries: 5
17-
api:
18-
build:
19-
context: .
20-
dockerfile: Dockerfile
21-
target: development
22-
restart: always
23-
volumes:
24-
- ./alembic/versions:/app/alembic/versions
25-
- .:/app:ro
26-
depends_on:
27-
postgres:
28-
condition: service_healthy
29-
read_only: true # Prod uses a read-only fs, override this locally if it helps with debugging
30-
user: "1000" # Prod uses a non-root user, override this locally if it helps with debugging
31-
environment:
32-
DATABASE_URL: postgresql+asyncpg://codejam_management:codejam_management@postgres:5432/codejam_management
33-
ports:
34-
- 5000:8000
17+
postgres:
18+
<< : *logging
19+
<< : *restart_policy
20+
image: postgres:13-alpine
21+
ports:
22+
- "127.0.0.1:7777:5432"
23+
environment:
24+
POSTGRES_DB: codejam_management
25+
POSTGRES_PASSWORD: codejam_management
26+
POSTGRES_USER: codejam_management
27+
healthcheck:
28+
test: [ "CMD-SHELL", "pg_isready -U codejam_management" ]
29+
interval: 2s
30+
timeout: 1s
31+
retries: 5
32+
33+
api:
34+
<< : *logging
35+
<< : *restart_policy
36+
<< : *secure_containers
37+
build:
38+
context: .
39+
args:
40+
- uvicorn_extras=--reload
41+
volumes:
42+
- ./alembic/versions:/app/alembic/versions
43+
- .:/app:ro
44+
depends_on:
45+
postgres:
46+
condition: service_healthy
47+
environment:
48+
DATABASE_URL: postgresql+asyncpg://codejam_management:codejam_management@postgres:5432/codejam_management
49+
ports:
50+
- 5000:8000

0 commit comments

Comments
 (0)