千家信息网

怎么用Dockerfile构建SSH Server

发表于:2025-02-06 作者:千家信息网编辑
千家信息网最后更新 2025年02月06日,这篇文章主要介绍了怎么用Dockerfile构建SSH Server,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。一. 相关的文件1.
千家信息网最后更新 2025年02月06日怎么用Dockerfile构建SSH Server

这篇文章主要介绍了怎么用Dockerfile构建SSH Server,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

一. 相关的文件

1. 新建一个目录和一个 Dockerfile
mkdir y109-sshdvim Dockerfile
2. Dockerfile 的内容如下
# docker sshdFROM ubuntu:14.04MAINTAINER y109# 使用 163.com 的源COPY sources.list.163.txt /etc/apt/sources.listRUN apt-get -y update# 设置 root 密码RUN echo 'root:bMg5kesfdsfesx9gD' | chpasswd# 安装 openssh-serverRUN apt-get -y install openssh-serverRUN mkdir /var/run/sshd# SSH login fix. Otherwise user is kicked off after loginRUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd## ENV NOTVISIBLE "in users profile"RUN echo "export VISIBLE=now" >> /etc/profile# 添加公钥(如果没有公钥可以省略)RUN mkdir /root/.sshRUN echo 'ssh-rsa YOU_PUB_KEY' > /root/authorized_keys# 容器启动后运行的程序CMD ["/usr/sbin/sshd", "-D"]# 打开 22 端口EXPOSE 22

sources.list.163.txt 的内容如下

deb http://mirrors.163.com/ubuntu/ precise main restricteddeb-src http://mirrors.163.com/ubuntu/ precise main restricteddeb http://mirrors.163.com/ubuntu/ precise-updates main restricteddeb-src http://mirrors.163.com/ubuntu/ precise-updates main restricteddeb http://mirrors.163.com/ubuntu/ precise universedeb-src http://mirrors.163.com/ubuntu/ precise universedeb http://mirrors.163.com/ubuntu/ precise-updates universedeb-src http://mirrors.163.com/ubuntu/ precise-updates universedeb http://mirrors.163.com/ubuntu/ precise-security main restricteddeb-src http://mirrors.163.com/ubuntu/ precise-security main restricteddeb http://mirrors.163.com/ubuntu/ precise-security universedeb-src http://mirrors.163.com/ubuntu/ precise-security universe

二.构建 Image

使用 docker build 来生成镜像
-t 参数是给这个镜像的 TAG

sudo docker build -t 'y109/sshd' ./Sending build context to Docker daemon 4.608 kBSending build context to Docker daemonStep 0 : FROM ubuntu:14.04 ---> 9cbaf023786cStep 1 : MAINTAINER y109 ---> Using cache ---> 2256ab1cc931Step 2 : COPY sources.list.163.txt /etc/apt/sources.list ---> Using cache ---> 65536ca26964Step 3 : RUN apt-get -y update ---> Using cache ---> 60639e42f098Step 4 : RUN echo 'root:pass123456' | chpasswd ---> Using cache ---> 8644dd20854fStep 5 : RUN apt-get -y install openssh-server ---> Using cache ---> 98039327bca7Step 6 : RUN mkdir /var/run/sshd ---> Using cache ---> 9bd3b3fc7828Step 7 : RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd ---> Using cache ---> d748cb9428a0Step 8 : RUN echo "export VISIBLE=now" >> /etc/profile ---> Using cache ---> e975cd819243Step 9 : RUN mkdir /root/.ssh ---> Using cache ---> e561acc07675Step 10 : RUN echo 'ssh-rsa YOU_PUBLIC_KEY' ---> Using cache ---> 8f6882a72037Step 11 : CMD ["/usr/sbin/sshd", "-D"] ---> Using cache ---> 48cbd2c4aa70Step 12 : EXPOSE 22 ---> Using cache ---> 3101a36f0084Successfully built 3101a36f0084

使用 docker images 命令查看镜像, 确认镜像构建成功了

sudo docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZEy109/sshd           latest              3101a36f0084        22 minutes ago      226.1 MB                            23f604e547b8        28 minutes ago      226.1 MB                            50647a1fb746        36 minutes ago      226.1 MBy...

y109/sshd就是我们刚才构建的镜像

三.创建 Container

使用 docker run 来用镜像创建一个 Container

-d : Detached mode, 使 Container 在 background 模式运行-p : 把 22 端口映射到主机的网卡上, 格式: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort-name : 给 Container 指定一个名字, 一旦指定了名称这个名称就和这个 Container 绑定了, 可以用 docker ps -a 列出来

sudo docker run -d -p 10922:22 --name y109-sshd y109/sshd
我用的外网端口是 10922, 可以根据需要修改, 下一步需要确认 Container 是否正常执行了

sudo docker psCONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMESfc37b83d343e        y109/sshd:latest    "/usr/sbin/sshd -D"   9 seconds ago       Up 9 seconds        0.0.0.0:10922->22/tcp   y109-sshd

看来执行成功了, 连接试试看看

ssh root@localhost -p10922The authenticity of host '[localhost]:10922 ([127.0.0.1]:10922)' can't be established.ECDSA key fingerprint is 4d:48:5c:61:54:d6:8f:62:70:a2:0e:ab:b7:1a:cb:f7.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '[localhost]:10922' (ECDSA) to the list of known hosts.The programs included with the Ubuntu system are free software;the exact distribution terms for each program are described in theindividual files in /usr/share/doc/*/copyright.Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted byapplicable law.root@80f07ad418fe:~#

已经成功连接进入 Container 了

四.关闭 Container

sudo docker stop fc3 fc3 是 Container Id fc37b83d343e 的缩写, 只要能够唯一标识这个 Container 就可以了。或者sudo docker stop y109-sshd

五.运行 Container

sudo docker start y109-sshd

感谢你能够认真阅读完这篇文章,希望小编分享的"怎么用Dockerfile构建SSH Server"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

0