## .## ## ## ==## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com
Start interactive shell
注意这些命令只限于当前终端,另开 cmd 终端的话由于无法连接到 docker server 而失败,相关提示如下:
> docker version
Client:
Version: 17.10.0-ce
API version: 1.33
Go version: go1.8.3
Git commit: f4ffd25
Built: Tue Oct 1719:00:022017
OS/Arch: windows/amd64
error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.33/version: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running.
这是由于docker server是运行在虚拟机中(ip=192.168.99.100),所以如果想在我们自己的程序中使用docker api,需要使用docker remote api
首先找到 docker server 的 ip 跟端口,使用 docker-machine ls 命令即可查看:
λ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default - virtualbox Running tcp://192.168.99.100:2376 v17.10.0-ce
可以看到 URL 为tcp://192.168.99.100:2376
λ docker -H tcp://192.168.99.100:2376 ps
Get http://192.168.99.100:2376/v1.33/containers/json: malformed HTTP response "\x15\x03\x01\x00\x02\x02".
* Are you trying to connect to a TLS-enabled daemon without TLS?
通过上面命令,可以看到想要连接到 docker server 需要使用TLS
在 cmd 终端,设置以下环境变量即可成功链接:
set DOCKER_HOST=tcp://192.168.99.100:2376set DOCKER_CERT_PATH=C:/Users/Administrator/.docker/machine/certs
set DOCKER_TLS_VERIFY=1
docker images
# 此时能得到正常的结果