Docker 似乎忽略了 DOCKER_HOST 和其他从`eval $(docker-machine env)` 设置的变量

Docker seems to ignore DOCKER_HOST and other vars set from `eval $(docker-machine env)`

我在 OS X 上,docker 通过 brew 安装。尝试结合使用 docker-machine 和 docker。 Docker 似乎忽略了我的环境设置。我该如何解决这个问题?

   $ docker-machine env default2
   export DOCKER_TLS_VERIFY="1"
   export DOCKER_HOST="tcp://192.168.99.100:2376"
   export DOCKER_CERT_PATH="/Users/kasterma/.docker/machine/machines/default2"
   export DOCKER_MACHINE_NAME="default2"
   # Run this command to configure your shell:
   # eval $(docker-machine env default2)

   $ eval $(docker-machine env default2)

我的理解是,现在一切都在 shell 中设置,因为它应该使用 docker。然而:

   $ docker ps
   Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

如果我在命令行上指定所有内容,它就可以工作

   $ docker -H tcp://192.168.99.100:2376 --tls --tlscacert /Users/kasterma/.docker/machine/machines/default2/ca.pem --tlscert /Users/kasterma/.docker/machine/machines/default2/cert.pem --tlskey /Users/kasterma/.docker/machine/machines/default2/key.pem ps
   CONTAINER ID        NAMES               IMAGE               STATUS

它们一起似乎表明环境变量被忽略了。请注意,它们设置正确(我相信,此处输出的缩进部分来自命令行完成)。

   $ set | grep DOCK
   DOCKER_CERT_PATH=/Users/kasterma/.docker/machine/machines/default2
   DOCKER_HOST=tcp://192.168.99.100:2376
   DOCKER_MACHINE_NAME=default2
   DOCKER_TLS_VERIFY=1
               if [ "${DOCKER_COMPLETION_SHOW_CONTAINER_IDS}" = yes ]; then
       case "$DOCKER_COMPLETION_SHOW_IMAGE_IDS" in
       if [ "${DOCKER_COMPLETION_SHOW_TAGS:-yes}" = "yes" ]; then
       case "$DOCKER_COMPLETION_SHOW_IMAGE_IDS" in
       if test ${DOCKER_MACHINE_NAME}; then
           if test ${DOCKER_MACHINE_PS1_SHOWSTATUS:-false} = true; then
               status=$(docker-machine status ${DOCKER_MACHINE_NAME});
           printf -- "${format}" "${DOCKER_MACHINE_NAME}${status}";
                   echo "Active machine: ${DOCKER_MACHINE_NAME}"
               if [ "${DOCKER_COMPLETION_SHOW_NETWORK_IDS}" = yes ]; then
       [ "${DOCKER_COMPLETION_SHOW_NODE_IDS}" = yes ] && fields=',';
       if [ "$DOCKER_COMPLETION_SHOW_PLUGIN_IDS" = yes ]; then
               if [ "$DOCKER_COMPLETION_SHOW_SECRET_IDS" = yes ]; then
       [ "${DOCKER_COMPLETION_SHOW_SERVICE_IDS}" = yes ] && fields=',';
       [ -z "$DOCKER_HIDE_LEGACY_COMMANDS" ] && commands+=(${legacy_commands[*]});
       if  ${DOCKER_MACHINE_WRAPPED} = true ; then

这是我的docker版本信息

$ docker -H tcp://192.168.99.100:2376 --tls --tlscacert /Users/kasterma/.docker/machine/machines/default2/ca.pem --tlscert /Users/kasterma/.docker/machine/machines/default2/cert.pem --tlskey /Users/kasterma/.docker/machine/machines/default2/key.pem version
Client:
 Version:      17.05.0-ce
 API version:  1.29
 Go version:   go1.8.1
 Git commit:   89658be
 Built:
 OS/Arch:      darwin/amd64

Server:
 Version:      17.05.0-ce
 API version:  1.29 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 21:43:09 2017
 OS/Arch:      linux/amd64
 Experimental: false

并显示链接到 brew 版本的 docker 二进制文件(有些东西是 运行 之前 docker 的 OS X 版本遗留下来的)。

$ ls -l dock*
lrwxr-xr-x 1 kasterma admin       35 Jun  5 12:14 docker -> ../Cellar/docker/17.05.0/bin/docker
lrwxr-xr-x 1 kasterma admin       50 Jun  5 12:15 docker-compose -> ../Cellar/docker-compose/1.13.0/bin/docker-compose
-rwxr-xr-x 1 kasterma admin  4938393 Dec  4  2015 docker-compose.backup
lrwxr-xr-x 1 kasterma staff       91 Jun  5 11:54 docker-credential-osxkeychain -> '/Users/kasterma/Library/Group Containers/group.com.docker/bin/docker-credential-osxkeychain'
lrwxr-xr-x 1 kasterma admin       50 Jun  5 12:12 docker-machine -> ../Cellar/docker-machine/0.11.0/bin/docker-machine
-rwxr-xr-x 1 kasterma admin  7417984 Dec  1  2015 docker-machine-driver-amazonec2
-rwxr-xr-x 1 kasterma admin  8112752 Dec  1  2015 docker-machine-driver-azure
-rwxr-xr-x 1 kasterma admin  7355040 Dec  1  2015 docker-machine-driver-digitalocean
-rwxr-xr-x 1 kasterma admin  7343472 Dec  1  2015 docker-machine-driver-exoscale
-rwxr-xr-x 1 kasterma admin  7471952 Dec  1  2015 docker-machine-driver-generic
-rwxr-xr-x 1 kasterma admin 10575760 Dec  1  2015 docker-machine-driver-google
-rwxr-xr-x 1 kasterma admin  7362496 Dec  1  2015 docker-machine-driver-hyperv
-rwxr-xr-x 1 kasterma admin  6830912 Dec  1  2015 docker-machine-driver-none
-rwxr-xr-x 1 kasterma admin  7709792 Dec  1  2015 docker-machine-driver-openstack
-rwxr-xr-x 1 kasterma admin  7778992 Dec  1  2015 docker-machine-driver-rackspace
-rwxr-xr-x 1 kasterma admin  7328400 Dec  1  2015 docker-machine-driver-softlayer
-rwxr-xr-x 1 kasterma admin  8046416 Dec  1  2015 docker-machine-driver-virtualbox
-rwxr-xr-x 1 kasterma admin  7907168 Dec  1  2015 docker-machine-driver-vmwarefusion
-rwxr-xr-x 1 kasterma admin  7545600 Dec  1  2015 docker-machine-driver-vmwarevcloudair
-rwxr-xr-x 1 kasterma admin  7449392 Dec  1  2015 docker-machine-driver-vmwarevsphere
-rwxr-xr-x 1 kasterma admin  9039648 Dec  1  2015 docker-machine.backup
-rwxr-xr-x 1 kasterma admin  9231840 Dec  4  2015 docker.backup

根据评论,您的 docker 命令别名为 sudo docker。 sudo 命令不会通过 docker-machine 定义的环境变量,因此它将默认返回 /var/lib/docker.sock.

至于 whichtypewhich 将指向 $PATH 中的二进制文件,让您知道某物的位置。 type 会让您知道 shell 如何 运行 执行该命令,包括 shell 内置命令、任何别名或函数定义。

要解决此问题,请从 bash 脚本中删除别名并启动新的 shell。在具有别名的现有 shell 中,您可以 运行 unalias docker/usr/local/bin/docker(使用完整路径)。在您使用 sudo 的环境中,考虑将用户添加到 docker 组:

sudo usermod -aG docker `whoami` && newgrp docker

这将使您可以组级别访问 docker 套接字,因此您不需要 运行 sudo 来执行 docker 命令。