Docker 開啟 Rest API
環境:Ubuntu 16.04 LTS 64bit,Docker CE 1.38
配置方法
修改:/lib/systemd/system/docker.service
把ExecStart那行改成:
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:想要開啟的埠 -H unix:///var/run/docker.sock
儲存,並重啟服務:
sudo systemctl docker restart
然後就可以用curl或者瀏覽器進行測試
API文件(1.37版本):
配置的時候遇到三個無效配置
1.修改 /usr/lib/systemd/system/docker.service
不知道是不是個例,我的伺服器上/usr/lib/systemd下面根本沒有system目錄
2.修改 /etc/default/docker
說是新增DOCKER_OPTS = " -H=unix:///var/run/docker.sock -H=0.0.0.0:埠",然後重啟服務就可以,但是實測無效
3.使用 systemctl edit docker.service
然後新增
[Service] ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:埠
實測使用命令後開啟的是空白檔案,並且輸入配置後,由於ExecStart重複配置還導致服務無法執行,並且無法重灌或解除安裝,提示:
Failed to stop docker.service: Unit docker.service not loaded.
並且讓我使用 systemctl status docker.service 檢視服務狀態
使用命令以後,根據顯示的資訊,找到了 /etc/systemd/system/docker.service.d/override.conf 檔案(也就是儲存剛剛新增的配置內容的檔案),把這個檔案清空,啟動服務成功,恢復正常
API 測試(使用Postman或Chrome)
1.搜尋映象(java)
get /images/search?term=java
返回結果:
給出了搜尋結果
2.檢視container
get /containers/json
返回:
[
{
"Id": "9a84f2443f038050115140155c46f3c7791de4f92a6be300a50f1c6586c42369",
"Names": [
"/lucid_stonebraker"
],
"Image": "ubuntu",
"ImageID": "sha256:ea4c82dcd15a33e3e9c4c37050def20476856a08e59526fbe533cc4e98387e39",
"Command": "/bin/bash",
"Created": 1541509446,
"Ports": [],
"Labels": {},
"State": "running",
"Status": "Up 18 hours",
"HostConfig": {
"NetworkMode": "default"
},
"NetworkSettings": {
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "c26e32aa88e8ba869fff5332c3dafb865e1eafcadd12c1e99f217cbfaa77fc00",
"EndpointID": "ae4c138cc718ea87de95a0eb8b59dedc424907223282bfd10cfefa40bb72c2a3",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
},
"Mounts": []
}
]
3.停止container
post /container/{id}/stop
id就是上一步返回結果中的Id欄位
成功後沒有返回值,在putty中查詢,結果如下:
[email protected]:~$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a84f2443f03 ubuntu "/bin/bash" 18 hours ago Exited (0) About a minute ago lucid_stonebraker
說明該容器確實是被這次請求關閉的