TNS
VOXPOP
What news from AWS re:Invent last week will have the most impact on you?
Amazon Q, an AI chatbot for explaining how AWS works.
0%
Super-fast S3 Express storage.
0%
New Graviton 4 processor instances.
0%
Emily Freeman leaving AWS.
0%
I don't use AWS, so none of this will affect me.
0%
Containers

Connect to Remote Docker Machines with Docker Context

Contexts make it easy for a single Docker CLI to manage multiple Swarm clusters, multiple Kubernetes clusters, and multiple individual Docker nodes.
Nov 12th, 2022 6:00am by
Featued image for: 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:


Next, we copy the key to IP address 192.168.1.60 with the command:

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:


You should see something like this in the output:


Let’s set the target host on the first machine by using the Linux export command like so:


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:


Before we can actually connect to the remote host, we must first start ssh-agent with the following commands:


The second command will prompt you for your SSH key authentication password. Once you successfully authenticate, you’re ready to move on.


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:


We can now create a new context like so:


You should see something like this in the output:


Awesome.

Now, if you issue the ls command you should see node2 listed in the output. So issue the command:


The output should include:


Next, we’ll switch to node2 with:


View all the remote containers found in node2 with:


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:


List the running containers with:


You should see the NGINX container running on node2.

Should you want to remove a context, that can be done like so:


If you find you get an error that the context is in use, switch to the default with:


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.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Docker.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.