Skip to content

Commit c1b547f

Browse files
authored
Utilize uv in Docker and workflows (#73)
1 parent d31c07f commit c1b547f

File tree

6 files changed

+124
-45
lines changed

6 files changed

+124
-45
lines changed

docker/Dockerfile

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,62 @@
1-
FROM python:3.13-slim-trixie
1+
FROM ghcr.io/astral-sh/uv:python3.13-trixie-slim AS build
2+
3+
# Compile bytecode and enable caching
4+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#compiling-bytecode
5+
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#caching
6+
ENV UV_COMPILE_BYTECODE=1
7+
ENV UV_LINK_MODE=copy
8+
9+
# Disable Python downloads as the system Python interpreter instead
10+
ENV UV_PYTHON_DOWNLOADS=0
211

3-
ENV DEBIAN_FRONTEND=noninteractive
12+
# Set Tini version
13+
# Ref: https://github.com/krallin/tini?tab=readme-ov-file#using-tini
414
ENV TINI_VERSION=v0.19.0
515

616
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
7-
RUN chmod +x /tini
817

9-
WORKDIR /kanae
10-
COPY /server /kanae/server/
11-
COPY /requirements.txt /kanae/requirements.txt
18+
WORKDIR /kanae/
1219

13-
RUN adduser --disabled-password --gecos "" kanae \
14-
&& chown -R kanae:kanae /kanae
20+
COPY requirements.txt /kanae
1521

16-
USER kanae
22+
# Install dependencies
23+
RUN --mount=type=cache,target=/root/.cache/uv \
24+
--mount=type=bind,source=requirements.txt,target=requirements.txt \
25+
uv venv && uv pip install -r requirements.txt
26+
27+
# Add project source code
28+
COPY server /kanae
29+
30+
FROM python:3.13-slim-trixie
31+
32+
ENV PYTHONUNBUFFERED=1
33+
ENV PYTHONPATH=/kanae
34+
35+
# Set up non-root user
36+
RUN groupadd --system --gid 999 kanae \
37+
&& useradd --system --gid 999 --uid 999 --no-create-home kanae
1738

18-
ENV PATH="${PATH}:/home/kanae/.local/bin"
39+
# Copy the server from the builder
40+
COPY --from=build --chmod=755 /kanae /kanae
41+
COPY --from=build --chmod=+x /tini /tini
1942

20-
RUN pip install --user -r requirements.txt
43+
WORKDIR /kanae/
44+
45+
# Place executables in the environment at the front of the path
46+
ENV PATH="/kanae/.venv/bin:$PATH"
47+
48+
# Use non-root user
49+
USER kanae
2150

2251
EXPOSE 8000
2352

2453
ENTRYPOINT ["/tini", "--"]
2554

26-
CMD ["python3", "/kanae/server/launcher.py"]
55+
CMD ["python3", "/kanae/launcher.py"]
2756

2857
STOPSIGNAL SIGTERM
2958

3059
LABEL org.opencontainers.image.title="Kanae"
3160
LABEL org.opencontainers.image.description="Internal backend server for ACM @ UC Merced"
3261
LABEL org.opencontainers.image.licenses="Apache-2.0"
33-
LABEL org.opencontainers.image.source="https://github.com/UCMercedACM/kanae"
62+
LABEL org.opencontainers.image.source="https://github.com/UCMercedACM/kanae"

docker/docker-compose.dev.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ name: kanae_dev
44
services:
55
database:
66
container_name: kanae_postgres
7-
build:
8-
context: ../
9-
dockerfile: docker/pg-test/Dockerfile
7+
image: postgres:17
108
environment:
119
POSTGRES_PASSWORD: ${DB_PASSWORD}
1210
POSTGRES_DB: ${DB_DATABASE_NAME}
1311
POSTGRES_USER: ${DB_USERNAME}
1412
volumes:
1513
- database:/var/lib/postgresql/data
16-
env_file:
17-
- .env
1814
ports:
1915
- 5432:5432
2016

docker/docker-compose.prod.yml

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,36 @@ services:
66
image: ghcr.io/ucmercedacm/kanae:latest
77
volumes:
88
# Do not edit the next line. If you want to change the path of the configuration file, please edit the CONFIG_LOCATION variable
9-
- ${CONFIG_LOCATION}:/kanae/server/config.yml
9+
- ${CONFIG_LOCATION}:/kanae/config.yml
1010
ports:
1111
- 8000:8000
12+
networks:
13+
- default
14+
- db_bridge
1215
depends_on:
1316
database:
1417
condition: service_healthy
1518
restart: true
1619
valkey:
1720
condition: service_healthy
1821
restart: true
19-
env_file:
20-
- .env
22+
migrate:
23+
condition: service_completed_successfully
2124
restart: always
2225

2326
database:
2427
container_name: kanae_postgres
25-
build:
26-
context: ../
27-
dockerfile: docker/pg-test/Dockerfile
28+
image: postgres:17
2829
environment:
2930
POSTGRES_PASSWORD: ${DB_PASSWORD}
3031
POSTGRES_DB: ${DB_DATABASE_NAME}
3132
POSTGRES_USER: ${DB_USERNAME}
3233
POSTGRES_INITDB_ARGS: '--data-checksums'
3334
ports:
3435
- 5432:5432
36+
networks:
37+
- default
38+
- db_bridge
3539
volumes:
3640
- database:/var/lib/postgresql/data
3741
healthcheck:
@@ -48,8 +52,27 @@ services:
4852
test: valkey-cli PING || exit 1
4953
ports:
5054
- 6379:6379
55+
restart: always
56+
57+
migrate:
58+
container_name: kanae_migrate
59+
image: arigaio/atlas:latest
60+
command: >
61+
schema apply
62+
--auto-approve
63+
--to "file://schema.sql"
64+
--url "postgres://${DB_USERNAME}:${DB_PASSWORD}@database:5432/${DB_DATABASE_NAME}?search_path=public&sslmode=disable"
65+
--dev-url "postgres://${DB_USERNAME}:${DB_PASSWORD}@database:5432/postgres?search_path=public&sslmode=disable"
66+
networks:
67+
- db_bridge
68+
- default
69+
depends_on:
70+
database:
71+
condition: service_healthy
72+
volumes:
73+
- ../server/schema.sql:/schema.sql
5174

52-
kanae-prometheus:
75+
prometheus:
5376
container_name: kanae_prometheus
5477
ports:
5578
- 9090:9090
@@ -59,8 +82,8 @@ services:
5982
- prometheus-data:/prometheus
6083

6184
# first login uses admin/admin
62-
# add data source for http://kanae-prometheus:9090 to get started
63-
kanae-grafana:
85+
# add data source for http://prometheus:9090 to get started
86+
grafana:
6487
container_name: kanae_grafana
6588
command: ['./run.sh', '-disable-reporting']
6689
ports:
@@ -73,3 +96,8 @@ volumes:
7396
database:
7497
prometheus-data:
7598
grafana-data:
99+
100+
networks:
101+
db_bridge:
102+
driver: bridge
103+

docker/docker-compose.yml

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,42 @@ name: kanae
33
services:
44
kanae:
55
container_name: kanae
6-
image: ghcr.io/ucmercedacm/kanae:latest
6+
# image: ghcr.io/ucmercedacm/kanae:latest
7+
build:
8+
context: ..
9+
dockerfile: docker/Dockerfile
710
volumes:
811
# Do not edit the next line. If you want to change the path of the configuration file, please edit the CONFIG_LOCATION variable
9-
- ${CONFIG_LOCATION}:/kanae/server/config.yml
12+
- ${CONFIG_LOCATION}:/kanae/config.yml
1013
ports:
1114
- 8000:8000
15+
networks:
16+
- default
17+
- db_bridge
1218
depends_on:
1319
database:
1420
condition: service_healthy
1521
restart: true
1622
valkey:
1723
condition: service_healthy
1824
restart: true
19-
env_file:
20-
- .env
25+
migrate:
26+
condition: service_completed_successfully
2127
restart: always
2228

2329
database:
2430
container_name: kanae_postgres
25-
build:
26-
context: ../
27-
dockerfile: docker/pg-test/Dockerfile
31+
image: postgres:17
2832
environment:
2933
POSTGRES_PASSWORD: ${DB_PASSWORD}
3034
POSTGRES_DB: ${DB_DATABASE_NAME}
3135
POSTGRES_USER: ${DB_USERNAME}
3236
POSTGRES_INITDB_ARGS: '--data-checksums'
3337
ports:
3438
- 5432:5432
39+
networks:
40+
- default
41+
- db_bridge
3542
volumes:
3643
- database:/var/lib/postgresql/data
3744
healthcheck:
@@ -48,6 +55,29 @@ services:
4855
test: valkey-cli PING || exit 1
4956
ports:
5057
- 6379:6379
58+
restart: always
5159

60+
migrate:
61+
container_name: kanae_migrate
62+
image: arigaio/atlas:latest
63+
command: >
64+
schema apply
65+
--auto-approve
66+
--to "file://schema.sql"
67+
--url "postgres://${DB_USERNAME}:${DB_PASSWORD}@database:5432/${DB_DATABASE_NAME}?search_path=public&sslmode=disable"
68+
--dev-url "postgres://${DB_USERNAME}:${DB_PASSWORD}@database:5432/postgres?search_path=public&sslmode=disable"
69+
networks:
70+
- db_bridge
71+
- default
72+
depends_on:
73+
database:
74+
condition: service_healthy
75+
volumes:
76+
- ../server/schema.sql:/schema.sql
77+
5278
volumes:
5379
database:
80+
81+
networks:
82+
db_bridge:
83+
driver: bridge

docker/pg-test/Dockerfile

Lines changed: 0 additions & 4 deletions
This file was deleted.

server/utils/config.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,17 @@ def configure_logging(self) -> None:
191191
handler.setFormatter(self._determine_formatter(handler))
192192

193193
if self.access_log:
194-
file_handler = RotatingFileHandler(
195-
filename="kanae-access.log",
196-
encoding="utf-8",
197-
mode="w",
198-
maxBytes=max_bytes,
199-
backupCount=5,
200-
)
201194
access_logger.setLevel(level)
202195
access_logger.addHandler(handler)
203196

204197
if not self._is_docker():
198+
file_handler = RotatingFileHandler(
199+
filename="kanae-access.log",
200+
encoding="utf-8",
201+
mode="w",
202+
maxBytes=max_bytes,
203+
backupCount=5,
204+
)
205205
access_logger.addHandler(file_handler)
206206

207207
root.setLevel(level)

0 commit comments

Comments
 (0)