Docker build fails with apk: not found in a Python or Ubuntu image
/bin/sh: 1: apk: not foundShort answer
apk is the package manager of Alpine Linux only. If the build says apk is not found, the image in your FROM line is not Alpine: Debian, Ubuntu and the default or -slim Python images use apt-get. Either switch the install lines to apt-get or change FROM to an -alpine tag.
Fix it fast
1Find out which cause you have
Read the FROM line the failing stage uses. The tag tells you the distribution family.
grep -n -E '^FROM' Dockerfile2Match what you see, then jump to its fix
What does the error look like?
/bin/sh: 1: apk: not found
Cause 1 of 2
The base image is Debian or Ubuntu, but the Dockerfile uses Alpine commands
1Check
The default python tag and the -slim tag are Debian-based. Ubuntu images are Ubuntu. None of them ships apk.
grep -n -E '^FROM|apk ' Dockerfile2Fix
Use apt-get in one RUN instruction: update, install without recommended extras, then clear the package lists.
RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc libpq-dev \
&& rm -rf /var/lib/apt/lists/*3Verify
Rebuild with no cache to prove the new line runs.
docker build --no-cache -t my-app .Confirmed by docs.docker.com
Cause 2 of 2
You wanted Alpine, but the tag does not select it
1Check
Only tags ending in -alpine use Alpine Linux. Alpine uses musl libc, so some software may hit compatibility issues.
grep -n -E '^FROM' Dockerfile2Fix
Select the Alpine variant and keep the apk lines, or stay on Debian and rewrite the lines as apt-get.
FROM python:3.13-alpine
RUN apk add gcc musl-dev3Verify
Build the image again.
docker build -t my-app .Confirmed by hub.docker.com
How do I stop it coming back?
- Decide on one distribution family per Dockerfile, and copy install commands only from guides for that family.
- Use a multi-line RUN that joins apt-get update and install, so the cache layer is never stale.
- Name the image tag in full, so nobody mistakes slim for Alpine.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Servers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
You cannot pip install packages inside an Ubuntu Docker container
error: externally-managed-environment
If pip is missing, install the python3-pip package with apt, in the same RUN as apt-get update.
3 causes, each with a fixServers
pip install psycopg2 fails while building the Docker image
libpq-fe.h: No such file or directory
The plain psycopg2 package compiles a C extension, so the image needs a compiler, the Python headers, the libpq headers and pg_config.
3 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
sam local fails: Running AWS SAM projects locally requires Docker
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
sam local start-api and sam local invoke run your function inside a Docker container, so SAM stops if it cannot talk to Docker.
3 causes, each with a fixServers
dnf fails to download repo metadata in a CentOS 8 Docker build
The same Dockerfile built fine before 2022
CentOS Linux 8 reached end of life on 31 December 2021 and its content left the mirrors.
2 causes, each with a fix