Maaz Zindani

Loading the index…

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

ssh-keyscan is missing inside your Docker build

Error/bin/sh: ssh-keyscan: not found

Short answer

Slim base images do not include the OpenSSH client, and ssh-keyscan is part of it. Install the client package in the same stage that runs the command: openssh-client with apt-get on Debian or Ubuntu, openssh-client with apk on Alpine. In a multi-stage build, install it in the stage that calls ssh-keyscan, not an earlier one.

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

Fix it fast

  1. 1Find out which cause you have

    Find out which base image the failing stage uses and whether the tool exists there. Run a throwaway container from the same image.

    docker run --rm IMAGE_NAME sh -c 'command -v ssh-keyscan || echo missing'
  2. 2Match what you see, then jump to its fix

    1. 1ssh-keyscan: command not found in a stage that starts FROM debian, ubuntu or a node/python image based on themDebian or Ubuntu base image without the OpenSSH clientFix
    2. 2/bin/sh: ssh-keyscan: not found (the shell wording of Alpine's busybox sh)Alpine base image without the OpenSSH clientFix
    3. 3ssh-keyscan works in the builder stage but fails in the final stageThe tool was installed in a different build stageFix

What does the error look like?

  • /bin/sh: ssh-keyscan: not found
  • ssh-keyscan: command not found

Cause 1 of 3

Debian or Ubuntu base image without the OpenSSH client

1Check

The Debian package openssh-client ships ssh-keyscan together with ssh, scp and sftp. Check whether it is installed in the image.

docker run --rm IMAGE_NAME dpkg -s openssh-client

2Fix

Install the package in the Dockerfile. Keep apt-get update and apt-get install in one RUN so the package index is never stale.

RUN apt-get update && apt-get install -y --no-install-recommends openssh-client \
    && rm -rf /var/lib/apt/lists/*

3Verify

Rebuild and run the command that failed.

docker build -t my-app .

Confirmed by packages.debian.org

Did this fix it?

Cause 2 of 3

Alpine base image without the OpenSSH client

1Check

On Alpine the client is split into packages. The ssh-keyscan binary is listed in the openssh-client-common package, and the openssh-client name installs the client with it. Check whether the command exists in the image.

docker run --rm IMAGE_NAME sh -c 'command -v ssh-keyscan || echo missing'

2Fix

Add the client with apk. apk add downloads the package from the repositories listed in /etc/apk/repositories.

RUN apk add --no-cache openssh-client

3Verify

Run the tool once to confirm it is on the PATH.

docker run --rm my-app sh -c 'command -v ssh-keyscan'

Confirmed by pkgs.alpinelinux.org

Did this fix it?

Cause 3 of 3

The tool was installed in a different build stage

1Check

Every FROM starts a new stage from its own base image. Packages installed in an earlier stage are not there unless that stage is used as the base. Look at which stage the failing RUN belongs to.

grep -n -E '^(FROM|RUN).*(ssh-keyscan|openssh)' Dockerfile

2Fix

Install the client in the stage that runs ssh-keyscan. If you only need the known_hosts file in the final image, generate it in the builder stage and copy that file across.

FROM debian:stable-slim AS keys
RUN apt-get update && apt-get install -y --no-install-recommends openssh-client
RUN mkdir -p /out && ssh-keyscan -t ed25519 github.com > /out/known_hosts

FROM debian:stable-slim
COPY --from=keys /out/known_hosts /root/.ssh/known_hosts

3Verify

Build the whole file, not only the first stage.

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

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • List every command-line tool a RUN step uses in the same stage as an explicit package install.
  • Check host keys against the provider's published fingerprints; ssh-keyscan cannot verify the keys it fetches.
  • Keep apt-get update and apt-get install together in one RUN.

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.