开启ssh的centos发行版的Dockerfile

帮我写个dockerfile文件, 并且开启ssh远程连接 需要centos发行版

要创建一个 Dockerfile 以构建一个包含 SSH 服务的 CentOS 容器,你可以按照以下步骤操作:

  1. 选择基础镜像: 选择一个 CentOS 基础镜像。
  2. 安装 SSH 服务: 在容器中安装 SSH 服务(通常是 openssh-server)。
  3. 生成 SSH 密钥: 为了安全起见,生成一个新的 SSH 密钥对,避免使用默认的密钥。
  4. 设置 root 密码: 设置一个 root 密码或者创建一个新的用户并设置密码。
  5. 配置 SSH 服务: 配置 SSH 服务,例如允许 root 登录或者使用密码认证。
  6. 暴露 SSH 端口: 在 Dockerfile 中指定要暴露的端口,通常是 22。
  7. 启动 SSH 服务: 设置容器启动时自动启动 SSH 服务。
    下面是一个简单的 Dockerfile 示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 使用官方 CentOS 基础镜像
FROM centos:latest
# 安装 SSH 服务
RUN yum install -y openssh-server
# 生成 SSH 密钥
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
RUN ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
# 设置 root 密码
RUN echo "root:password" | chpasswd
# 配置 SSH 服务
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
# 暴露 SSH 端口
EXPOSE 22
# 启动 SSH 服务
CMD ["/usr/sbin/sshd", "-D"]

请注意,这个 Dockerfile 使用了简单密码 “password” 作为示例,实际使用时你应该使用一个更复杂的密码。此外,为了安全起见,你可能不希望使用 root 用户进行 SSH 连接,而是创建一个新的用户。
构建镜像:

1
docker build -t centos-ssh .

运行容器并映射端口:

1
docker run -d --name centos-ssh -p 2222:22 centos-ssh

现在,你可以使用 SSH 客户端从宿主机连接到容器的 SSH 服务:

1
ssh root@localhost -p 2222

输入之前设置的密码即可登录。
请确保你了解运行 SSH 服务的 Docker 容器的安全风险,并根据实际情况进行相应的安全配置。