Maaz Zindani

Loading the index…

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

A docker context pointing at ssh:// stops with Host key verification failed

Your error

Host key verification failed.1 more line it can show

Fix it fast

Most common cause, 1 of 3

The remote host has never been trusted by this account

Open a normal terminal and connect once with plain ssh, using the same user and host as the context.

Fix
# on the server: print its key fingerprint
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub

# on your machine: connect once and confirm
ssh docker-user@host1.example.com

# CI alternative, after checking the fingerprint
ssh-keyscan host1.example.com >> ~/.ssh/known_hosts
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Host key verification failed from a docker context or DOCKER_HOST=ssh:// means the local ssh client has no trusted key for that host. Docker starts ssh without a terminal, so the yes/no prompt never appears. Connect once with plain ssh as the same user, host and port, or add a checked key with ssh-keyscan. Applies to Docker 29 and OpenSSH 10.

  • Applies to Docker Engine and CLI 29.10, OpenSSH 10.6
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Read the exact endpoint the context uses, then look that name up in the known_hosts file of the account that runs docker. Use the bracket form when the endpoint carries a port other than 22.

Run this first
docker context ls
ssh-keygen -F host1.example.com
ssh-keygen -F '[host1.example.com]:2222'

Then find the line you see in this list and open its fix:

  1. 1The remote host has never been trusted by this accountssh-keygen -F prints nothing for the endpoint's host name
  2. 2The context uses a port or a name that differs from the saved entrydocker context ls shows ssh://user@host:2222 or an IP address, while known_hosts only holds the plain host name
  3. 3docker runs as another account, such as root through sudoPlain ssh from your shell connects without a prompt, yet sudo docker, a systemd unit or a CI runner user gets the error

What does the error look like?

  • Host key verification failed.
  • make sure the URL is valid, and Docker 18.09 or later is installed on the remote host: stderr=Host key verification failed.

Cause 1 of 3

The remote host has never been trusted by this account

You seessh-keygen -F prints nothing for the endpoint's host name

Check

When docker reports Host key verification failed for a host you have never reached by ssh, the key simply is not in known_hosts yet. The Docker CLI launches ssh in a new session with no controlling terminal, and with the default StrictHostKeyChecking ask, a new key is only saved after someone confirms it. Nobody can, so ssh refuses.

Check
ssh-keygen -F host1.example.com || echo 'no entry'

Fix

Open a normal terminal and connect once with plain ssh, using the same user and host as the context. Compare the fingerprint ssh shows with the one printed on the server, and type yes only if they match. In a CI job with no terminal, write the scanned key into known_hosts after checking it the same way.

Fix
# on the server: print its key fingerprint
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub

# on your machine: connect once and confirm
ssh docker-user@host1.example.com

# CI alternative, after checking the fingerprint
ssh-keyscan host1.example.com >> ~/.ssh/known_hosts

Verify

docker info through the context prints the remote engine's details.

Verify
docker --context my-remote-engine info

Confirmed by man.openbsd.org

Did this fix it?

Cause 2 of 3

The context uses a port or a name that differs from the saved entry

You seedocker context ls shows ssh://user@host:2222 or an IP address, while known_hosts only holds the plain host name

Check

A known_hosts line matches by the name exactly as typed. For a port other than 22 the entry has to read [host]:port, so a key saved by ssh on port 22, or under the DNS name while the context uses the IP, does not cover the context's endpoint.

Check
ssh-keygen -F '[host1.example.com]:2222'

Fix

Add the key under the same name and port the context uses: connect once with ssh -p, or scan that port. Changing the context to the name you already trust works too.

Fix
ssh -p 2222 docker-user@host1.example.com
# or, after checking the fingerprint
ssh-keyscan -p 2222 host1.example.com >> ~/.ssh/known_hosts

Verify

ssh-keygen -F with the bracket form now lists one or more lines, and the docker command succeeds.

Verify
ssh-keygen -F '[host1.example.com]:2222'

Confirmed by man.openbsd.org

Did this fix it?

Cause 3 of 3

docker runs as another account, such as root through sudo

You seePlain ssh from your shell connects without a prompt, yet sudo docker, a systemd unit or a CI runner user gets the error

Check

The connection helper starts the local ssh binary as the user who ran docker. ssh then reads that user's ~/.ssh/known_hosts, so a key you accepted as yourself is unknown to root or to a service account.

Check
sudo ssh-keygen -F host1.example.com -f /root/.ssh/known_hosts

Fix

Run docker as the account that already trusts the host, without sudo. If the job has to run as another account, add the checked key to that account's known_hosts or point UserKnownHostsFile at a shared file in its ssh config. That account also needs its own login key for the server.

Fix
# trust the host for root as well
sudo ssh docker-user@host1.example.com

Verify

The same command, run the way the job runs it, connects to the remote engine.

Verify
sudo docker --context my-remote-engine info

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Store the server's host key in the repository or CI secret store and write it to known_hosts before the first docker command, instead of scanning it fresh on every run.
  • Create contexts with the same host name and port you use for plain ssh, or add a Host block in ~/.ssh/config and point the context at that alias.
  • When a server is rebuilt, remove its old line with ssh-keygen -R and record the new fingerprint at the same time.

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 on the same stack: TaxiDex, Dispatch Pro.

Next step

Want this handled for good?

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