A Single Docker CLI is powerful enough to manage multiple Docker nodes. Before Docker v19.03 release, managing the multiple Docker hosts from a single node was a cumbersome task. The only option available was to use
DOCKER_HOST variable to identify a remote host to connect to.
With the release of Docker v19.03, a new
docker context CLI was introduced for the first time. The
docker context command makes it easy to export and import contexts on different machines with the Docker client installed. You can use the docker context export command to export an existing context to a file. This file can later be imported on another machine that has the docker client installed.
$ docker context --help 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.
In this blog tutorial, you will see how one can use docker context command to access the remote Docker hosts seamlessly. Before that, let us look how can one achieve this without even using
docker context command
- Install Docker 19.03+ on Node #1 ( 192.168.0.7)
- Install Docker 19.03+ on Node #2 (192.168.0.8)
Listing the current context values
Login to Node 1 and check the current context value:
$ docker context ls NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm
We should see the default context, pointing to the socket file
Run a new Docker container on Node 2
Open Node 2 and run the Docker container
$ docker container run -d --name node2 ajeetraina/hellowhale tail -f /dev/null
Listing the container
$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1311ecf00b84 ajeetraina/hellowhale "tail -f /dev/null" About a minute ago Up About a minute 80/tcp node2 [node2] (local) firstname.lastname@example.org ~ $
Setting the Environment Variable
You’ll need to copy down the IP address of the target node(i.e. Node 2) and set it on the first terminal.
Verify you configured variable
$ printenv TARGET_HOST 192.168.0.8
Configure SSH to trust the host
ssh-keyscan -H $TARGET_HOST >~/.ssh/known_hosts
Connecting to Nodes with DOCKER_HOST
Docker will use the
DOCKER_HOST variable to identify a remote host to connect to. Let’s compare what happens when listing containers locally and on a remote host.
DOCKER_HOST=ssh://$TGT_HOST docker container ls
This will list the containers running on the target node.
Using Docker Context
Unset the environment variable
With the release of 19.03, docker now supports managing the context within the CLI. Note that this context is overridden by the
DOCKER_HOST variable, so to use it, you’ll need to be sure that isn’t set(as shown above).
Create a new context
$ docker context create node2 --description "Node 2" --docker "host=ssh://$TARGET_HOST" node2 Successfully created context "node2"
Listing the Context
$ docker context ls NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm node2 Node 2 ssh://192.168.0.8
Switching to the new context
docker context use node2
Listing the overall context
$ docker context ls NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR default Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm node2 * Node-2 ssh://192.168.0.7
Listing the remote containers
$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9ee527be530 ajeetraina/hellowhale "tail -f /dev/null" 27 seconds ago Up 27 seconds 80/tcp node2