如何在OSX上获得与docker容器的ssh连接(boot2docker)

我用boot2docker在OSX上使用docker 。

我想从我的terminal获得一个Ssh连接到一个正在运行的容器。

但我不能这样做:(

我认为这是因为Docker在虚拟机上运行。

有几件事情你必须做,以启用ssh'ing虚拟机中运行的容器:

  1. 在你的容器中安装和运行sshd ( 例子 )。 sshd在默认情况下是不存在的,因为容器通常只运行一个进程,尽pipe他们可以运行多个进程。
  2. 将端口作为创build映像的一部分(典型值为22),以便在运行容器时,守护程序连接到容器内部的EXPOSE端口,并且可以在容器的外部暴露某些东西。
  3. 在运行容器时,您需要决定如何映射该端口。 您可以让Docker自动执行或明确执行。 我build议是显式的: docker run -p 42222:22 ...它将VM上的端口42222映射到容器中的端口22。
  4. 将端口映射添加到虚拟机以将端口展示给主机。 例如,当您的虚拟机未运行时,可以添加如下映射: VBoxManage modifyvm "boot2docker-vm" --natpf1 "containerssh,tcp,,42222,,42222"

然后从你的主机,你应该能够SSH到端口42222到达容器的SSH守护进程。

以下是执行上述步骤时发生的情况:

 $ VBoxManage modifyvm "boot2docker-vm" --natpf1 "containerssh,tcp,,42222,,42222" $ ./boot2docker start [2014-04-11 12:07:35] Starting boot2docker-vm... [2014-04-11 12:07:55] Started. $ docker run -d -p 42222:22 dhrp/sshd Unable to find image 'dhrp/sshd' (tag: latest) locally Pulling repository dhrp/sshd 2bbfe079a942: Download complete c8a2228805bc: Download complete 8dbd9e392a96: Download complete 11d214c1b26a: Download complete 27cf78414709: Download complete b750fe79269d: Download complete cf7e766468fc: Download complete 082189640622: Download complete fa822d12ee30: Download complete 1522e919ec9f: Download complete fa594d99163a: Download complete 1bd442970c79: Download complete 0fda9de88c63: Download complete 86e22a5fdce6: Download complete 79d05cb13124: Download complete ac72e4b531bc: Download complete 26e4b94e5a13b4bb924ef57548bb17ba03444ca003128092b5fbe344110f2e4c $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 26e4b94e5a13 dhrp/sshd:latest /usr/sbin/sshd -D 6 seconds ago Up 3 seconds 0.0.0.0:42222->22/tcp loving_einstein $ ssh root@localhost -p 42222 The authenticity of host '[localhost]:42222 ([127.0.0.1]:42222)' can't be established. RSA key fingerprint is .... Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[localhost]:42222' (RSA) to the list of known hosts. root@localhost's password: screencast Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.12.1-tinycore64 x86_64) * Documentation: https://help.ubuntu.com/ The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. root@26e4b94e5a13:~# exit logout 

这样就显示出ssh-> localhost 42222-> VM port 42222-> container port 22。

Docker已经将docker exec命令添加到了Docker 1.3.0。 您可以使用以下命令连接到正在运行的容器:

 docker exec -it <container id> /bin/bash 

这将连接到正在运行的容器上的bash提示符。

如果你只是想进入正在运行的容器,你可以考虑使用nsenter 。 这是一个简单的bash脚本( Chris Jonesbuild议 ),您可以使用它来input泊坞窗容器。 将它保存在$PATH某处作为docker-enter和chmod +x

 #!/bin/bash set-e # Check for nsenter. If not found, install it boot2docker ssh '[ -f /var/lib/boot2docker/nsenter ] || docker run --rm -v /var/lib/boot2docker/:/target jpetazzo/nsenter' # Use bash if no command is specified args=$@ if[[ $# = 1 ]]; then args+=(/bin/bash) fi boot2docker ssh -t sudo /var/lib/boot2docker/docker-enter "${args[@]}" 

然后你可以运行docker-enter 89af3d (或任何你想要input的configuration)

迈克尔答案的一个稍微修改的变体,只需要input的容器被命名为(APPNAME):

 boot2docker ssh '[ -f /var/lib/boot2docker/nsenter ] || docker run --rm -v /var/lib/boot2docker/:/target jpetazzo/nsenter' boot2docker ssh -t sudo /var/lib/boot2docker/docker-enter $(docker ps | grep $APPNAME | awk '{ print $1 }')