Maaz Zindani

Loading the index…

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

apt-get is not found while building a Docker image

Error/bin/sh: apt-get: not found

Short answer

apt-get belongs to Debian and Ubuntu. If the build says it is not found, your base image is a different system, most often Alpine, whose package tool is apk. Check the image's operating system, then use the command that image has. A minimal image may have no package tool at all.

  • Applies to Docker Engine 28, Alpine Linux 3.22, BuildKit
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the operating system of the base image in your FROM line. Its name decides which package command exists.

    docker run --rm alpine:3.22 cat /etc/os-release
  2. 2Match what you see, then jump to its fix

    1. 1FROM line has alpine or a -alpine tag, for example node:22-alpineThe base image is Alpine, which uses apkFix
    2. 2FROM scratch, or a later build stage that starts from a minimal baseThe final image is a minimal one that holds only your programFix

What does the error look like?

  • /bin/sh: apt-get: not found

Cause 1 of 2

The base image is Alpine, which uses apk

1Check

Alpine's package manager is apk. Check the FROM line of the Dockerfile.

grep -n "^FROM" Dockerfile

2Fix

Replace the apt-get commands with apk. apk add downloads packages from the repositories listed in /etc/apk/repositories. Alpine package names can differ from Debian names, so search for each one.

RUN apk update && apk add bash curl

3Verify

Rebuild the image without cache and watch the step finish.

docker build --no-cache -t my-app .

Confirmed by docs.alpinelinux.org

Did this fix it?

Cause 2 of 2

The final image is a minimal one that holds only your program

1Check

The scratch base is used to create minimal images with only what an application needs, and the next Dockerfile command becomes its first filesystem layer. Nothing is there to install packages with.

grep -n "^FROM" Dockerfile

2Fix

Do the installs in an earlier stage that uses a full base image, then copy the finished files into the minimal stage.

FROM debian:bookworm AS build
RUN apt-get update && apt-get install -y build-essential
COPY . /src
RUN make -C /src

FROM scratch
COPY --from=build /src/my-program /my-program

3Verify

Rebuild; the install step runs in the full stage and the final image builds.

docker build -t my-app .

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • Pin the base image tag in FROM and write install commands for that exact system.
  • Keep installs in the stage that has a package manager, and copy only results forward.
  • Search the package index of the distribution you use before copying a package name from a tutorial.

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

Servers

A dev container port does not open in the browser when Docker runs on a remote host

If your application only accepts calls from localhost, it will reject connections from published ports

Three things usually break it: the app listens only on localhost inside the container, so published ports are rejected; you open the container port instead of the mapped localhost port VS Code shows; or you changed appPort or a Compose ports list and did not rebuild.

3 causes, each with a fix

Next step

Want this handled for good?

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