Mac OSX系統 Docker啟用Docker遠程API功能_docker

來源:互聯網
上載者:User

在Mac OSX系統的Docker機上啟用Docker遠程API功能

Docker守護進程提供了一套遠程REST API,具體可以參考文檔:

https://docs.docker.com/engine/reference/api/docker_remote_api/

這套API是提供給用戶端與Docker引擎通訊時使用,這套API也可以由其他工具調用,比如curl或Chrome瀏覽器的Postman REST用戶端工具。

如果是在Mac OSX Mavericks系統上使用Docker機建立Docker守護進程,那麼要啟用Docker遠程API功能需要一定的技巧。下面一一道來。

可以使用curl工具串連到安全的Docker連接埠,命令如下:

$ curl https://$HOST:2376/images/json  --cert ~/.docker/cert.pem  --key ~/.docker/key.pem  --cacert ~/.docker/ca.pem

此命令存在一定的問題。主要有:

1)命令可能不工作,因為每一個Docker機的憑證存放區在.docker/machine/machines/目錄。
2)即使命令根據路徑做了修改,比如:

curl https://192.168.99.100:2376/images/json --cert $DOCKER_CERT_PATH/cert.pem --key $DOCKER_CERT_PATH/key.pem --cacert $DOCKER_CERT_PATH/ca.pem

執行命令仍然會得到錯誤資訊:

curl: (58) SSL: Can't load the certificate "/Users/arungupta/.docker/machine/machines/couchbase/cert.pem" and its private key: OSStatus -25299

解決方案是需要更新curl工具。總的來說,最新版的curl工具使用了Apple的安全傳輸層API(Secure Transport API),取代了原先的OpenSSL API。這意味著認證必須是p12格式。

下面可以這樣修複命令:

1)進入Docker機存放認證的目錄,比如.docker/machine/machines/couchbase目錄
2)產生*.p12格式的認證

openssl pkcs12 -export -inkey key.pem -in cert.pem -CAfile ca.pem -chain -name client-side -out cert.p12 -password pass:mypass

現在可以調用REST API了:

curl https://192.168.99.100:2376/images/json --cert $DOCKER_CERT_PATH/cert.p12 --pass mypass --key $DOCKER_CERT_PATH/key.pem --cacert $DOCKER_CERT_PATH/ca.pem

注意,–cert參數現在指向了產生的p12認證,認證的密碼使用–pass參數進行指定。

然後會得到如下結果:

[{"Id":"sha256:d38beda529d3274636d6cb1c9000afe4f00fbdcfa544140d6cc0f5d7f5b8434a","ParentId":"","RepoTags":["arungupta/couchbase:latest"],"RepoDigests":null,"Created":1450330075,"Size":374824677,"VirtualSize":374824677,"Labels":{}}]

現在可以嘗試啟動CouchBase伺服器:

~ > docker run -d -p 8091-8093:8091-8093 -p 11210:11210 arungupta/couchbase42d1414883affd0fbb272cb1378c2f6b5118acf3ed5cb60cbecdc42f95602e3e

再調用另一個REST API來查看容器的細節內容:

~ > curl https://192.168.99.100:2376/containers/json --cert $DOCKER_CERT_PATH/cert2.p12 --pass mypass --key $DOCKER_CERT_PATH/key.pem --cacert $DOCKER_CERT_PATH/ca.pem[{"Id":"42d1414883affd0fbb272cb1378c2f6b5118acf3ed5cb60cbecdc42f95602e3e","Names":["/admiring_pike"],"Image":"arungupta/couchbase","ImageID":"sha256:d38beda529d3274636d6cb1c9000afe4f00fbdcfa544140d6cc0f5d7f5b8434a","Command":"/entrypoint.sh /opt/couchbase/configure-cluster.sh","Created":1454850194,"Ports":[{"IP":"0.0.0.0","PrivatePort":8092,"PublicPort":8092,"Type":"tcp"},{"PrivatePort":11207,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":11210,"PublicPort":11210,"Type":"tcp"},{"PrivatePort":18092,"Type":"tcp"},{"PrivatePort":18091,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":8093,"PublicPort":8093,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":8091,"PublicPort":8091,"Type":"tcp"},{"PrivatePort":11211,"Type":"tcp"}],"Labels":{},"Status":"Up 2 seconds","HostConfig":{"NetworkMode":"default"},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"","EndpointID":"6feaf4c1c70feaf0ba240ce55fb58ce83ebb84c8098bef9171998e84f607fa0b","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02"}}}}]

感謝閱讀,希望能協助到大家,謝謝大家對本站的 支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.