16

我在 OSX 上使用docker 和boot2docker

我想从我的终端获得一个 SSH 连接到一个正在运行的容器。

但我不能这样做:(

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

4

5 回答 5

32

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

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

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

于 2015-01-07T19:06:09.170 回答
32

要启用 ssh 到在 VM 中运行的容器,您必须做几件事:

  1. sshd在您的容器中安装并运行(示例)。sshd默认情况下不存在,因为容器通常只运行一个进程,尽管它们可以运行任意多个。
  2. EXPOSE一个端口作为创建映像的一部分,通常是 22,这样当您运行容器时,守护程序会连接到EXPOSE容器内部的 'd 端口,并且可以在容器外部暴露一些东西。
  3. 当您运行容器时,您需要决定如何映射该端口。您可以让 Docker 自动或显式地执行此操作。我建议明确一点:docker run -p 42222:22 ...它将 VM 上的端口 42222 映射到容器中的端口 22。
  4. 将端口映射添加到 VM 以将端口公开给您的主机。例如,当您的 VM 未运行时,您可以添加如下映射: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 端口 42222->container 端口 22。

于 2014-04-11T19:25:06.133 回答
1

如果你只是想进入正在运行的容器,你可以考虑使用nsenter. 这是一个简单的 bash 脚本(由 Chris Jones 建议),您可以使用它进入 docker 容器。将它保存在你的某个地方$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(或任何你想输入的配置)

于 2014-10-01T20:04:33.070 回答
0

A slightly modified variant of Michael's answer that just requires the container you want to enter be named (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 }')
于 2014-10-19T01:33:17.570 回答
0

我已经针对在具有相同操作系统 Docker 18.09.2 的主机上运行的 Ubuntu 16.04 映像对此进行了测试,它也应该适用于 boot2Docker,只需稍作修改。

构建图像。在后台容器中运行它(您的用户可能是 root):

$ docker run -ditu <youruser> <imageId>

用外壳附加到它:

$ docker exec -it <containerId> /bin/bash

安装 openssh-server(sudo仅当您的用户不是 root 时才需要,boot2Docker 的命令可能不同):

$ sudo apt-get install -y openssh-server

运行:

$ sudo service ssh start

(以下步骤是可选的,如果您的用户有密码,您可以跳过它并在每次 ssh 连接时提供密码)。

在客户端主机上创建 RSA 密钥:

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/youruser/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/youruser/.ssh/id_rsa.
Your public key has been saved in /home/youruser/.ssh/id_rsa.pub.

在 docker 镜像上,创建一个目录$HOME/.ssh

$ cd
$ mkdir .ssh && cd .ssh
$ vi authorized_keys

$HOME/.ssh/id_rsa.pub将客户端计算机上的内容复制并粘贴到authorized_keysdocker 映像上并保存文件。

(可选步骤结束)。

记下图片的 IP 地址:

$ cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2  63448863ac39
^^^^^^^^^^ this

现在来自客户端主机的连接应该是有效的:

$ ssh 172.17.0.2
Enter passphrase for key '/home/youruser/.ssh/id_rsa': 
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.15.0-46-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
Last login: Fri Apr  5 09:50:30 2019 from 172.17.0.1

当然,您可以在 Dockerfile 中以非交互方式应用上述过程。

于 2019-04-05T10:38:10.063 回答