DEV Community

Clavin June
Clavin June

Posted on • Originally published at clavinjune.dev on

3 1

Working With Remote Docker Using Docker Context

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

$ docker context create my-remote-docker-machine --docker "host=ssh://username@host"
my-remote-docker-machine
Successfully created context "my-remote-docker-machine"
Enter fullscreen mode Exit fullscreen mode

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

$ 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"
Enter fullscreen mode Exit fullscreen mode

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

List All Context

$ 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
Enter fullscreen mode Exit fullscreen mode

Use Context

$ docker --context my-remote-docker-machine images -q
65dadc9c7fe7
f814fce55133
7a9b6da4328e
33655f17f093
d120da10b040
6d6859d1a42a
c19ae228f069
Enter fullscreen mode Exit fullscreen mode

Set New Context as Default

$ 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

Enter fullscreen mode Exit fullscreen mode

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

Remove Context

$ docker context use default # back to default
$ docker context rm my-remote-docker-machine 
my-remote-docker-machine
Enter fullscreen mode Exit fullscreen mode

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!

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay