一、docker container實現
1、docker容器啟動時掛載docker.sock
docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock centos:7.6.1810
2、curl調用接口
curl -s --unix-socket /var/run/docker.sock http://localhost/containers/json
二、docker service實現
1、docker service啟動時掛載
docker service create -p 8080 --name=tomcat --mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock,readonly tomcat:8.5.41
2、進入對應容器curl調用接口
curl -s --unix-socket /var/run/docker.sock http://localhost/services
擴展:
在java程序中可以使用《java執行系統命令, 返回執行結果》一文中的工具類獲取接口返回
參考連接:
https://portainer.readthedocs.io/en/latest/deployment.html
https://www.cnblogs.com/fundebug/p/6723464.html