Docker似乎忽略了来自`eval $(docker-machine env)的DOCKER_HOST和其他变量。

时间:2017-06-05 10:25:17

标签: docker docker-machine

我在OS X上,通过brew安装了docker。尝试使用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='$1,$2';
       if [ "$DOCKER_COMPLETION_SHOW_PLUGIN_IDS" = yes ]; then
               if [ "$DOCKER_COMPLETION_SHOW_SECRET_IDS" = yes ]; then
       [ "${DOCKER_COMPLETION_SHOW_SERVICE_IDS}" = yes ] && fields='$1,$2';
       [ -z "$DOCKER_HIDE_LEGACY_COMMANDS" ] && commands+=(${legacy_commands[*]});
       if  ${DOCKER_MACHINE_WRAPPED} = true ; then

这是我的泊坞窗版本信息

$ 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二进制文件(之前已经运行过OS X版本的docker了一些东西)。

$ 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

1 个答案:

答案 0 :(得分:4)

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

对于which vs typewhich将指向$PATH中的二进制文件,以告知您某些内容的位置。 type将告诉您shell如何运行该命令,包括shell内置函数,任何别名或函数定义。

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

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

这将为您提供对docker套接字的组级访问权限,因此您不需要为docker命令运行sudo。