-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add production Dockerfile and ci image upload workflow #70
base: develop
Are you sure you want to change the base?
Changes from 12 commits
4bba5fe
cd4ca0e
ba8bfc3
05a7082
4e06f88
bf5e983
a2d28e0
1b2aa49
f97a991
7ec2aec
9a87e9c
823c294
11ae8d0
2decebd
2f74518
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM python:3.12.8-alpine3.20@sha256:0c4f778362f30cc50ff734a3e9e7f3b2ae876d8386f470e0c3ee1ab299cec21b | ||
FROM python:3.12.8-alpine3.20@sha256:0c4f778362f30cc50ff734a3e9e7f3b2ae876d8386f470e0c3ee1ab299cec21b as base | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't really need a base stage in this case. You can just move this to the |
||
WORKDIR /object-storage-api-run | ||
|
||
|
@@ -10,5 +10,41 @@ RUN --mount=type=cache,target=/root/.cache \ | |
\ | ||
python3 -m pip install -r requirements.txt; | ||
|
||
FROM python:3.12.8-alpine3.20@sha256:0c4f778362f30cc50ff734a3e9e7f3b2ae876d8386f470e0c3ee1ab299cec21b as dev | ||
|
||
WORKDIR /object-storage-api-run | ||
|
||
COPY --from=base /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages | ||
COPY --from=base /usr/local/bin /usr/local/bin | ||
COPY object_storage_api/ object_storage_api/ | ||
|
||
CMD ["fastapi", "dev", "object_storage_api/main.py", "--host", "0.0.0.0", "--port", "8000"] | ||
EXPOSE 8000 | ||
|
||
FROM dev as test | ||
|
||
WORKDIR /object-storage-api-run | ||
|
||
COPY test/ test/ | ||
|
||
CMD ["pytest", "--config-file", "test/pytest.ini", "test/", "--cov"] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would fail because only the prod dependencies are installed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've opted to install from the pyproject.toml instead to get the test dependencies, which I believe should solve the issue. |
||
FROM python:3.12.8-alpine3.20@sha256:0c4f778362f30cc50ff734a3e9e7f3b2ae876d8386f470e0c3ee1ab299cec21b as prod | ||
|
||
WORKDIR /object-storage-api-run | ||
|
||
COPY requirements.txt ./ | ||
COPY object_storage_api/ object_storage_api/ | ||
|
||
RUN --mount=type=cache,target=/root/.cache \ | ||
set -eux; \ | ||
\ | ||
python3 -m pip install --no-cache-dir -r requirements.txt; \ | ||
# Create a non-root user to run as \ | ||
addgroup -S object-storage-api; \ | ||
adduser -S -D -G object-storage-api -H -h /object-storage-api-run object-storage-api; | ||
|
||
USER object-storage-api | ||
|
||
CMD ["fastapi", "run", "object_storage_api/main.py", "--host", "0.0.0.0", "--port", "8000"] | ||
EXPOSE 8000 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
services: | ||
object-storage-api: | ||
container_name: object_storage_api_container | ||
build: | ||
context: . | ||
target: ${TARGET_STAGE:-dev} | ||
volumes: | ||
- ./object_storage_api:/object-storage-api-run/object_storage_api | ||
- ./keys:/object-storage-api-run/keys | ||
restart: on-failure | ||
ports: | ||
- 8002:8000 | ||
depends_on: | ||
- mongo-db | ||
- minio | ||
environment: | ||
DATABASE__HOST_AND_OPTIONS: object_storage_api_mongodb_container:27017/?authMechanism=SCRAM-SHA-256&authSource=admin | ||
extra_hosts: | ||
# Want to use localhost for MinIO connection so the presigned URLs are correct but also want to avoid using host | ||
# networking | ||
- "localhost:host-gateway" | ||
|
||
mongo-db: | ||
image: mongo:7.0-jammy | ||
container_name: object_storage_api_mongodb_container | ||
volumes: | ||
- ./mongodb/data:/data/db | ||
restart: always | ||
ports: | ||
- 27018:27017 | ||
environment: | ||
MONGO_INITDB_ROOT_USERNAME: root | ||
MONGO_INITDB_ROOT_PASSWORD: example | ||
|
||
minio: | ||
image: minio/minio:RELEASE.2024-09-13T20-26-02Z | ||
container_name: object_storage_minio_container | ||
command: minio server /data | ||
volumes: | ||
- ./minio/data:/data | ||
ports: | ||
- 9000:9000 | ||
- 9001:9001 | ||
environment: | ||
MINIO_ROOT_USER: root | ||
MINIO_ROOT_PASSWORD: example_password | ||
MINIO_ADDRESS: ":9000" | ||
MINIO_CONSOLE_ADDRESS: ":9001" | ||
network_mode: "host" | ||
|
||
# From https://stackoverflow.com/questions/66412289/minio-add-a-public-bucket-with-docker-compose | ||
minio_create_buckets: | ||
image: minio/mc | ||
container_name: object_storage_minio_mc_container | ||
depends_on: | ||
- minio | ||
entrypoint: > | ||
/bin/sh -c " | ||
/usr/bin/mc alias set object-storage http://localhost:9000 root example_password; | ||
/usr/bin/mc mb object-storage/object-storage; | ||
/usr/bin/mc mb object-storage/test-object-storage; | ||
exit 0; | ||
" | ||
network_mode: "host" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests should now run in a Docker container. This requires some thinking given there are multiple jobs for the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 solution I've thought of is in the
Dockerfile
, we can split up the build stage for test into a unit test stage, and e2e test stage (the only difference being the command run).Then in the workflow in the
run e2e/unit test
step of the separate jobs, we build the image targeting the unit or e2e stage respectively, and then run the image instead of using the docker compose file.This saves us having to create a new compose file for unit/e2e testing, or introduce environment variables into the compose file. This does create a new problem of now the docker-compose.test.yml file no longer has a target stage that runs all of the tests. This could be addressed by:
docker-compose -f docker-compose.test.yml up object-storage-api-unit-tests