使用dockerfile快速构建一个带ssh的docker镜像

不多说先给代码

dockerfile 复制代码
FROM ubuntu:22.04 # 基础镜像 可替换为其他镜像
USER root
RUN echo 'root:root' |chpasswd
RUN apt-get update -y \
    &&  apt-get install -y git wget curl
RUN apt-get install -y openssh-server vim && apt clean \
    && rm -rf /tmp//* /var/lib/apt/lists/* /var/tmp* \
    && echo "PermitRootLogin yes" >> /etc/ssh/sshd_config



RUN mkdir -p /var/run/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

启动镜像

  • -p xxxx:22 挂载内部端口 22
bash 复制代码
docker run -d -p 30969:22  docker.io/library/target-image

详解

设置用户

bash 复制代码
USER root
RUN echo 'root:root' |chpasswd 
# 账号:密码

ssh 三步骤

  • 首要条件是安装 openssh-servervim
bash 复制代码
apt-get update -y \
&&  apt-get install -y openssh-server vim
  • 其次添加字段 "PermitRootLogin yes"/etc/ssh/sshd_config文件
bash 复制代码
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
  • 最后新建文件夹
bash 复制代码
mkdir -p /var/run/sshd

如何让vscode也能连接

  • 安装 wgetcurl
bash 复制代码
apt-get update -y && \
apt-get install -y git wget curl

设置镜像自动启动 ssh

bash 复制代码
CMD ["/usr/sbin/sshd", "-D"]
相关推荐
fetasty20 小时前
rustfs加picgo图床搭建
docker
蝎子莱莱爱打怪1 天前
GitLab CI/CD + Docker Registry + K8s 部署完整实战指南
后端·docker·kubernetes
碳基沙盒2 天前
OpenClaw 多 Agent 配置实战指南
运维
小p3 天前
docker学习7:docker 容器的通信方式
docker
小p3 天前
docker学习5:提升Dockerfile水平的5个技巧
docker
小p3 天前
docker学习3:docker是怎么实现的?
docker
小p4 天前
docker学习: 2. 构建镜像Dockerfile
docker
小p5 天前
docker学习: 1. docker基本使用
docker
蝎子莱莱爱打怪5 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
埃博拉酱5 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code