如何在Mac容器中SSH到Docker [英] How do I SSH to a Docker in Mac container

查看:240
本文介绍了如何在Mac容器中SSH到Docker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Docker for Mac(版本1.12.0-rc2-beta16(内部版本:9493)).

I am running Docker for Mac (Version 1.12.0-rc2-beta16 (Build: 9493)).

我已经从本地存储库中提取了一个映像,并使用'docker run -d'创建了一个容器.使用'docker ps'我获得了'CONTAINER ID',然后使用'docker inspect< CONTAINER_ID> | grep IPA'来获取正在运行的容器的IP地址.

I have pulled an image from my local repository, and used 'docker run -d' to create a container. Using 'docker ps' I obtained the 'CONTAINER ID', and then used 'docker inspect <CONTAINER_ID>| grep IPA' to obtain the IP address for the running container.

我现在想使用SSH和"ssh root @< IP地址>"连接到容器.但该命令给出以下错误:操作超时".

I now want to connect to the container using SSH with 'ssh root@<IP address>' but the command gives the following error: 'Operation timed out'.

进一步的调查显示,我无法ping通< IP地址>. ->请求icmp_seq 0超时"

Further investigation shows that I can not ping the <IP address> -> 'Request timeout for icmp_seq 0'

如何使用SSH连接到容器?正确的命令是什么?

How can I connect to the container using SSH? What is the correct command?

更新:这不是重复项(如上所述).以您描述的方案"开头的条目是正确的解决方案.

UPDATE: This IS NOT a duplicate (as stated above). The entry that begins "The scenario you described" is the correct solution.

推荐答案

您所描述的场景是将在普通" Docker上使用的方法.

The scenario you have described is the approach that would be used on 'normal' Docker.

由于Mac上的Docker是专为Mac而从头开始创建的,因此对其进行了调整以使其更易于使用.因此,在Mac上无法以这种方式使用容器的IP地址.

As Docker on Mac has been created from scratch specifically for the Mac, it has been tweaked to make it easier to use. Therefore, the IP address of the container cannot be used in this way on the Mac.

文档 Docker for Mac入门指出:

以前的beta版本使用docker作为构建URL的主机名. 从此版本开始,端口将在专用IP上公开 VM的地址,并转发到没有其他主机名的localhost 放.另请参阅Beta 9发行说明.

Previous beta releases used docker as the hostname to build the URL. From this release forward, ports are exposed on the private IP addresses of the VM and forwarded to localhost with no other host name set. See also, Release Notes for Beta 9.

因此,将SSH放入容器的正确方法是使用端口映射到SSH端口(22)在Docker for Mac上将其启动.例如

Therefore, the correct way to SSH into a container is to spin it up on Docker for Mac using a port mapping to the SSH port (22). e.g.

 docker run -d -p 2022:22 <Image Name>

并且使用此命令启动SSH连接(注意,它在指定的端口上使用"localhost",而不必确定和使用容器的IP地址):

And the SSH connection is instigated using this command (N.B. it uses 'localhost' on the port specified instead of having to determine and use the container's IP Address):

 ssh -p 2022 root@localhost

不能简单地将端口22映射到自身,即'-p 22:22',因为这会导致以下错误(至少对我来说是这样!):

N.B. It is NOT possible to simply map port 22 to itself i.e. '-p 22:22' as this caused the following error (at least is did for me!):

docker:来自守护程序的错误响应:驱动程序外部编程失败 端点pensive_wilson上的连接 (2e832b82fc67d3e48864975c6eb02f6c099e34eee64b29634cfde286c41e00a7): 启动用户级代理时出错:绑定失败:EADDRINUSE.

docker: Error response from daemon: driver failed programming external connectivity on endpoint pensive_wilson (2e832b82fc67d3e48864975c6eb02f6c099e34eee64b29634cfde286c41e00a7): Error starting userland proxy: Failed to bind: EADDRINUSE.

这篇关于如何在Mac容器中SSH到Docker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆