无法通过 SSH 连接到运行 Alpine Linux 3.10 的 Docker 容器

Nyx*_*nyx 0 linux ssh openssh docker alpine-linux

我在使用 SSH 连接到运行 Alpine Linux 3.10 的Docker 容器(从此图像中)时遇到问题。

此连接必须使用 SSH,因为我使用的备份软件barman需要通过 SSH 连接到在 Docker 容器内运行的 PostgreSQL 11 数据库。

首先我使用连接到 docker 容器

docker exec -it <container_name> /bin/bash
Run Code Online (Sandbox Code Playgroud)

然后尝试重新安装并启动sshd

bash-5.0# apk add openssh --no-cache
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
OK: 117 MiB in 42 packages

bash-5.0# apk add openrc --no-cache
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
OK: 117 MiB in 42 packages

bash-5.0# rc-update add sshd
 * rc-update: sshd already installed in runlevel `sysinit'; skipping

bash-5.0# /etc/init.d/sshd start
 * WARNING: sshd is already starting
Run Code Online (Sandbox Code Playgroud)

但是,我无法sshd从 Docker 容器内部连接到本地服务器

# ssh root@127.0.0.1
ssh: connect to host 127.0.0.1 port 22: Connection refused
Run Code Online (Sandbox Code Playgroud)

同样,从 Ubuntu 主机通过 SSH 连接到 Docker 容器也会失败。

$ ssh postgres@172.26.0.4
ssh: connect to host 172.26.0.4 port 22: Connection refused
Run Code Online (Sandbox Code Playgroud)

ifconfig其中 172.26.0.4 是在Docker 容器内运行时显示的 IP 地址。

我们有什么想法可以解决这个问题吗?

tin*_*ink 7

我没有下载您所指的图像,而是使用默认的 docker alpine-image 进行工作。要让 ssh 在容器内运行,需要执行一些额外的步骤 - 您很可能需要执行相同的操作:

ssh-keygen -A
rc-status
touch /run/openrc/softlevel
/etc/init.d/sshd start
Run Code Online (Sandbox Code Playgroud)