Pages

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
[Service]
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -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
{
   "log-level":"warn",
   "hosts": ["unix:///var/run/docker.sock","tcp://0.0.0.0:2375"]
}
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
{"Version":"1.13.1","ApiVersion":"1.26","MinAPIVersion":"1.12","GitCommit":"8633870/1.13.1","GoVersion":"go1.9.4","Os":"linux","Arch":"amd64","KernelVersion":"3.10.0-862.3.2.el7.x86_64","BuildTime":"2018-09-28T19:45:08.749348837+00:00","PkgVersion":"docker-1.13.1-75.git8633870.el7.centos.x86_64"}

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