mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2026-06-10 06:40:38 +03:00
Refactor Dockerfile and compose.yml for slim-build improvements and remove unused database services.
This commit is contained in:
@@ -66,12 +66,17 @@ py-upgrade: ## Upgrade Python syntax to a newer version
|
|||||||
lint: ## Lint and format project code
|
lint: ## Lint and format project code
|
||||||
uv run ruff check --fix .
|
uv run ruff check --fix .
|
||||||
|
|
||||||
|
|
||||||
# ====================================================================================
|
# ====================================================================================
|
||||||
# DOCKER IMAGE BUILDING
|
# DOCKER IMAGE BUILDING
|
||||||
# ====================================================================================
|
# ====================================================================================
|
||||||
.PHONY: slim-build
|
.PHONY: slim-build
|
||||||
slim-build: ## Build smaller and more secure Docker images with docker-slim
|
slim-build: ## Build smaller and more secure Docker images with docker-slim
|
||||||
docker-slim build --compose-file docker-compose.yml --target-compose-svc api1 --dep-include-target-compose-svc-deps true --http-probe-exec api1 fastapi-sqlalchemy-asyncpg_api1:latest
|
docker-slim build --compose-file compose.yml \
|
||||||
|
--target-compose-svc api1 \
|
||||||
|
--dep-include-target-compose-svc-deps true \
|
||||||
|
--http-probe-exec api1 fastapi-sqlalchemy-asyncpg-api1:latest \
|
||||||
|
--include-path /panettone/lib/python3.14
|
||||||
|
|
||||||
# ====================================================================================
|
# ====================================================================================
|
||||||
# DATABASE SEEDING
|
# DATABASE SEEDING
|
||||||
|
|||||||
+9
-9
@@ -30,21 +30,21 @@ async def lifespan(app: FastAPI):
|
|||||||
app.redis = await get_redis()
|
app.redis = await get_redis()
|
||||||
postgres_dsn = global_settings.postgres_url.unicode_string()
|
postgres_dsn = global_settings.postgres_url.unicode_string()
|
||||||
try:
|
try:
|
||||||
app.postgres_pool = await asyncpg.create_pool(
|
# app.postgres_pool = await asyncpg.create_pool(
|
||||||
dsn=postgres_dsn,
|
# dsn=postgres_dsn,
|
||||||
min_size=5,
|
# min_size=5,
|
||||||
max_size=20,
|
# max_size=20,
|
||||||
)
|
# )
|
||||||
await app.logger.ainfo(
|
# await app.logger.ainfo(
|
||||||
"Postgres pool created", idle_size=app.postgres_pool.get_idle_size()
|
# "Postgres pool created", idle_size=app.postgres_pool.get_idle_size()
|
||||||
)
|
# )
|
||||||
yield
|
yield
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
await app.logger.aerror("Error during app startup", error=repr(e))
|
await app.logger.aerror("Error during app startup", error=repr(e))
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
await app.redis.close()
|
await app.redis.close()
|
||||||
await app.postgres_pool.close()
|
# await app.postgres_pool.close()
|
||||||
|
|
||||||
|
|
||||||
middleware = [
|
middleware = [
|
||||||
|
|||||||
+43
-43
@@ -1,7 +1,7 @@
|
|||||||
services:
|
services:
|
||||||
api1:
|
api1:
|
||||||
container_name: panettone_api1
|
container_name: panettone_api1
|
||||||
build: .
|
image: fastapi-sqlalchemy-asyncpg-api1.slim:latest
|
||||||
environment:
|
environment:
|
||||||
- PYTHONPATH=/panettone
|
- PYTHONPATH=/panettone
|
||||||
env_file:
|
env_file:
|
||||||
@@ -19,45 +19,45 @@ services:
|
|||||||
- ./alembic:/panettone/alembic
|
- ./alembic:/panettone/alembic
|
||||||
ports:
|
ports:
|
||||||
- "8080:8080"
|
- "8080:8080"
|
||||||
depends_on:
|
# depends_on:
|
||||||
postgres:
|
# postgres:
|
||||||
condition: service_healthy
|
# condition: service_healthy
|
||||||
redis:
|
# redis:
|
||||||
condition: service_started
|
# condition: service_started
|
||||||
|
#
|
||||||
postgres:
|
# postgres:
|
||||||
container_name: panettone_postgres
|
# container_name: panettone_postgres
|
||||||
build:
|
# build:
|
||||||
context: ./db
|
# context: ./db
|
||||||
dockerfile: Dockerfile
|
# dockerfile: Dockerfile
|
||||||
volumes:
|
# volumes:
|
||||||
- panettone_postgres_data:/var/lib/postgresql/data
|
# - panettone_postgres_data:/var/lib/postgresql/data
|
||||||
env_file:
|
# env_file:
|
||||||
- .env
|
# - .env
|
||||||
ports:
|
# ports:
|
||||||
- 5432:5432
|
# - 5432:5432
|
||||||
environment:
|
# environment:
|
||||||
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set}
|
# - POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set}
|
||||||
- POSTGRES_USER=${POSTGRES_USER?Variable not set}
|
# - POSTGRES_USER=${POSTGRES_USER?Variable not set}
|
||||||
- POSTGRES_DB=${POSTGRES_DB?Variable not set}
|
# - POSTGRES_DB=${POSTGRES_DB?Variable not set}
|
||||||
healthcheck:
|
# healthcheck:
|
||||||
test:
|
# test:
|
||||||
[
|
# [
|
||||||
"CMD-SHELL", "pg_isready -d $POSTGRES_DB -U $POSTGRES_USER"
|
# "CMD-SHELL", "pg_isready -d $POSTGRES_DB -U $POSTGRES_USER"
|
||||||
]
|
# ]
|
||||||
interval: 5s
|
# interval: 5s
|
||||||
timeout: 5s
|
# timeout: 5s
|
||||||
retries: 5
|
# retries: 5
|
||||||
|
#
|
||||||
redis:
|
# redis:
|
||||||
image: redis:latest
|
# image: redis:latest
|
||||||
container_name: panettone_redis
|
# container_name: panettone_redis
|
||||||
ports:
|
# ports:
|
||||||
- "6379:6379"
|
# - "6379:6379"
|
||||||
env_file:
|
# env_file:
|
||||||
- .env
|
# - .env
|
||||||
entrypoint: redis-server --appendonly yes
|
# entrypoint: redis-server --appendonly yes
|
||||||
|
#
|
||||||
|
#
|
||||||
volumes:
|
#volumes:
|
||||||
panettone_postgres_data: {}
|
# panettone_postgres_data: {}
|
||||||
Reference in New Issue
Block a user