osx – 无法连接到Mac OS X上的Docker守护进程

我想在Mac OS X El Capitan(v10.11.2)上使用docker-compose运行多容器应用程序。

但是,命令$ docker-compose up命令表明它无法连接到Docker守护程序。

ERROR: Couldn’t connect to Docker daemon – you might need to run docker-machine start default.

只有在执行$ eval“$(docker-machine env default)”之后,我才能访问docker-compose命令。

为什么会这样,如何克服这个额外的步骤?

最佳答案
Kevan Ahlquist’s helpful answer显示添加到您的Bash配置文件(〜/ .bash_profile)中的命令,以便在打开交互式shell时自动初始化Docker。

请注意,您可以通过打开应用程序/ Applications / Docker / Docker Quickstart Terminal.app(例如,通过Spotlight),始终在新的shell选项卡/窗口中初始化Docker。
从现有的shell,你可以将它调用为open -a’Docker Quickstart Terminal.app'(这也打开一个新的shell选项卡)。
这个答案是在当前shell中启动Docker的一种方便的方法。

在下面添加Bash shell函数 – docker-start和docker-stop – 在以下方面改进了Kevan方法:

>您可以根据需要运行docker-start,而不需要在打开shell时启动VM的开销(一旦Docker VM运行,初始化速度要快得多,但仍然需要大量时间)。
(当然,您仍然可以选择从您的个人资料中调用docker-start。)
> docker-stop允许停止Docker并根据需要清理环境变量。
>这些功能确保Docker的错误消息不被抑制,并且它们通过Docker错误退出代码。
>提供其他状态信息。
>您可以将VM名称作为参数传递;默认是默认值。

例:

$ docker-start
-- Starting Docker VM 'default' (`docker-machine start default`; this will take a while)...
Starting "default"...
(default) Check network to re-create if needed...
(default) Waiting for an IP...
Machine "default" was started.
Waiting for SSH to be available...
Detecting the provisioner...
Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.
-- Setting DOCKER_* environment variables (`eval "$(docker-machine env default)"`)...
DOCKER_CERT_PATH="/Users/jdoe/.docker/machine/machines/default"
DOCKER_HOST="tcp://192.168.99.100:2376"
DOCKER_MACHINE_NAME="default"
DOCKER_TLS_VERIFY="1"
-- Docker VM 'default' is running.


$ docker-stop
-- Stopping Docker VM 'default' (`docker-machine stop default`)...
Stopping "default"...
Machine "default" was stopped.
-- Unsetting DOCKER_* environment variables (DOCKER_CERT_PATH, DOCKER_HOST, DOCKER_MACHINE_NAME, DOCKER_TLS_VERIFY)...
-- Docker VM 'default' is stopped.

Shell功能用于Docker的按需启动和停止(将它们放入,例如〜/ .bash_profile,以便在交互式shell中进行全局可用性)。

注意:这些函数在bash,ksh和zsh中工作,但是在ksh中,您必须重命名它们,以便在函数名中不包含“ – ”。

function docker-start {
  typeset vm=${1:-default} sts
  case $vm in
    -h|--help)
      echo $'usage: docker-start [<vm>]\n\nEnsures that the specified/default Docker VM is started\nand the environment is initialized.'
      return 0
      ;;
  esac
  sts=$(docker-machine status "$vm") || return
  [[ $sts == 'Running' ]] && echo "(Docker VM '$vm' is already running.)" || { 
    echo "-- Starting Docker VM '$vm' (\`docker-machine start "$vm"\`; this will take a while)..."; 
    docker-machine start "$vm" || return
  }
  echo "-- Setting DOCKER_* environment variables (\`eval \"\$(docker-machine env "$vm")\"\`)..."
  # Note: If the machine hasn't fully finished starting up yet from a
  #       previously launched-but-not-waited-for-completion `docker-machine status`,
  #       the following may output error messages; alas, without signaling failure
  #       via the exit code. Simply rerun this function to retry.
  eval "$(docker-machine env "$vm")" || return
  export | grep -o 'DOCKER_.*'
  echo "-- Docker VM '$vm' is running."
}

function docker-stop {
  typeset vm=${1:-default} sts envVarNames fndx
  case $vm in
    -h|--help)
      echo $'usage: docker-stop [<vm>]\n\nEnsures that the specified/default Docker VM is stopped\nand the environment is cleaned up.'
      return 0
      ;;
  esac
  sts=$(docker-machine status "$vm") || return
  [[ $sts == 'Running' ]] && { 
    echo "-- Stopping Docker VM '$vm' (\`docker-machine stop "$vm"\`)...";
    docker-machine stop "$vm" || return
  } || echo "(Docker VM '$vm' is not running.)"
  [[ -n $BASH_VERSION ]] && fndx=3 || fndx=1 # Bash prefixes defs. wit 'declare -x '
  envVarNames=( $(export | awk -v fndx="$fndx" '$fndx ~ /^DOCKER_/ { sub(/=.*/,"", $fndx); print $fndx }') )
  if [[ -n $envVarNames ]]; then
    echo "-- Unsetting DOCKER_* environment variables ($(echo "${envVarNames[@]}" | sed 's/ /, /g'))..."
    unset "${envVarNames[@]}"
  else
    echo "(No DOCKER_* environment variables to unset.)"
  fi
  echo "-- Docker VM '$vm' is stopped."
}

转载注明原文:osx – 无法连接到Mac OS X上的Docker守护进程 - 代码日志