Remote Docker Contexts
Despite being a Docker user for years now, I recently learned about a killer feature called Docker Contexts which allows the docker cli to target different nodes or clusters. This seems like a great addition to tools like code-server and projector to do development on a remote server.
Remote Host Setup
Setup is simple - to start, make sure that the remote host has docker installed.
Client Machine Setup
Make sure your client machine (the machine where you’re going to be running the docker cli) has passwordless ssh setup to the remote host. Now, you’re ready to create a new docker context.
[~]$ docker context create contextname --docker "host=ssh://username@remotehostname"
contextname
Successfully created context "contextname"
[~]$ docker context use contextname
contextname
That’s it! Your docker cli is now pointing to your remote host.
Testing it Out
If you now run a docker command, it will be executed on the remote host.
# Client Machine
[~]$ docker run hello-world
# Remote Host
[~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bee29c20474 hello-world "/hello" About a minute ago Exited (0) About a minute ago strange_torvalds
Other Commands
You can use docker context ls
to get a list of all the contexts on your machine.
Limitations
It’s worth nothing that if you specify volumes that are mapped on the client machine, they of course will not be available on the remote machine and thus will not work. This means that you’ll need to ensure that volume paths refer to the remote machine - which in retrospect seems pretty obvious.