【问题标题】:How to ssh to a docker container on ec2 ubuntu host from outside machine如何从外部机器 ssh 到 ec2 ubuntu 主机上的 docker 容器
【发布时间】:2017-12-26 18:32:37
【问题描述】:

我在 ubuntu ec2 主机上运行 docker Demon。

我能够在这个 ubuntu ec2 主机上设置一个 sshd 容器,并能够从 docker 主机 .i.e. SSH 进入

ubuntu@ip-172-16-27-205:~/docker-work$ sudo docker run -d -P --name ssh-enabled ssh-enabled
bb7f535124b3df403ae97da770fe2e4a4969a7ecddfb5d546aaddfe00e40374d

ubuntu@ip-172-16-27-205:~/docker-work$ sudo docker port ssh-enabled
22/tcp -> 0.0.0.0:32768

ubuntu@ip-172-16-27-205:~/docker-work$ ssh root@127.0.0.1 -p 32768

The authenticity of host '[127.0.0.1]:32768 ([127.0.0.1]:32768)' can't be established.
ECDSA key fingerprint is e6:74:aa:52:8e:3d:19:7a:92:da:85:d4:b9:f9:ed:f1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[127.0.0.1]:32768' (ECDSA) to the list of known      hosts.
root@127.0.0.1's password:
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.19.0-30-generic 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@bb7f535124b3:~#

但是我不确定如何从外部 docker 主机连接到这个容器。

我可以像下面这样从外部机器 ssh 到 docker 主机

 ssh -i /home/xxxxxx/xxxxxxxxx.pem ubuntu@172.16.27.205

但是我希望直接 ssh 到容器

docker 容器已启动,如下命令响应所示

ubuntu@ip-172-16-27-205:~/docker-work$ sudo docker ps -as

CONTAINER ID        IMAGE               COMMAND                  CREATED                  STATUS                      PORTS                   NAMES               SIZE
bb7f535124b3        ssh-enabled         "/usr/sbin/sshd -D"      8 minutes ago       Up 8 minutes                0.0.0.0:32768->22/tcp   ssh-enabled         32.51 kB (virtual 868.4 MB)

由于 docker 主机正在侦听端口 32768 当我尝试使用 root 用户 ssh 容器时失败

rbharadwaj@syd01-devops-ansible02:~$ ssh -i '/home/xxxxx/xxxxxxx.pem' root@172.16.27.205 -p 32768 -v
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.8
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1  Ubuntu-2ubuntu2.8
debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.8 pat OpenSSH_6.6.1* compat  0x04000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client chacha20-poly1305@openssh.com <implicit> none
debug1: kex: client->server chacha20-poly1305@openssh.com <implicit> none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ED25519   3e:6a:c6:9e:34:98:2e:81:b5:e1:88:d1:15:63:bb:41
debug1: checking without port identifier
 The authenticity of host '[172.16.27.205]:32768 ([172.16.27.205]:32768)' can't be established.
 ED25519 key fingerprint is 3e:6a:c6:9e:34:98:2e:81:b5:e1:88:d1:15:63:bb:41.
 Are you sure you want to continue connecting (yes/no)? yes
 Warning: Permanently added '[172.16.27.205]:32768' (ED25519) to the list of   known hosts.
 debug1: ssh_ed25519_verify: signature correct
 debug1: SSH2_MSG_NEWKEYS sent
 debug1: expecting SSH2_MSG_NEWKEYS
 debug1: SSH2_MSG_NEWKEYS received
 debug1: SSH2_MSG_SERVICE_REQUEST sent
 debug1: SSH2_MSG_SERVICE_ACCEPT received
 debug1: Authentications that can continue: publickey,password
 debug1: Next authentication method: publickey
 debug1: Trying private key: /home/rbharadwaj/syd01-devops-ruc-app-instances
 debug1: key_parse_private2: missing begin marker
 debug1: read PEM private key done: type RSA
 debug1: Authentications that can continue: publickey,password
 debug1: No more authentication methods to try.
 Permission denied (publickey,password).

认为私钥是用户 Ubuntu 我也尝试像下面这样通过 ssh 进入容器,但它失败了

ssh -i '/home/xxxx/xxxxx.pem' ubuntu@172.16.27.205 -p 32768

请建议我如何在上述情况下直接连接到容器

【问题讨论】:

    标签: docker ssh amazon-ecs


    【解决方案1】:

    并不总是建议您将容器设置为运行 ssh。如果您需要监视日志、控制服务等,那么您最好找到其他方法来完成此操作(日志聚合器等),而不是设置对容器的直接 ssh 访问。

    请参阅https://jpetazzo.github.io/2014/06/23/docker-ssh-considered-evil/ 了解更多信息。

    我知道这并不能直接回答您的问题。但是,除了方便之外,还不清楚为什么您需要直接访问您的容器。您如何管理 ssh 密钥?

    【讨论】:

    • 我有一个现有的设置,它使用 ansible 进行部署(远程主机上的 ansible ssh 用于配置管理),我正在尝试设置容器并使用我的 ans-bile 主机直接连接容器并执行部署。我知道我可以在 docker 主机本身上拥有 ansible,但是要对当前的部署配置管理策略进行尽可能小的更改。我期待实现上述场景
    • 让 Jenkins 构建您更新的容器并将其推送到您的主机似乎是以可重现的方式而不是使用剧本的标准方式。 FWIW 这就是为什么有像 Jenkins 和 ECS 这样的 DevOps 框架来促进修订版的蓝绿部署的原因。 IMO 在 docker 文件中捕获您的修订似乎比依赖需要由 Ansible playbook 更新的过时 Dockerfile 更干净。无论如何!我很高兴您找到了解决方案。干杯@RuchirBharadwaj!
    【解决方案2】:

    ssh 的输出看起来您没有正确设置私钥。它是否位于容器内的正确位置(/root/.ssh/authorized_keys)并且是否具有正确的权限?

    chmod 700 /root/.ssh
    chmod 600 /root/.ssh/authorized_keys
    

    【讨论】:

    • 能否请您帮我突出显示日志中的确切内容指出这是授权密钥的问题。
    • 只有这两行: debug1: Trying private key: /home/rbharadwaj/syd01-devops-ruc-app-instances debug1: key_parse_private2: missing begin marker 我之前遇到过这个问题并且是相关的文件权限。
    【解决方案3】:

    按照上面的建议,容器中没有 /root/.ssh 文件夹。

    1.) 在容器中创建了一个 /root/.ssh 目录

    2.) 创建授权文件

    3.) 将我要连接的主机的公钥内容放置到此主机

    我可以连接

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 1970-01-01
      • 2023-01-19
      • 1970-01-01
      • 2016-03-19
      • 2021-10-25
      • 2014-08-10
      相关资源
      最近更新 更多