This is a series of short articles illustrating less known capabilities of docker.
Using this feature docker allows you to build, run, delete containers and pretty much everything else on a remote machine.
Prerequisite:
A remote instance having docker installed with SSH access.
Steps:
Adding local machine's public ssh key to remote machine's authorized keys.
This is required so that the remote machine correctly verifies your ssh connection and doesn't allow random people to run docker containers on your machine.
Generate a ssh key on your local machine using:
ssh-keygen
For simplicity don't provide any passphrase and save the key at default location (~/.ssh/id_rsa
)
Your public key would be at ~/.ssh/id_rsa.pub
and the private key at ~/.ssh/id_rsa
. NEVER SHARE YOUR PRIVATE KEY.
Now append the contents of ~/.ssh/id_rsa.pub
at the remote machine's ~/.ssh/authorized_keys
, separated by new line.
If everything goes right, you should be able to access remote machine from local machine by simply doing:
ssh <remote_user>@<remote_ip>
Running docker commands from local machine on remote machine
You can now run docker commands like this
docker -H ssh://<remote_user>@<remote_ip> run hello-world
The container would be built and ran on the remote machine.
How it works
Docker is composed of 2 components.
- Docker daemon: This is responsible for actually building and running the containers.
- Docker client: More commonly this the docker-cli, the command line tool is just one way of interacting with docker daemon. You can use programming languages to build containers too, which directly interact with the daemon.
So in our case we are using docker daemon of a remote machine and interacting with it from local machine. The local machine
uses ssh to access the daemon.
Please share in the comment sections other less known features of docker. I would be glad to learn more about docker.
Top comments (0)