Skip to content

Commit bd82cb2

Browse files
authored
Merge pull request #59 from febus982/fix_container_exec
Use array syntax in Dockerfile, minor improvements
2 parents e8b19c8 + 48faac9 commit bd82cb2

File tree

4 files changed

+21
-28
lines changed

4 files changed

+21
-28
lines changed

Dockerfile

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ RUN apt-get update \
2020
RUN pip install --no-cache-dir -U pip
2121
RUN pip install --no-cache-dir -U poetry
2222

23+
# We run everything by poetry run from now on, so that PATH will be handled
24+
# for binaries installed in virtual environments
25+
ENTRYPOINT ["poetry", "run"]
26+
2327
FROM base as base_builder
2428
# Install build system requirements (gcc, library headers, etc.)
2529
# for compiled Python requirements like psycopg2
@@ -37,9 +41,12 @@ COPY --chown=nonroot:nonroot pyproject.toml .
3741
COPY --chown=nonroot:nonroot poetry.lock .
3842

3943
# Test image, contains all files and dependencies
40-
FROM base_builder as test
44+
FROM base_builder as dev
4145
RUN poetry install --with http,grpc,dev
4246
COPY --chown=nonroot:nonroot . .
47+
# Note that opentelemetry doesn't play well together with uvicorn reloader
48+
# when signals are propagated, we disable it in dev image default CMD
49+
CMD ["uvicorn", "http_app:create_app", "--host", "0.0.0.0", "--port", "8000", "--factory", "--reload"]
4350

4451
# Installs requirements to run production http application
4552
FROM base_builder as http_builder
@@ -62,20 +69,17 @@ COPY --chown=nonroot:nonroot config.py .
6269
COPY --chown=nonroot:nonroot di_container.py .
6370
COPY --chown=nonroot:nonroot alembic.ini .
6471
COPY --chown=nonroot:nonroot Makefile .
65-
ENTRYPOINT ["poetry", "run"]
6672

6773
# Copy the http python package and requirements from relevant builder
6874
FROM base_app as http_app
6975
COPY --from=http_builder /poetryvenvs /poetryvenvs
7076
COPY --chown=nonroot:nonroot http_app ./http_app
71-
# opentelemetry-instrument will spawn a subprocess, therefore we use exec
72-
# to make sure the app runs on PID 1 and receives correctly system signals
73-
CMD opentelemetry-instrument uvicorn http_app:create_app --host 0.0.0.0 --port 8000 --factory
77+
# Run CMD using array syntax, so it's uses `exec` and runs as PID1
78+
CMD ["opentelemetry-instrument", "uvicorn", "http_app:create_app", "--host", "0.0.0.0", "--port", "8000", "--factory"]
7479

7580
# Copy the grpc python package and requirements from relevant builder
7681
FROM base_app as grpc_app
7782
COPY --from=grpc_builder /poetryvenvs /poetryvenvs
7883
COPY --chown=nonroot:nonroot grpc_app ./grpc_app
79-
# opentelemetry-instrument will spawn a subprocess, therefore we use exec
80-
# to make sure the app runs on PID 1 and receives correctly system signals
81-
CMD exec opentelemetry-instrument python3 -m grpc_app
84+
# Run CMD using array syntax, so it's uses `exec` and runs as PID1
85+
CMD ["opentelemetry-instrument", "python3", "-m", "grpc_app"]

docker-compose.yaml

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
version: '3.8'
22
services:
3-
# We don't use `make` or `poetry` for long running processes,
4-
# to make sure they run as pid 1 and receive SIGTERM signal
53
dev:
64
build:
75
dockerfile: Dockerfile
86
context: .
9-
target: http_app
7+
target: dev
108
ports:
119
- '8000:8000'
12-
command:
13-
- "uvicorn"
14-
- "http_app:create_app"
15-
- "--host"
16-
- "0.0.0.0"
17-
- "--port"
18-
- "8000"
19-
- "--factory"
20-
- "--reload"
2110
volumes:
2211
- '.:/app'
2312
http:
@@ -66,7 +55,7 @@ services:
6655
build:
6756
dockerfile: Dockerfile
6857
context: .
69-
target: test
58+
target: dev
7059
volumes:
7160
- '.:/app'
7261
command:
@@ -77,7 +66,7 @@ services:
7766
build:
7867
dockerfile: Dockerfile
7968
context: .
80-
target: test
69+
target: dev
8170
command:
8271
- "make"
8372
- "ci-test"

docker-container.png

-5.07 KB
Loading

docker-container.puml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ usecase base_builder [
1515
""dependencies""
1616
""(e.g. gcc, library headers)""
1717
]
18-
usecase test [
19-
=Test
18+
usecase dev [
19+
=Dev
2020
----
2121
""Fat image containing everything""
2222
""necessary to run and test""
@@ -32,12 +32,12 @@ usecase base_app [
3232

3333
rectangle HTTP {
3434
usecase http_builder [
35-
=Http builder
35+
=HTTP builder
3636
----
3737
""Builds HTTP dependencies""
3838
]
3939
usecase http_app [
40-
=Http app
40+
=HTTP app
4141
----
4242
""Copies HTTP framework""
4343
""shared logic""
@@ -54,7 +54,7 @@ rectangle GRPC {
5454
usecase grpc_app [
5555
=GRPC app
5656
----
57-
""Copies HTTP framework""
57+
""Copies GRPC framework""
5858
""shared logic""
5959
""and dependencies""
6060
"" to run the application""
@@ -72,5 +72,5 @@ base_builder ===> grpc_builder
7272
grpc_builder ===> grpc_app
7373
base_app ===> grpc_app
7474

75-
base_builder ==r=> test
75+
base_builder ==r=> dev
7676
@enduml

0 commit comments

Comments
 (0)