Skip to content

Commit a7e35c0

Browse files
committed
chore(docker): improve Dockerfile build efficiency
1 parent 7f66fd6 commit a7e35c0

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

Dockerfile

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,37 @@
1-
# Use the official Python image as base
2-
FROM python:3-slim-bullseye as python-base
1+
FROM python:3-slim-bullseye AS python-base
32

4-
# Set environment variables
53
ENV PYTHONUNBUFFERED=1 \
6-
PYTHONDONTWRITEBYTECODE=1 \
7-
PIP_NO_CACHE_DIR=off \
8-
PIP_DISABLE_PIP_VERSION_CHECK=on \
9-
PIP_DEFAULT_TIMEOUT=100 \
10-
POETRY_HOME="/opt/poetry" \
11-
POETRY_VIRTUALENVS_IN_PROJECT=true \
12-
POETRY_NO_INTERACTION=1 \
13-
PYSETUP_PATH="/opt/pysetup" \
14-
VENV_PATH="/opt/pysetup/.venv"
15-
16-
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"
17-
18-
FROM python-base as builder-base
19-
20-
# Install system dependencies
21-
RUN apt-get update && apt-get install -y --no-install-recommends curl build-essential && \
4+
PYTHONDONTWRITEBYTECODE=1 \
5+
PIP_NO_CACHE_DIR=on \
6+
PIP_DISABLE_PIP_VERSION_CHECK=on \
7+
PIP_DEFAULT_TIMEOUT=100 \
8+
POETRY_HOME="/opt/poetry" \
9+
POETRY_VIRTUALENVS_IN_PROJECT=true \
10+
POETRY_NO_INTERACTION=1 \
11+
PATH="/opt/poetry/bin:$PATH"
12+
13+
FROM python-base AS builder-base
14+
15+
RUN apt-get update && \
16+
apt-get install --no-install-recommends -y \
17+
curl \
18+
build-essential && \
2219
apt-get clean && \
2320
rm -rf /var/lib/apt/lists/*
2421

25-
# Install poetry
26-
RUN curl -sSL https://install.python-poetry.org | python3 -
22+
RUN --mount=type=cache,target=/root/.cache \
23+
curl -sSL https://install.python-poetry.org | python3 -
2724

28-
# Set the working directory in the container
29-
WORKDIR $PYSETUP_PATH
25+
WORKDIR /opt/pysetup
26+
COPY poetry.lock pyproject.toml ./
3027

31-
# Copy the poetry files
32-
COPY ./poetry.lock ./pyproject.toml ./
28+
RUN --mount=type=cache,target=/root/.cache \
29+
poetry install --no-root --without=dev
3330

34-
# Install project dependencies
35-
RUN poetry install --no-root --no-dev
31+
COPY ./src /opt/pysetup/src
3632

37-
FROM python-base as runtime
38-
39-
# Copy the source code into the container
40-
COPY --from=builder-base $VENV_PATH $VENV_PATH
41-
COPY src/ /app
33+
FROM python-base AS runtime
34+
COPY --from=builder-base /opt/pysetup /opt/pysetup
35+
COPY ./src /app
4236
WORKDIR /app
43-
44-
# Run the bot
4537
CMD ["python", "-u", "main.py"]

0 commit comments

Comments
 (0)