Docker+Magento: Failed to add the host to the list of known hosts

Docker+Magento: Failed to add the host to the list of known hosts

October 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Docker+Magento: Failed to add the host to the list of known hosts

Solving the SSH Known Hosts Issue for Docker+Magento

The error you’re encountering is due to Docker’s isolated environment not recognizing your SSH key as authorized. This happens because when you run commands inside a Docker container, it operates in its own sandboxed environment separate from your host system.

Understanding the Problem: Isolated Docker Environment

When you execute bin/cli git clone git@github.com:magento/magento2.git within the container, it doesn’t have access to the SSH keys stored on your local machine. Instead, it expects an SSH key to be present in /var/www/.ssh/id_rsa inside the container itself.

The .ssh/id_rsa file is configured in the docker-compose.yml file under the app service with a volume mapping:

- ~/.ssh/id_rsa:/var/www/.ssh/id_rsa:cached

This means it should copy your local .ssh/id_rsa key to the container’s /var/www/.ssh/id_rsa. However, this mapping is not working as expected due to either caching issues or permission problems.

Resolving the Issue: Providing Valid SSH Keys

To fix this, you need to ensure that:

  1. The .ssh/id_rsa key exists on your local machine at ~/.ssh/id_rsa.

    If it doesn’t exist, generate a new key pair using:

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  2. The .ssh/known_hosts file is correctly configured on your local machine.

    The known_hosts file should contain the host key (SHA-256 fingerprints) for github.com. You can add it manually or run:

    ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
    
  3. The permissions of both .ssh/id_rsa and .ssh/known_hosts are set to 600.

    Use the following command to ensure these files have proper permissions:

    chmod 600 ~/.ssh/id_rsa
    chmod 644 ~/.ssh/known_hosts
    

Verifying the Docker Volumes

To double-check if your volumes are being mapped correctly, run the following commands inside your container:

docker-compose exec app ls -la /var/www/.ssh
cat /var/www/.ssh/id_rsa.pub

This should display the contents of your SSH key and verify that it’s being copied into the container as expected.

Conclusion

The root cause of this issue lies in the interaction between Docker containers (which operate in isolated environments) and the host system. By ensuring you have a valid SSH key pair on your local machine, configuring the known_hosts file properly, and setting the correct permissions, you can resolve this problem.

Remember to always keep your SSH keys secure by protecting them with strong passphrases and avoiding sharing them publicly. For more detailed information about managing SSH keys in a safe manner, I recommend checking out our guide on SSH key best practices at Laravel Company.

Happy coding! Let me know if you have any other questions.

Magento 2 Cache Security Configuration

Our network