Managing remote docker instances

This is a short recipe to use new context feature in docker to create & switch between docker contexts, so you can run any docker command remotely without need to establish a SSH connection (it’s done automatically by the docker context).

Prerequisites

Docker version

You need a relative modern docker version, to check if your installed docker version supports it, just run:

$ docker context

Usage:	docker context COMMAND

Manage contexts

Commands:
  create      Create a context
  export      Export a context to a tar or kubeconfig file
  import      Import a context from a tar or zip file
  inspect     Display detailed information on one or more contexts
  ls          List contexts
  rm          Remove one or more contexts
  update      Update a context
  use         Set the current docker context

Run 'docker context COMMAND --help' for more information on a
command.

If all is OK you will see the help message, if not you will see an error message complaining about not existing docker command.

Public SSH key

You also need to send your public SSH key to the remote host, in order to allow a SSH connection from your local docker client to the remote docker server (replace user & IP):

$ ssh-copy-id emilio@192.168.1.18

You will need to accept the remote host key (if it’s the first time you connect to that server) and type the remote password.

Then you can try to log in without introducing the password:

$ ssh emilio@192.168.1.18

Creating & using docker contexts

Creating the context

To create a context, just run (we will call it eht, you can use the name you want):

$ docker context create eht --docker \
         "host=ssh://emilio@192.168.1.18"
eht
Successfully created context "eht"

Listing & switch contexts

The context is created (but not in use yet), you can list your contexts (including the default one pointing to local docker server) with this command:

$ docker context ls
NAME                DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT   ORCHESTRATOR
default *           Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                         swarm
eht                                                           ssh://emilio@192.168.1.18                           

The selected context is marked with an asterisk after the context name.

To switch between context just run:

$ docker context use eht
eht
Current context is now "eht"

To check is working properly, run any docker command to check which docker server you are connected with, in my case I want to list the swarm nodes, because my selected docker context is the eht cluster:

$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2tusd451dit99xa4rqakio7bk     docker-01           Ready               Active              Reachable           19.03.8
bjpao3x71d2vxfd0ale7yzvqg     docker-02           Ready               Active              Reachable           19.03.8
bimgyeacu85cdcc6h77vzqech     docker-03           Down                Drain                                   19.03.6
lgmhp6uq54mc52hhnqkvbbzt4 *   docker-04           Ready               Active              Leader              19.03.8
vvgq04cxfojaam59za35o6f7v     rasp-01             Ready               Active                                  19.03.8
9xd2s0e4lv4xese3yieol54d0     rasp-02             Ready               Active                                  19.03.8
iqyqky61f04uoh3na6e8qbm0c     rasp-03             Ready               Active                                  19.03.8
juwcoql2no9ilj6479wevn88m     rasp-04             Ready               Active                                  19.03.8

References

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.