简体   繁体   English

无法在正在运行的 Docker 容器中使用 ssh localhost

[英]Unable to ssh localhost within a running Docker container

I'm building a Docker image for an application which requires to ssh into localhost (ie ssh user@localhost)我正在为需要 ssh 到本地主机(即 ssh user@localhost)的应用程序构建一个 Docker 图像

I'm working on a Ubuntu desktop machine and started with a basic ubuntu:16.04 container.我正在使用一台 Ubuntu 台式机,并从一个基本的 ubuntu:16.04 容器开始。 Following is the content of my Dockerfile:以下是我的Dockerfile的内容:

FROM ubuntu:16.04

RUN apt-get update && apt-get install -y \
        openjdk-8-jdk \
        ssh && \
        groupadd -r custom_group && useradd -r -g custom_group -m user1

USER user1

RUN ssh-keygen -b 2048 -t rsa -f ~/.ssh/id_rsa -q -N "" && \
        cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

Then I build this container using the command:然后我使用命令构建这个容器:

docker build -t test-container .

And run it using:并使用以下命令运行它:

docker run -it test-container

The container opens with the following prompt and the keys are generated correctly to enable ssh into localhost:容器打开并显示以下提示,并且正确生成密钥以启用 ssh 到本地主机:

user1@0531c0f71e0a:/$ 
user1@0531c0f71e0a:/$ cd ~/.ssh/
user1@0531c0f71e0a:~/.ssh$ ls
authorized_keys  id_rsa  id_rsa.pub

Then ssh into localhost and greeted by the error:然后 ssh 进入 localhost 并受到错误的欢迎:

user1@0531c0f71e0a:~$ ssh user1@localhost
ssh: connect to host localhost port 22: Cannot assign requested address

Is there anything I'm doing wrong or any additional.network settings that needs to be configured?我做错了什么或需要配置任何额外的网络设置吗? I just want to ssh into localhost within the running container.我只想将 ssh 放入正在运行的容器中的本地主机。

First you need to install the ssh server in the image building script: 首先,您需要在映像构建脚本中安装ssh服务器:

  • RUN sudo apt-get install -y openssh-server

Then you need to start the ssh server: 然后你需要启动ssh服务器:

  • RUN sudo /etc/init.d/ssh start

or probably even in the last lines of the Dockerfile ( you must have one binary instantiated to keep the container running ... ) 或者甚至可能在Dockerfile的最后几行(你必须实例化一个二进制文件以保持容器运行......)

 USER root
 CMD [ "sh", "/etc/init.d/ssh", "start"]

on the host than 在主机上比

# init a container from an the image
run -d --name my-ssh-container-name-01 \
    -v /opt/local/dir:/opt/container/dir my-image-01

As @user2915097 stated in the OP comments, this was due to the ssh instance in the container was attempting to connect to the host using IPv6. 正如在OP注释中所述@ user2915097,这是由于容器中的ssh实例尝试使用IPv6连接到主机。 Forcing connection over IPv4 using -4 solved the issue. 使用-4强制通过IPv4连接解决了这个问题。

$ docker run -it ubuntu ssh -4 user@hostname

For Docker Compose I was able to add the following to my .yml file: 对于Docker Compose,我能够将以下内容添加到我的.yml文件中:

network_mode: "host"

I believe the equivalent in Docker is: 我相信Docker中的等价物是:

--net=host

I also faced this error today, here's how to fix it:我今天也遇到了这个错误,这是修复它的方法:

If(and only if) you are facing this error inside a running container that isn't in production.如果(且仅当)您在未投入生产的正在运行的容器中遇到此错误。 Do this:做这个:

docker exec -it -u 0 [your container id here] /bin/bash

then when you entered the container in god mode, run this:然后当你以上帝模式进入容器时,运行这个:

service ssh start

then you can run your ssh based commands.然后您可以运行基于 ssh 的命令。

Of course it is best practice to do it in your Dockerfile before all these, but no need to sweat if you are not done with your image built process just yet.当然,最佳做法是在所有这些之前在您的 Dockerfile 中执行此操作,但如果您尚未完成图像构建过程,则无需担心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM