Working With Remote Docker Using Docker Context

tech · Dec 2, 2021 · ~2 min
Photo by @carrier_lost on Unsplash
Photo by @carrier_lost on Unsplash

Introduction

This is a cheatsheet for working with docker context to connect remote docker locally. Might help you to work with remote docker without manually SSH to the remote server.

Add Context

1
2
3
$ docker context create my-remote-docker-machine --docker "host=ssh://username@host"
my-remote-docker-machine
Successfully created context "my-remote-docker-machine"

You can also utilize SSH Config file to connect to the remote docker. Especially when you need to define your private key or password.

1
2
3
4
5
$ cat ~/.ssh/config 
Host my-remote-docker-machine
  Hostname host
  User username
$ docker context create my-remote-docker-machine --docker "host=ssh://my-remote-docker-machine"

Besides ssh, you can also add your context using tcp protocol if you enable the Docker API.

List All Context

1
2
3
4
$ docker context ls
NAME                       DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT   ORCHESTRATOR
default *                  Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                         swarm
my-remote-docker-machine                                             ssh://username@host

Use Context

1
2
3
4
5
6
7
8
$ docker --context my-remote-docker-machine images -q
65dadc9c7fe7
f814fce55133
7a9b6da4328e
33655f17f093
d120da10b040
6d6859d1a42a
c19ae228f069

Set New Context as Default

1
2
3
4
5
6
7
8
$ docker context use my-remote-docker-machine 
my-remote-docker-machine
Current context is now "my-remote-docker-machine"
$ docker context ls
NAME                         DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT   ORCHESTRATOR
default                      Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                         swarm
my-remote-docker-machine *                                             ssh://username@host

See the * is moved from default to my-remote-docker-machine. Now you can use your docker command without --context flag.

Remove Context

1
2
3
$ docker context use default # back to default
$ docker context rm my-remote-docker-machine 
my-remote-docker-machine

Conclusion

Using docker context might help to avoid SSH manually to the remote server. But, when it comes to build an image using a remote docker locally, you need to consider how much docker context that will be uploaded/downloaded.

Thank you for reading!

· · ·

Love This Content?

Any kind of supports is greatly appreciated! Kindly support me via Bitcoin, Ko-fi, Trakteer, or just continue to read another content. You can write a response via Webmention and let me know the URL via Telegraph.

Drop Your Comment Below