Skip to content

Commit 93568a6

Browse files
committed
Update Docker / k8s to use uv
Context: - We needed to explicilty include psycopg-binary here - Rest of the changes are just replacements of python -> uv run
1 parent f928e0b commit 93568a6

8 files changed

Lines changed: 61 additions & 19 deletions

File tree

.github/workflows/docker-k8s.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
name: Container
2+
# These workflows are intended to check that the Docker and Kubernetes
3+
# configurations work as expected in a local development environment.
24

35
on:
46
pull_request:

Makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ docker-up: # (Docker) Create services/volumes/networks
4747
docker compose up -d
4848

4949
docker-migrate: # (Docker) Run any pending migrations
50-
docker compose exec -T app python manage.py migrate ${docker_config}
50+
docker compose --verbose exec -T app uv run manage.py migrate ${docker_config}
5151

5252
docker-build-db: # (Docker) Build the database
53-
docker compose exec -T app sh -c 'echo "from data.v2.build import build_all; build_all()" | python manage.py shell ${docker_config}'
53+
docker compose exec -T app sh -c 'echo "from data.v2.build import build_all; build_all()" | uv run manage.py shell ${docker_config}'
5454

5555
docker-make-migrations: # (Docker) Create migrations files if schema has changed
56-
docker compose exec -T app sh -c 'python manage.py makemigrations ${docker_config}'
56+
docker compose exec -T app sh -c 'uv run manage.py makemigrations ${docker_config}'
5757

5858
docker-flush-db: # (Docker) Removes all the data present in the database but preserves tables and migrations
59-
docker compose exec -T app sh -c 'python manage.py flush --no-input ${docker_config}'
59+
docker compose exec -T app sh -c 'uv run manage.py flush --no-input ${docker_config}'
6060

6161
docker-destroy-db: # (Docker) Removes the volume where the database is installed on, alongside to the container itself
6262
docker rm -f pokeapi_db_1
@@ -72,7 +72,7 @@ docker-down: # (Docker) Stop and removes containers and networks
7272
docker compose down
7373

7474
docker-test: # (Docker) Run tests
75-
docker compose exec -T app python manage.py test ${local_config}
75+
docker compose exec -T app uv run manage.py test ${local_config}
7676

7777
docker-prod:
7878
docker compose -f docker-compose.yml -f docker-compose.override.yml -f Resources/compose/docker-compose-prod-graphql.yml up -d
@@ -130,10 +130,10 @@ kustomize-ga-apply: # (Kustomize) Run kubectl apply -k on the connected k8s clu
130130
kubectl apply -k Resources/k8s/kustomize/ga/
131131

132132
k8s-migrate: # (k8s) Run any pending migrations
133-
kubectl exec --namespace pokeapi deployment/pokeapi -- python manage.py migrate ${docker_config}
133+
kubectl exec --namespace pokeapi deployment/pokeapi -- uv run manage.py migrate ${docker_config}
134134

135135
k8s-build-db: # (k8s) Build the database
136-
kubectl exec --namespace pokeapi deployment/pokeapi -- sh -c 'echo "from data.v2.build import build_all; build_all()" | python manage.py shell ${docker_config}'
136+
kubectl exec --namespace pokeapi deployment/pokeapi -- sh -c 'echo "from data.v2.build import build_all; build_all()" | uv run manage.py shell ${docker_config}'
137137

138138
k8s-delete: # (k8s) Delete pokeapi namespace
139139
kubectl delete namespace pokeapi

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ If you don't have `make` on your machine you can use the following commands
9999
100100
```sh
101101
docker compose up -d
102-
docker compose exec -T app python manage.py migrate --settings=config.docker-compose
103-
docker compose exec -T app sh -c 'echo "from data.v2.build import build_all; build_all()" | python manage.py shell --settings=config.docker-compose'
102+
docker compose exec -T app uv run manage.py migrate --settings=config.docker-compose
103+
docker compose exec -T app sh -c 'echo "from data.v2.build import build_all; build_all()" | uv run manage.py shell --settings=config.docker-compose'
104104
```
105105
106106
Browse [localhost/api/v2/](http://localhost/api/v2/) or [localhost/api/v2/pokemon/bulbasaur/](http://localhost/api/v2/pokemon/bulbasaur/) on port `80`.
@@ -151,8 +151,8 @@ Configure `kubectl` to point to a cluster and then run the following commands to
151151
kubectl apply -k Resources/k8s/kustomize/base/
152152
kubectl config set-context --current --namespace pokeapi # (Optional) Set pokeapi ns as the working ns
153153
# Wait for the cluster to spin up
154-
kubectl exec --namespace pokeapi deployment/pokeapi -- python manage.py migrate --settings=config.docker-compose # Migrate the DB
155-
kubectl exec --namespace pokeapi deployment/pokeapi -- sh -c 'echo "from data.v2.build import build_all; build_all()" | python manage.py shell --settings=config.docker-compose' # Build the db
154+
kubectl exec --namespace pokeapi deployment/pokeapi -- uv run manage.py migrate --settings=config.docker-compose # Migrate the DB
155+
kubectl exec --namespace pokeapi deployment/pokeapi -- sh -c 'echo "from data.v2.build import build_all; build_all()" | uv run manage.py shell --settings=config.docker-compose' # Build the db
156156
kubectl wait --namespace pokeapi --timeout=120s --for=condition=complete job/load-graphql # Wait for Graphql configuration job to finish
157157
```
158158

Resources/docker/app/Dockerfile

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ ENV PYTHONUNBUFFERED=1
55
RUN mkdir /code
66
WORKDIR /code
77

8-
ADD requirements.txt /code/
8+
# Install UV
9+
RUN apk add --no-cache curl
10+
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
11+
12+
# Add UV to PATH for this stage
13+
ENV PATH="/root/.local/bin:$PATH"
14+
15+
# Copy dependency files
16+
ADD pyproject.toml uv.lock* /code/
17+
18+
# Install dependencies
919
RUN apk add --no-cache postgresql-libs libstdc++
1020
RUN apk add --no-cache --virtual .build-deps gcc g++ musl-dev \
1121
postgresql-dev binutils rust cargo && \
12-
python3 -m pip install -r requirements.txt --no-cache-dir
22+
uv sync --frozen
1323

1424
FROM python:3.13.7-alpine
1525

@@ -19,15 +29,23 @@ ENV DJANGO_SETTINGS_MODULE='config.docker-compose'
1929
RUN mkdir /code
2030
WORKDIR /code
2131

22-
COPY --from=builder /usr/local/lib/python3.13/site-packages /usr/local/lib/python3.13/site-packages
23-
COPY --from=builder /usr/local/bin /usr/local/bin
32+
# Copy UV and virtual environment from builder
33+
COPY --from=builder /root/.local /root/.local
34+
COPY --from=builder /code/.venv /code/.venv
35+
36+
# Add UV to PATH
37+
ENV PATH="/root/.local/bin:$PATH"
38+
39+
# Install runtime dependencies
40+
RUN apk add --no-cache postgresql-libs libstdc++
2441

42+
# Copy application code
2543
ADD . /code/
2644

2745
RUN addgroup -g 1000 -S pokeapi && \
2846
adduser -u 1000 -S pokeapi -G pokeapi
2947
USER pokeapi
3048

31-
CMD ["gunicorn", "config.wsgi:application", "-c", "gunicorn.conf.py"]
49+
CMD ["uv", "run", "gunicorn", "config.wsgi:application", "-c", "gunicorn.conf.py"]
3250

3351
EXPOSE 80

Resources/docker/app/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ Pokémon data isn't automatically present in this image. All Pokémon data is pe
4646
When the container is up and running, run the following shell commands:
4747

4848
```sh
49-
docker exec pokeapi python manage.py migrate --settings=config.docker-compose
50-
docker exec pokeapi sh -c 'echo "from data.v2.build import build_all; build_all()" | python manage.py shell --settings=config.docker-compose'
49+
docker exec pokeapi uv run manage.py migrate --settings=config.docker-compose
50+
docker exec pokeapi sh -c 'echo "from data.v2.build import build_all; build_all()" | uv run manage.py shell --settings=config.docker-compose'
5151
```

data/v2/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# To build out the data you'll need to jump into the Django shell
22
#
3-
# $ python manage.py shell
3+
# $ uv run manage.py shell
44
#
55
# and run the build script with
66
#

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies = [
1414
"drf-spectacular==0.29.0",
1515
"gunicorn==23.0.0",
1616
"psycopg==3.3.2",
17+
"psycopg2-binary>=2.9.11",
1718
"ruff>=0.14.14",
1819
"ty>=0.0.14",
1920
]

uv.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)