When building the following Dockerfile, a build error occurs during the image creation process. The error indicates that a required system command is not available in the selected base image, causing the build to fail before completion.
How to reproduce it
Dockerfile
# syntax=docker/dockerfile:1
FROM python:3.12
WORKDIR /code
ENV FLASK_APP=./src/app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
Error
=> ERROR [web 3/6] RUN apk add --no-cache gcc musl-dev linux-headers 0.2s
------
> [web 3/6] RUN apk add --no-cache gcc musl-dev linux-headers:
0.152 /bin/sh: 1: apk: not found
------
failed to solve: process "/bin/sh -c apk add --no-cache gcc musl-dev linux-headers" did not complete successfully: exit code: 127
Solution
Use an Alpine-based Python image, which includes the package manager required by the Dockerfile.
# syntax=docker/dockerfile:1
FROM python:3.12-alpine
WORKDIR /code
ENV FLASK_APP=./src/app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]