Often you want to save some battery life when you are doing docker builds and leverage a remote host to do the intensive work and we can utilise docker context over ssh to do just that.
About
In this tutorial I will show you how to use a remote docker engine to do docker builds, so you still run the docker client locally, but the context of your build will be sent to a remote docker engine via ssh.
We will setup password-less ssh, configure our ssh config, create the remote docker context, then use the remote docker context.
Password-less SSH
I will be copying my public key to the remote host:
1
|
|
Setup my ssh config:
1 2 3 4 5 6 7 |
|
Test:
1 2 |
|
Docker Context
On the target host (192.168.2.18) we can verify that docker is installed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
On the client (my laptop in this example), we will create a docker context called “home-server” and point it to our target host:
1 2 3 |
|
Now we can list our contexts:
1 2 3 4 |
|
Using Contexts
We can verify if this works by listing our cached docker images locally and on our remote host:
1 2 |
|
And listing the remote images by specifying the context:
1 2 |
|
We can set the default context to our target host:
1 2 |
|
Running Containers over Contexts
So running containers with remote contexts essentially becomes running containers on remote hosts. In the past, I had to setup a ssh tunnel, point the docker host env var to that endpoint, then run containers on the remote host.
Thats something of the past, we can just point our docker context to our remote host and run the container. If you haven’t set the default context, you can specify the context, so running a docker container on a remote host with your docker client locally:
1 2 |
|
Now from our client (laptop), we can test our container on our remote host:
1 2 |
|
The same way can be used to do remote docker builds, you have your Dockerfile locally, but when you build, you point the context to the remote host, and your context (dockerfile and files referenced in your dockerfile) will be sent to the remote host. This way you can save a lot of battery life as the computation is done on the remote docker engine.
Thank You
Thanks for reading, feel free to check out my website, feel free to subscribe to my newsletter or follow me at @ruanbekker on Twitter.