Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject host's SSH keys into Docker Machine with Docker Compose

I am using Docker on Mac OS X with Docker Machine (with the default boot2docker machine), and I use docker-compose to setup my development environment.

Let's say that one of the containers is called "stack". Now what I want to do is call:

docker-composer run stack ssh [email protected] 

My public key (which has been added to stackoverflow.com and which will be used to authenticate me) is located on the host machine. I want this key to be available to the Docker Machine container so that I will be able to authenticate myself against stackoverflow using that key from within the container. Preferably without physically copying my key to Docker Machine.

Is there any way to do this? Also, if my key is password protected, is there any way to unlock it once so after every injection I will not have to manually enter the password?

like image 275
Ruslan Avatar asked Jan 21 '16 19:01

Ruslan


People also ask

How do I forward SSH agent to Docker container?

To shared the SSH agent between your host machine and your docker container all you need to do is set an environment variable and a volume mount in your docker setup. In the container, we're setting the environment variable SSH_AUTH_SOCK to the path /ssh-agent .

How do I copy a Dockerfile SSH key?

In order to inject you ssh key, within a container, you have multiple solutions: Using a Dockerfile with the ADD instruction, you can inject it during your build process. Simply doing something like cat id_rsa | docker run -i <image> sh -c 'cat > /root/. ssh/id_rsa'


2 Answers

You can add this to your docker-compose.yml (assuming your user inside container is root):

volumes:     - ~/.ssh:/root/.ssh 

Also you can check for more advanced solution with ssh agent (I did not tried it myself)

like image 78
Anton Serdyuk Avatar answered Sep 22 '22 08:09

Anton Serdyuk


WARNING: This feature seems to have limited support in Docker Compose and is more designed for Docker Swarm.

(I haven't checked to make sure, but) My current impression is that:

  • In Docker Compose secrets are just bind mount volumes, so there's no additional security compared to volumes
  • Ability to change secrets permissions with Linux host may be limited

See answer comments for more details.


Docker has a feature called secrets, which can be helpful here. To use it one could add the following code to docker-compose.yml:

--- version: '3.1' # Note the minimum file version for this feature to work services:   stack:     ...     secrets:       - host_ssh_key  secrets:   host_ssh_key:     file: ~/.ssh/id_rsa 

Then the new secret file can be accessed in Dockerfile like this:

RUN mkdir ~/.ssh && ln -s /run/secrets/host_ssh_key ~/.ssh/id_rsa 

Secret files won't be copied into container:

When you grant a newly-created or running service access to a secret, the decrypted secret is mounted into the container in an in-memory filesystem

For more details please refer to:

  • https://docs.docker.com/engine/swarm/secrets/
  • https://docs.docker.com/compose/compose-file/compose-file-v3/#secrets
like image 39
Anton Styagun Avatar answered Sep 21 '22 08:09

Anton Styagun