You will need Docker remote API in various situations. Let’s say you are running the docker host on a remote server and you want to connect to it from your laptop. For this scenario, you can use the remote API and connect to it using REST API’s as the docker engine accepts REST requests.
One more use case is that lets say you have an application and you want to get the details of the contianers in docker host. For this, you can use the remote API feature.
Enable Docker Remote API
All the docker configurations are resent in /etc/init/docker.conf file. In that file, there is a DOCKER_OPTS parameter.
Open the docker.conf file, search for DOCKER_OPTS and add values as shown below.
DOCKER_OPTS='-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock'
The above command will bind the docker engine server to the Unix socket as well on TCP port 4243. “0.0.0.0” means, docker engine accepts connections from all IP addresses.
Now for all the changes to take place, you need to restart the docker service. Execute the following command to do that.
sudo service docker restart
Now, remote API is enabled in your docker host. To test this, there are few ways.
Test using curl
Get the IP address of your docker host where you enabled remote API and execute the following command from any terminal which supports curl.
Note: replace the IP with your Docker host IP
curl -X GET http://192.168.5.5:4243/images/json
The above command will output all the images in your docker host in JSON format.
Test using REST client
You can test the API using REST client like Postman. If you use the following URL in your REST client, you will get the JSON output in a prettified manner as shown in the image below.
In this tutorial, you learned to enable docker remote API. For better security, you can use certificated with the REST requests. That we will cover in another detailed post If you face any issues for this setup, let us know in the comments section.