Skip to content

Commit

Permalink
Merged dockerfiles into 1 file, and split compose files up #5
Browse files Browse the repository at this point in the history
  • Loading branch information
asuresh-code committed Jan 24, 2025
1 parent f97a991 commit 7ec2aec
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 18 deletions.
38 changes: 37 additions & 1 deletion Dockerfile
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

WORKDIR /object-storage-api-run

Expand All @@ -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"]

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
16 changes: 0 additions & 16 deletions Dockerfile.prod

This file was deleted.

4 changes: 3 additions & 1 deletion docker-compose.yml → docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
services:
object-storage-api:
container_name: object_storage_api_container
build: .
build:
context: .
target: dev
volumes:
- ./object_storage_api:/object-storage-api-run/object_storage_api
- ./keys:/object-storage-api-run/keys
Expand Down
64 changes: 64 additions & 0 deletions docker-compose-test.yml
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: test
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"

0 comments on commit 7ec2aec

Please sign in to comment.