DEV Community

Cover image for Docker Can Do That ! Part 1: Managing containers on remote machine
Shivam Sandbhor
Shivam Sandbhor

Posted on

Docker Can Do That ! Part 1: Managing containers on remote machine

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The container would be built and ran on the remote machine.

How it works

Docker is composed of 2 components.

  1. Docker daemon: This is responsible for actually building and running the containers.
  2. 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)