Enabling and accessing Docker Engine API on a remote docker host on Ubuntu

What is Docker Engine API ?

Docker has become one of the most popular technologies used all around the world today. Docker engine is the portion of a docker installation which takes care of the all the docker features. But when we are trying to automate the processes which uses the docker features such as “deploying an app via Jenkins” we need a way to access the docker engine via a scripts and run docker commands. Docker API helps the developers to access the docker engine through their scripts.

When the Docker Host is in a remote machine, we need a way to access the docker engine API . For accomplishing this task, we can open up a TCP port which allows us to connect with the remote docker host

Enabling Docker Remote API ( Earlier versions of Ubuntu)

After a successful docker installation we have to verify that the docker host is running. You can verify that by running

sudo systemctl status docker

Then we have to add the tcp port details to the /etc/default/docker file. For that first open a file in /etc/default/ named docker by running

sudo nano /etc/default/docker

This needs nano editor to be installed before.If the above command fails you can use some other editor such as vi or run the below command for installing nano editor

sudo apt-get update
sudo apt-get install nano

Then add the below line at the end of the file

DOCKER_OPTS=”-H tcp://0.0.0.0:<port> -H unix:///var/run/docker.sock”

You have to replace <port> with the port number like below

DOCKER_OPTS=”-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock”

Then we have to restart the docker host for the changes to take effect. Do that by typing

sudo service docker restart

Then docker has to be accessed via the tcp port you gave. Verify that you can access the docker host through the specified docker port by running the command

sudo docker -H tcp://127.0.0.1:<port> ps

Replace <port> with the port you specified. In my case it is

sudo docker -H tcp://127.0.0.1:4243 ps

Enabling Docker Remote API ( for Ubuntu 16.04)

In Ubuntu 16.04 , above procedure doesn’t have any effect on docker and is not able to open up the tcp port for docker host. For that open up the /lib/systemd/system/docker.service file by running the command. (For this command , you need nano or you can use an editor like vi as I mentioned above)

sudo nano /lib/systemd/system/docker.service

Then you have to update the file like this,

ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:<port>

You have to replace <port> with your desired port. In my case it’s like this,

ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:4243

now save your changes with ctrl+o and exit from the editor using ctrl+x

Then restart the docker service runningg below commands

sudo systemctl daemon-reload
sudo systemctl restart docker

For verifying whether the port is working, you can either use the method I mentioned earlier for earlier versions of ubuntu or run the following command

curl -X GET http://localhost:4243/images/json

This should return a json with all the images in the docker

Enjoy !!!!