From 61597737e151757834f8f597208584f46f5081a2 Mon Sep 17 00:00:00 2001 From: "airo.pi_" <47398145+AiroPi@users.noreply.github.com> Date: Tue, 5 Dec 2023 23:42:06 +0100 Subject: [PATCH] Dockerfile optimisations! --- Dockerfile | 27 +++++++++++++++++++++------ bin/alembic.sh | 2 ++ docker-compose.dev.yml | 2 -- docker-compose.yml | 8 ++++++-- 4 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 bin/alembic.sh diff --git a/Dockerfile b/Dockerfile index 914e1b5..9bfe296 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,30 @@ -FROM python:3.11.2 as base +FROM python:3.12.0-alpine as build +RUN python -m venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" +RUN --mount=type=cache,target=/var/cache/apk/ \ + --mount=type=cache,target=/root/.cache/pip \ + --mount=type=bind,source=requirements.txt,target=requirements.txt \ + : \ + && apk add gcc musl-dev linux-headers \ + && pip install -U -r requirements.txt + + +FROM python:3.12.0-alpine as base WORKDIR /app +COPY --from=build /opt/venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" ENV PYTHONUNBUFFERED=0 -COPY requirements.txt alembic.ini ./ -RUN pip install -U -r requirements.txt +COPY ./alembic.ini ./ +COPY ./alembic ./alembic +COPY ./src ./ + FROM base as prod -COPY ./src ./ -CMD ["/bin/bash", "-c", "alembic upgrade head && python ./main.py bot --sync -c ./config.toml"] +CMD ["/bin/sh", "-c", "alembic upgrade head && python ./main.py bot --sync -c ./config.toml"] + FROM base as debug ENV DEBUG=1 ENV LOG_LEVEL=DEBUG RUN pip install debugpy -CMD ["/bin/bash", "-c", "alembic upgrade head && python -m debugpy --wait-for-client --listen 0.0.0.0:5678 ./src/main.py bot -c ./config.toml"] +CMD ["/bin/sh", "-c", "alembic upgrade head && python -m debugpy --wait-for-client --listen 0.0.0.0:5678 ./main.py bot -c ./config.toml"] diff --git a/bin/alembic.sh b/bin/alembic.sh new file mode 100644 index 0000000..75ea34e --- /dev/null +++ b/bin/alembic.sh @@ -0,0 +1,2 @@ +docker compose --progress quiet up database -d --quiet-pull +docker compose --progress quiet run --rm -t -v "${PWD}/alembic:/app/alembic" mybot alembic "$@" diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 56d7243..327c9aa 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -5,8 +5,6 @@ services: build: target: debug restart: "no" - volumes: - - ./src:/app/src ports: - 5678:5678 # for debugging - 8080:8080 diff --git a/docker-compose.yml b/docker-compose.yml index ff7bdbf..4e8476c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,6 +7,11 @@ services: context: . dockerfile: ./Dockerfile target: prod + develop: + watch: + - action: sync + path: ./src + target: /app env_file: - .env tty: true @@ -15,8 +20,7 @@ services: database: condition: service_healthy volumes: - - ./data/locale:/app/locale - - ./alembic:/app/alembic + - ./resources/locale:/app/locale - ./config.toml:/app/config.toml ports: - 666:8081