Docker binding Remote API port

Docker officially provides two different SDKs and HTTP API s: Go and python. I'm not familiar with the Go language. I tried the python SDK. Docker's official Python SDK is for version 2.x, which is tested directly on Python 2.7.5 of CentOS, but there are errors:

# python
Python 2.7.5 (default, Aug  4 2017, 00:39:18) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import docker
/usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.2.1) doesn't match a supported version!
  RequestsDependencyWarning)
>>>

In the first import, there was a problem. Considering the actual situation of the project, I decided to study python API temporarily and change to HTTP API.

according to Official website documents Direct operation:

# curl --unix-socket /var/run/docker.sock http:/v1.35/containers/json?all=true
[{"Id":"af08e69d49be5db6caaf24e1509b69e07baea24f6ac7633b74e76bb3e88aa981","Names":["/boring_booth"],"Image":"demo:latest","ImageID":"sha256:a432acdcjkb22ac42819b9dd53b87930e9016b64eb0a63707f9698e1d90e8a6a","Command":"/bin/bash","Created":1515726419,"Ports":[],"Labels":{"build-date":"20171128","license":"GPLv2","name":"CentOS Base Image","vendor":"CentOS"},"State":"exited","Status":"Exited (0) 17 seconds ago","HostConfig":{"NetworkMode":"default"},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"3125372fde203e6916bc96502ef5951670cbc558e4e0069cc51f8f05dc19a45f","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"","DriverOpts":null}}},"Mounts":[]}]

Obviously, the above command form is not suitable for remote use of HTTP calls. To use it remotely, you need to expose the management port. Modify / etc/docker/daemon.json (if the file does not exist, create a new one), and add hosts configuration:

{
   "hosts": ["unix:///var/run/docker.sock", "0.0.0.0:4789"]
}

The API that can access docker on any IP of the host is specified later. It is recommended to change it to a fixed value to reduce exposure and risk. Then restart the docker service.

# systemctl daemon-reload
# systemctl restart docker
# systemctl -l status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2018-01-12 14:22:13 CST; 6s ago
     Docs: https://docs.docker.com
 Main PID: 21991 (dockerd)
   Memory: 28.6M
   CGroup: /system.slice/docker.service
           ├─21991 /usr/bin/dockerd
           └─21998 docker-containerd --config /var/run/docker/containerd/containerd.toml

......
Jan 12 14:22:13 plouto-docker-host-01 systemd[1]: Started Docker Application Container Engine.
Jan 12 14:22:13 plouto-docker-host-01 dockerd[21991]: time="2018-01-12T14:22:13.397856641+08:00" level=info msg="API listen on 0.0.0.0:4789"
Jan 12 14:22:13 plouto-docker-host-01 dockerd[21991]: time="2018-01-12T14:22:13.397938735+08:00" level=info msg="API listen on /var/run/docker/sock"

It can be seen from the above that port 4789 has been bound. The next host can access another host of the host for testing:

# curl http://192.168.1.21:4789/containers/json?all=true
[{"Id":"af08e69d49be5db6caaf24e1509b69e07baea24f6ac7633b74e76bb3e88aa981","Names":["/boring_booth"],"Image":"demo:latest","ImageID":"sha256:a432acdcjkb22ac42819b9dd53b87930e9016b64eb0a63707f9698e1d90e8a6a","Command":"/bin/bash","Created":1515726419,"Ports":[],"Labels":{"build-date":"20171128","license":"GPLv2","name":"CentOS Base Image","vendor":"CentOS"},"State":"exited","Status":"Exited (0) 17 seconds ago","HostConfig":{"NetworkMode":"default"},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"3125372fde203e6916bc96502ef5951670cbc558e4e0069cc51f8f05dc19a45f","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"","DriverOpts":null}}},"Mounts":[]}]

Tags: Docker Python CentOS JSON

Posted on Fri, 01 May 2020 15:27:32 -0700 by fdost