[Docker] 配置HTTP API访问

Docker1.10.1以上版本配置http API访问:

1、在/usr/lib/systemd/system/docker.service,配置远程访问。

主要是在[Service]这个部分,加上下面两个参数:
-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

# vim /usr/lib/systemd/system/docker.service  
[Service]  
ExecStart=/usr/bin/docker -d -H unix://var/run/docker.sock -H tcp://0.0.0.0:2375   

2、docker重新读取配置文件,重新启动docker服务

# systemctl daemon-reload
# systemctl restart docker

3、查看docker进程,发现docker守护进程在已经监听2375的tcp端口

# ps -ef|grep docker  
root     26208     1  0 23:51 ?        00:00:00 /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

4、查看系统的网络端口,发现tcp的2375端口,的确是docker的守护进程在监听

# netstat -tulp  
Active Internet connections (only servers)  
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name      
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN      886/sshd              
tcp6       0      0 [::]:2375               [::]:*                  LISTEN      26208/dockerd     

5、验证,访问docker镜像

$ sudo docker -H tcp://10.141.211.176:2375 images  
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE   

* 要在别的服务器上远程访问,才能得到,本机不行,可能是配了代理的原因


## Docker API

调用/images/json接口可以获取镜像列表,通过python -mjson.tool可以将JSON数据格式化显示:

sudo docker -H tcp://10.141.211.176:2375/images/json | python -mjson.tool

调用/containers/json接口可以获取正在运行中的容器列表:

curl http://10.141.211.176:2375/containers/json  |python -m json.tool

调用/containers/create接口创建容器:

sudo curl -X POST -H "Content-Type: application/json" -d '{
	"Hostname": "",
	"User": "",
	"MemorySwap": 0,
	"AttachStdin": false,
	"AttachStdout": true,
	"AttachStderr": true,
	"Tty": false,
	"OpenStdin": false,
	"StdinOnce": false,
	"Env": null,
	"Image": "test/test-ui-dashboard",
	"Tag": "latest",
	"WorkingDir": "",
	"ExposedPorts": {"8082/tcp": { }},
	"NetworkDisabled": false,
	"HostConfig": {
		"PortBindings": {
			"8082/tcp": [ {"HostPort": "8082"} ]
		}
	}
}' http://10.141.211.176:2375/containers/create?name=myapiserver

查询特定容器信息:

curl http://10.141.211.176:2375/containers/0c51c77395dccc4af3d899c5046d876ac1eb235d025d103864165ed4290ab81d/json  |python -m json.tool

查询特定容器日志:

curl http://10.141.211.176:2375/containers/282cc100968b2d1df735116346d3bc52a14e9518e7e1de70dc27c78e35f9ed38/logs  |python -m json.tool

启动特定容器:

curl -v -X POST -H  -d http://10.141.211.176:2375/containers/0d581b39e7edd426d765a0c4297c985832f751a61510621ff75046c45ff8ca42/start 

获取network信息:

curl http://10.141.211.176:2375/networks

将特定容器连接到特定网络中:

sudo curl -X POST -H "Content-Type: application/json" -d '
{
"Container": "751eae59e0ae"
}'http://10.141.211.176:2375/network/6da120a74ece9e656e4d36e0fb91751c7a3a331fcf0cc257577b9340c0ce6034/connect

dockerapi详细信息可以查看官方文档:https://docs.docker.com/engine/api/v1.29/

你可能感兴趣的:(微服务)