Skip to content

Commit 19945ee

Browse files
Adding example files for running locally with Uvicorn.
1 parent be4aea2 commit 19945ee

File tree

3 files changed

+164
-2
lines changed

3 files changed

+164
-2
lines changed

.gitignore

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,11 @@ cython_debug/
138138

139139
# Config files:
140140
src/.env
141-
docker-compose.yml
142-
Dockerfile
141+
142+
# Ignore root files:
143+
/Dockerfile
144+
/docker-compose.yml
145+
146+
# Don't ignore files inside of script folder:
147+
!scripts/*
148+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# --------- Builder Stage ---------
2+
FROM ghcr.io/astral-sh/uv:python3.11-bookworm-slim AS builder
3+
4+
# Set environment variables for uv
5+
ENV UV_COMPILE_BYTECODE=1
6+
ENV UV_LINK_MODE=copy
7+
8+
WORKDIR /app
9+
10+
# Install dependencies first (for better layer caching)
11+
RUN --mount=type=cache,target=/root/.cache/uv \
12+
--mount=type=bind,source=uv.lock,target=uv.lock \
13+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
14+
uv sync --locked --no-install-project
15+
16+
# Copy the project source code
17+
COPY . /app
18+
19+
# Install the project in non-editable mode
20+
RUN --mount=type=cache,target=/root/.cache/uv \
21+
uv sync --locked --no-editable
22+
23+
# --------- Final Stage ---------
24+
FROM python:3.11-slim-bookworm
25+
26+
# Create a non-root user for security
27+
RUN groupadd --gid 1000 app \
28+
&& useradd --uid 1000 --gid app --shell /bin/bash --create-home app
29+
30+
# Copy the virtual environment from the builder stage
31+
COPY --from=builder --chown=app:app /app/.venv /app/.venv
32+
33+
# Ensure the virtual environment is in the PATH
34+
ENV PATH="/app/.venv/bin:$PATH"
35+
36+
# Switch to the non-root user
37+
USER app
38+
39+
# Set the working directory
40+
WORKDIR /code
41+
42+
# -------- replace with comment to run with gunicorn --------
43+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
44+
# CMD ["gunicorn", "app.main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "-b", "0.0.0.0:8000"]
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
services:
2+
web:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
# -------- Both of the following commands should be commented to run with nginx --------
7+
8+
# -------- replace with comment to run with gunicorn --------
9+
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
10+
# command: gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000
11+
env_file:
12+
- ./src/.env
13+
# -------- replace with expose if you are using nginx --------
14+
ports:
15+
- "8000:8000"
16+
# expose:
17+
# - "8000"
18+
depends_on:
19+
- db
20+
- redis
21+
volumes:
22+
- ./src/app:/code/app
23+
- ./src/.env:/code/.env
24+
25+
worker:
26+
build:
27+
context: .
28+
dockerfile: Dockerfile
29+
command: arq app.core.worker.settings.WorkerSettings
30+
env_file:
31+
- ./src/.env
32+
depends_on:
33+
- db
34+
- redis
35+
volumes:
36+
- ./src/app:/code/app
37+
- ./src/.env:/code/.env
38+
39+
db:
40+
image: postgres:13
41+
env_file:
42+
- ./src/.env
43+
volumes:
44+
- postgres-data:/var/lib/postgresql/data
45+
expose:
46+
- "5432"
47+
48+
redis:
49+
image: redis:alpine
50+
volumes:
51+
- redis-data:/data
52+
expose:
53+
- "6379"
54+
55+
#-------- uncomment to run with nginx --------
56+
# nginx:
57+
# image: nginx:latest
58+
# ports:
59+
# - "80:80"
60+
# volumes:
61+
# - ./default.conf:/etc/nginx/conf.d/default.conf
62+
# depends_on:
63+
# - web
64+
65+
#-------- uncomment to create first superuser --------
66+
create_superuser:
67+
build:
68+
context: .
69+
dockerfile: Dockerfile
70+
env_file:
71+
- ./src/.env
72+
depends_on:
73+
- db
74+
- web
75+
command: python -m src.scripts.create_first_superuser
76+
volumes:
77+
- ./src:/code/src
78+
79+
#-------- uncomment to run tests --------
80+
pytest:
81+
build:
82+
context: .
83+
dockerfile: Dockerfile
84+
env_file:
85+
- ./src/.env
86+
depends_on:
87+
- db
88+
- create_superuser
89+
- redis
90+
command: python -m pytest ./tests
91+
volumes:
92+
- .:/code
93+
94+
#-------- uncomment to create first tier --------
95+
# create_tier:
96+
# build:
97+
# context: .
98+
# dockerfile: Dockerfile
99+
# env_file:
100+
# - ./src/.env
101+
# depends_on:
102+
# - create_superuser
103+
# - db
104+
# - web
105+
# command: python -m src.scripts.create_first_tier
106+
# volumes:
107+
# - ./src:/code/src
108+
109+
volumes:
110+
postgres-data:
111+
redis-data:
112+

0 commit comments

Comments
 (0)