One of the cool feature released with Docker 18.09 is the ability to connect to a docker daemon over ssh. This post would walk you through the steps to connect to a docker daemon running on DigitalOcean.
To create a remote docker host we will use a tool called docker-machine :
|
|
Here is a detailed documentation about setting up a droplet on DigitalOcean. You may verify this by logging into your DigitalOcean account as shown below
When the Droplet is created, Docker generates a unique SSH key and stores it on your local system in ~/.docker/machine/machines
and for this droplet named do-node2 its generated under ~/.docker/machine/machines/do-node2
in my case. This location would contain a file called id_rsa
which is used under the hood to access the droplet directly with docker-machine ssh command as shown below
To connect to the above remote docker daemon we would add the same private key to the authentication agent using the below command -
|
|
Now issue the command :
|
|
On executing the above command your docker client will connect to the DigitalOcean droplet to find that in order to run the nginx container it needs to pull it from DockerHub and then starts the container on the port 32768
(with docker doing the port mapping for you because of flag -P). Below is what you get when you try to access the nginx container using the droplet ip in a browser:
If you don’t want to use the above flag every time with the docker command you may set the environment variable called DOCKER_HOST as shown below -
|
|
Let’s try to use docker-compose now -
The above command failed because docker-compose version < 1.24.0 doesn’t support ssh connection to a remote docker engine. The latest version of docker-compose (as of writing 1.24.0-rc1) supports connecting to remote docker engine using ssh protocol. So lets upgrade to the latest docker-compose
Now let’s create a docker-compose file using the same nginx image and this time exposing the container on port 8000.
As you see on executing the command docker-compose -f docker-compose-ngix.yml up
it spinned up a container on my DigitalOcean droplet named do-node2 having ip 165.227.106.59 and if you try to access the same ip with the port 8000 you may see the default page for nginx
.