Prior to Docker v19.03, we could use the
DOCKER_HOST environment variable to configure and connect to remote Docker hosts. Since Docker v19.03, Docker’s command line interface (CLI) has added a subcommand: context, which is used to manage docker clients connecting to multiple contexts.
The context command allows you to connect and manage multiple Docker hosts remotely by configuring contexts for the SSH protocol, as well as exporting a context environment from a machine with the Docker CLI installed and importing it on another machine with the Docker CLI installed.
You can first view the subcommands supported by the command via the
This will demonstrate how to connect to a remote Docker host using the
DOCKER_HOST environment variable, as well as the
Prepare in advance
First we need to prepare two Docker hosts and install Docker v19.03+ version, for example here
- 192.168.0.110(linux-dev) - my local working host, Docker version 20.10.12-ce
- 192.168.0.200(home-boxsrv) - remote Docker host, Docker version 20.10.7
To avoid entering the SSH password, please configure the password-free access to the remote Docker host from the Docker client host in advance.
Using the DOCKER_HOST environment variable to connect to a remote Docker host
First we run a container on a remote Docker host (home-boxsrv), for example a container named
Configure environment variables on the local host (linux-dev)
Then view the container
We can see that the containers running on the remote host are listed.
Using the context command
First we clear the environment variables configured above on the local host (linux-dev)
context ls command to list the currently configured contexts for the client.
As you can see, there is currently an environment named
default that is connected to the Docker engine on the local machine.
Now, let’s add the context environment for the connection (home-boxsrv) with the
context create command.
The environment named
home-boxsrv has been successfully added, but the
default environment is still active and we need to set the current environment with the
context use command.
Now let’s take a look using
The containers listed are on the remote host (home-boxsrv), if we use the
docker info command to view them, the server will be the remote host’s information.
In addition to the SSH protocol endpoint method, if the remote host exposes the docker endpoint via tcp, then we can also use the tcp endpoint method, such as the following environment named
context command, we can easily connect and switch to manage multiple Docker host environments on a single Docker client host, which greatly improves the efficiency of operating and maintaining multiple host environments, and can easily export and import the managed Docker context environment to other Docker clients. For more information on how to use the command, please refer to the official documentation.