Connect to Remote Docker Machines with Docker Context

Docker is a great tool for developing and deploying containerized applications and services. The tool has plenty of options that make it very powerful and flexible enough for nearly every type of workflow and process.
For most developers and admins, Docker is best used from the CLI (Command Line Interface). With the command line, you can do nearly everything you need to get things developed and deployed. And although some would rather opt to use a well-designed GUI (such as my favorite, Portainer) for the process, there are some tricks up Docker’s sleeve that are limited to the CLI-only.
One such tool is Docker Context. What this does is make it possible to export and import contexts from different machines that have Docker installed. The best feature found with Context is the ability to connect to remote Docker instances. That means, if you have multiple Docker nodes, you can easily manage them from a single machine.
One thing to keep in mind is that Context wasn’t introduced until Docker v19.03. So if you’re using a version that predates that, you’ll need to upgrade. To find out which version of Docker you are running, log into your hosting machine and issue the command:
docker -v
If you see any version earlier than 19.03, it’s time to upgrade. On my test machine, I’m running Docker 20.10.14, build a224086.
Docker Context offers four different command options:
- 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
What I want to do is show you how to use Docker Context to connect to a remote Docker instance.
What You’ll Need
To follow along, you’ll need two Docker nodes. I’ll be demonstrating with nodes on IP addresses 192.168.1.5 and 192.168.1.60 but you can use whatever IP address scheme you need. Just remember to change the IP addresses to fit your scheme as you run the commands.
You’ll also need SSH key authentication setup.
Setting up SSH Key Authentication
On 192.168.1.15, issue the command:
1 |
ssh-keygen |
Next, we copy the key to IP address 192.168.1.60 with the command:
1 |
ssh-copy-id 192.168.1.60 |
Connecting to the Second Node
The first thing we’ll do is log into the machine at IP address 192.168.1.15 and check the default contexts. For that, issue the command:
1 |
docker context ls |
You should see something like this in the output:
1 |
default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm |
Let’s set the target host on the first machine by using the Linux export command like so:
1 |
export TARGET_HOST=192.168.1.60 |
The next thing we must do is configure SSH to trust the new host. For that, we’ll use the ssh-keyscan command like so:
1 |
ssh-keyscan -H $TARGET_HOST ~/.ssh/known_hosts |
Before we can actually connect to the remote host, we must first start ssh-agent with the following commands:
1 2 |
eval `ssh-agent -s` ssh-add |
The second command will prompt you for your SSH key authentication password. Once you successfully authenticate, you’re ready to move on.
1 2 3 |
Let's view the running containers on the second node with the command: DOCKER_HOST=ssh://$TARGET_HOST docker container ls |
You should see a list of the running targets on the second node.
Now, we can successfully use Docker Context.
How to Use Docker Context
The first thing we must do is unset our variable with:
1 |
unset DOCKER_HOST |
We can now create a new context like so:
1 |
docker context create node2 --description "Docker Node 2" --docker "host=ssh://$TARGET_HOST" |
You should see something like this in the output:
1 2 |
node2 Successfully created context "node2" |
Awesome.
Now, if you issue the ls command you should see node2 listed in the output. So issue the command:
1 |
docker context ls |
The output should include:
1 |
node2 Docker Node 2 ssh://192.168.1.60 |
Next, we’ll switch to node2 with:
1 |
docker context use node2 |
View all the remote containers found in node2 with:
1 |
docker container ls |
You can add as many contexts as you need and switch between them with the docker context use command.
While using a context, you can even deploy containers to the remote host as you would normally. So when using the node2 context, I could deploy an NGINX container like so:
1 |
docker container run -d -p 80:80 nginx |
List the running containers with:
1 |
docker container ls |
You should see the NGINX container running on node2.
Should you want to remove a context, that can be done like so:
1 |
docker context rm node2 |
If you find you get an error that the context is in use, switch to the default with:
1 |
docker context use default |
You should then be able to remove node2.
And that’s all there is to connect to a remote Docker node with the docker context command.