Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Docker build fails with apk: not found in a Python or Ubuntu image

Error/bin/sh: 1: apk: not found

Short 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.

  • Applies to Docker Engine, python:3 images, Ubuntu base images
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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' Dockerfile
  2. 2Match what you see, then jump to its fix

    1. 1FROM ubuntu, FROM python:3.13 or FROM python:3.13-slim, with RUN apk add linesThe base image is Debian or Ubuntu, but the Dockerfile uses Alpine commandsFix
    2. 2The Dockerfile was written for Alpine, yet FROM names a tag without alpine in itYou wanted Alpine, but the tag does not select itFix

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 ' Dockerfile

2Fix

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

Did this fix it?

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' Dockerfile

2Fix

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-dev

3Verify

Build the image again.

docker build -t my-app .

Confirmed by hub.docker.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.