2016-12-12 58 views
2

我有docker Demon在ubuntu ec2主机上运行。如何从外部机器ssh到ec2 ubuntu主机上的docker容器

我可以在这个ubuntu ec2主机上设置一个sshd conatainer,并且可以从docker主机ssh中获取.i.e。

[email protected]:~/docker-work$ sudo docker run -d -P --name ssh-enabled ssh-enabled 
bb7f535124b3df403ae97da770fe2e4a4969a7ecddfb5d546aaddfe00e40374d 

[email protected]:~/docker-work$ sudo docker port ssh-enabled 
22/tcp -> 0.0.0.0:32768 

[email protected]:~/docker-work$ ssh [email protected] -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. 
[email protected]'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. 

[email protected]:~# 

但我不知道如何从外部码头主机连接到此容器。

我可以ssh从外部机器泊坞窗主机像下面

ssh -i /home/xxxxxx/xxxxxxxxx.pem [email protected] 

但是我希望直接ssh到容器

搬运工容器达如图下方命令响应

[email protected]:~/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) 

作为码头主机正在侦听端口32768当我尝试用root用户ssh容器失败时

[email protected]:~$ ssh -i '/home/xxxxx/xxxxxxx.pem' [email protected] -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 [email protected] <implicit> none 
debug1: kex: client->server [email protected] <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 -i '/home/xxxx/xxxxx.pem' [email protected] -p 32768 

ssh到容器象下面这样好心建议我怎么能在上述情况下直接连接到容器

回答

1

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

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

请问,请帮助我突出显示日志中的确切内容,这是授权密钥的问题。 –

+0

只是那些两条线: DEBUG1:试图私钥:/家庭/ rbharadwaj/SYD01-DEVOPS-RUC-APP-实例 DEBUG1:key_parse_private2:缺少开始标记 我以前也遇到过这个问题,并在相关文件权限。 – Gabb0

2

并不总是建议设置你的容器来运行ssh。如果你需要监视日志,控制服务等,那么你可能会更好地找到其他方式来完成这个(日志聚合器等),而不是设置直接访问你的容器的SSH。

有关更多信息,请参阅https://jpetazzo.github.io/2014/06/23/docker-ssh-considered-evil/

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

+0

我有一个现有的设置,使用部署ansible(ansible SSH远程主机上进行配置管理),我想设置的容器,用我ANS-胆汁主机直接与集装箱连接并进行部署。 我知道我可以在Docker主机上拥有自己的能力,但是尽可能少地改变当前的部署配置管理策略。我看起来实现了以上场景 –

+0

看起来像让詹金斯构建更新的容器并将其推送到主机是以可重现的方式执行操作的标准方式,而不是使用剧本。 FWIW这就是为什么像Jenkins和ECS这样的DevOps框架可以促进您的修订版的蓝绿色部署。国际海事组织似乎更清楚捕获您的泊坞窗文件中的修订版本,而不是依赖于需要由Ansible手册更新的过期Dockerfile。无论如何!我很高兴你找到了解决办法。欢呼@RuchirBharadwaj! – W4t3randWind

1

容器中没有/root/.ssh文件夹,我按照以上推荐的方法执行了下面的操作。

1)集装箱

2)创建创建一个/root/.ssh目录中的授权档案

3)把公钥从我尝试连接到主机的内容该主机

,我是能够连接

相关问题