gpt4 book ai didi

ssh - Docker SSH 服务器连接被拒绝

转载 作者:行者123 更新时间:2023-12-02 01:23:31 28 4
gpt4 key购买 nike

我的容器有以下 Dockerfile:

FROM ubuntu:latest
RUN apt-get upgrade
RUN apt-get update
RUN apt-get -y install openssh-server
CMD tail -f /dev/null

但是,当我构建并运行它时,它说连接被拒绝。我尝试了几乎所有我能找到的解决方案,包括“ufw allow”,它不起作用,因为它告诉我我需要成为 root(我如何为 Dockerfile 做到这一点?)。

我需要以这种方式访问​​容器:
ssh <someUsername?>@containerIP

PS:我知道还有其他方法可以连接到 Docker 容器,但是我需要使用 SSH,因此附加等不是一种选择。

编辑:这似乎是因为 sshd 没有运行,因为如果我运行“ps -A”它不会出现。

最佳答案

Docker 容器隔离应用程序或命令,在您的情况下,它们隔离命令 tail -f /dev/null这将使容器保持运行,但不会做任何有用的事情。如果要运行 sshd 服务器,则需要将其作为前台命令启动:

CMD ["/usr/sbin/sshd", "-D"]

Docker Hub 上已经为此构建了很多镜像,包括 https://hub.docker.com/r/trsouz/ssh/https://hub.docker.com/r/rastasheep/ubuntu-sshd/ .

关于ssh - Docker SSH 服务器连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38568246/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com