Wednesday, March 13, 2019

Docker - Enable the Remote API

Most of the times we will be running docker command to run docker containers.There will be few cases where we need to create containers using the api. Docker engine api allows to do basically everything that we do from a command line. This is very useful when we need to create containers as a part of automation or CI/CD process.

One very good use case is to create application containers directory from the continuous integration tool Jenkins. In order to use the docker api, we first need to enable that and process an access point for the automation tools.

1. Configure the docker.conf file in the location /etc/systemd/system/docker.service.d/docker.conf
[root@ip-172-31-21-252 docker.service.d]# cat /etc/systemd/system/docker.service.d/docker.conf
ExecStart=/usr/bin/dockerd -H tcp:// -H unix://var/run/docker.sock

If the docker.service.d directory is not available, create it and also docker.conf inside the directory.

Another way to do the same is to add the details to the daemon.json file in the /etc/docker/daemon.json file as below, 
[root@ip-172-31-26-225 docker]# cat /etc/docker/daemon.json
   "hosts": ["unix:///var/run/docker.sock","tcp://"]
2. Add the contents as shown above. The important thing is the ExecStart line. The -H flag binds the dockerd to a listening socket. The socket can be either a unix socket or a tcp port. We can use the -H flag to bind to multiple sockets or multiple ports. The default -H fd:// uses the systemd socket activation feature to refer to /lib/systemd/system/docker.socket.

We are also binding the port 2375 on the localmachine. So that once we access on the port and IP address, we can get the response.

3. Restart the docker daemon and docker server using
sudo systemctl daemon-reload
sudo systemctl restart docker.service

4. Confirm the service is running by,
[root@ip-172-31-21-252 docker.service.d]# curl http://localhost:2375/version

We can either use the http or also use the tcp calls to access the service.
More to come , Happy learning :-)

No comments :

Post a Comment