当前位置:   article > 正文

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

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

不多说先给代码

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"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

启动镜像

  • -p xxxx:22 挂载内部端口 22
docker run -d -p 30969:22  docker.io/library/target-image
  • 1

详解

设置用户

USER root
RUN echo 'root:root' |chpasswd 
# 账号:密码
  • 1
  • 2
  • 3

ssh 三步骤

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

如何让vscode也能连接

  • 安装 wgetcurl
apt-get update -y && \
apt-get install -y git wget curl
  • 1
  • 2

设置镜像自动启动 ssh

CMD ["/usr/sbin/sshd", "-D"]
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/647664
推荐阅读
相关标签
  

闽ICP备14008679号