Ajeet Raina Docker Captain, ARM Innovator & Docker Bangalore Community Leader.

How to Connect to Remote Docker using docker context CLI

2 min read

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

Pre-requisite:

  • 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 /var/run/docker.sock.

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) root@192.168.0.7 ~
$ 

 

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.

export TARGET_HOST=192.168.0.8

 

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

unset DOCKER_HOST

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

 

References:

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Docker Captain, ARM Innovator & Docker Bangalore Community Leader.

51 Replies to “How to Connect to Remote Docker using docker context…”

  1. Yoս’re so awesome! I don’t suppose I have read through
    ɑ single thing like that beforе. Տo nice to ԁiscover someone
    witһ some unique thoսghts օn this suЬject.
    Տeriously.. tһanks for starting this up. This website is ⲟne thіng that is rеquired
    on the internet, someone with some originality!

  2. I will right away take hold of your rss feed as I can not to find your e-mail subscription hyperlink or newsletter service. Do you have any? Kindly allow me recognize so that I could subscribe. Thanks.

  3. It’s a pity you don’t have a donate button! I’d without a doubt donate to this brilliant blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to new updates and will talk about this blog with my Facebook group. Talk soon!

  4. With havin so much content do you ever run into any problems of plagorism or copyright infringement? My site has a lot of completely unique content I’ve either created myself or outsourced but it looks like a lot of it is popping it up all over the web without my agreement. Do you know any solutions to help protect against content from being stolen? I’d really appreciate it.

  5. Great – I should certainly pronounce, impressed with your web site. I had no trouble navigating through all the tabs as well as related information ended up being truly easy to do to access. I recently found what I hoped for before you know it at all. Quite unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your client to communicate. Nice task..

  6. Great – I should certainly pronounce, impressed with your site. I had no trouble navigating through all the tabs and related info ended up being truly easy to do to access. I recently found what I hoped for before you know it at all. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, website theme . a tones way for your client to communicate. Excellent task..

  7. Thanks a lot for sharing this with all of us you really know what you are talking about! Bookmarked. Please also visit my web site =). We could have a link exchange contract between us!

  8. I would like to thnkx for the efforts you have put in writing this blog. I am hoping the same high-grade blog post from you in the upcoming as well. In fact your creative writing abilities has inspired me to get my own blog now. Really the blogging is spreading its wings quickly. Your write up is a good example of it.

  9. Hi my friend! I wish to say that this article is amazing, great written and come with approximately all vital infos. I would like to look extra posts like this .

  10. Simply wish to say your article is as surprising. The clearness for your publish is just excellent and that i can suppose you’re knowledgeable on this subject. Well with your permission allow me to clutch your RSS feed to stay up to date with impending post. Thank you one million and please carry on the rewarding work.

  11. of course like your web-site however you have to take a look at the spelling on several of your posts. Many of them are rife with spelling problems and I in finding it very troublesome to tell the reality then again I¦ll certainly come again again.

  12. I have been browsing on-line greater than 3 hours lately, yet I never found any fascinating article like yours. It’s lovely price enough for me. In my view, if all web owners and bloggers made just right content material as you probably did, the internet will probably be a lot more useful than ever before. “When the heart speaks, the mind finds it indecent to object.” by Milan Kundera.

  13. Hi! Quick question that’s completely off topic. Do you know how to make your site mobile friendly? My web site looks weird when browsing from my iphone 4. I’m trying to find a theme or plugin that might be able to resolve this issue. If you have any recommendations, please share. Thank you!

  14. There are some attention-grabbing points in time on this article but I don’t know if I see all of them center to heart. There’s some validity however I’ll take maintain opinion till I look into it further. Good article , thanks and we wish more! Added to FeedBurner as well

  15. Hi , I do believe this is an excellent blog. I stumbled upon it on Yahoo , i will come back once again. Money and freedom is the best way to change, may you be rich and help other people.

  16. Throughout the grand pattern of things you’ll secure an A just for effort. Exactly where you actually misplaced me personally ended up being on the particulars. You know, as the maxim goes, the devil is in the details… And it couldn’t be more true in this article. Having said that, permit me say to you what did do the job. Your authoring is actually incredibly convincing and that is most likely the reason why I am making the effort in order to opine. I do not make it a regular habit of doing that. Second, although I can easily notice a jumps in logic you make, I am not really certain of how you appear to unite the details that make the conclusion. For the moment I will subscribe to your issue however trust in the future you actually link the dots much better.

  17. What i do not understood is actually how you’re not really a lot more neatly-appreciated than you may be now. You are very intelligent. You know thus significantly in the case of this subject, produced me in my view imagine it from a lot of numerous angles. Its like women and men don’t seem to be interested except it is something to do with Woman gaga! Your own stuffs outstanding. Always care for it up!

  18. Hey very nice site!! Man .. Beautiful .. Amazing .. I will bookmark your blog and take the feeds also…I’m happy to find so many useful information here in the post, we need work out more strategies in this regard, thanks for sharing. . . . . .

  19. Wow, marvelous blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is magnificent, as well as the content!

  20. An fascinating dialogue is value comment. I feel that you must write more on this matter, it might not be a taboo subject but typically persons are not sufficient to talk on such topics. To the next. Cheers

  21. Thanks for another excellent post. The place else may just anyone get that kind of info in such an ideal approach of writing? I have a presentation next week, and I’m on the search for such info.

  22. Usually I do not read post on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing style has been surprised me. Thanks, very nice article.

Leave a Reply

Your email address will not be published.