When we want to run a container on a remote Docker Engine host, we can utilize the context feature of Docker. Context allows us to maintain information of several Docker Engine hosts to be remotely accessed from our local Docker Engine host. Adding the record is done by running the following command.
docker context create yourContextName --docker "host=ssh://user@remote.host"
The connection utilizes SSH protocol so that we need to generate keys for establishing communication with the remote host. After storing the public key value on the remote host, we can spawn a new SSH agent on the current session on our host and add the private key into the agent.
eval $(ssh-agent -s)
cat /path/to/private/key | ssh-add -
Before we can access the remote Docker API, we need to add the remote keys information to our ~/.ssh/known_hosts
file by making an SSH connection for the first time or using ssh-keyscan
.
Now, we can access remote Docker API by specifying the context on the local docker
command.
docker --context yourContextName ps
Or, we can set the context value permanently for the active session.
docker context use yourContextName
docker ps
The latest version of Docker Compose has also supported this mechanism.
docker-compose --context yourContextName up -d
Comments
Post a Comment