Utilizing SSH agent in Docker Compose and Dockerfile

It is recommended to consistently use the syntax when mentioning environment variables to avoid any potential inconsistencies that may lead to unforeseen issues in other contexts.


Solution:

There is an error in the syntax of

environment

. Instead of that, the

environment

block can be provided in the form of a list, which should consist of

NAME=VALUE

pairs.

environment:
  - SSH_AUTH_SOCK=${SSH_AUTH_SOCK}

Or it can be a dictionary:

environment:
  SSH_AUTH_SOCK: ${SSH_AUTH_SOCK}

As your container does not possess

SSH_AUTH_SOCK

characteristics, it lacks the corresponding environment variable.

When utilizing the document labeled as

docker-compose.yaml

,

version: "3"
services:
  ssh:
    image: fedora:35
    environment:
      - SSH_AUTH_SOCK=${SSH_AUTH_SOCK}
    volumes:
      - ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK}
    command:
      - sh
      - -c
      - |
        yum -y install openssh-clients
        sleep inf

After the package installation is completed, I can insert

exec

into the container and confirm that it can communicate with my agent.

$ docker-compose exec ssh ssh-add -l
2048 SHA256:... (RSA)
4096 SHA256:... (RSA)

Additionally, I have an unrelated remark regarding your

volumes:

block. It appears that you are displaying inconsistency in your variable references. Although this may not pose a significant issue, it can be confusing and may result in unexpected difficulties in other situations. To avoid such scenarios, it is advisable to consistently utilize the

${varname}

syntax when referring to environment variables.

volumes:
  - ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK}

Frequently Asked Questions