久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

怎么用Dockerfile構(gòu)建SSH Server

共計(jì) 4418 個(gè)字符,預(yù)計(jì)需要花費(fèi) 12 分鐘才能閱讀完成。

這篇文章主要介紹了怎么用 Dockerfile 構(gòu)建 SSH Server,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓丸趣 TV 小編帶著大家一起了解一下。

一. 相關(guān)的文件 1. 新建一個(gè)目錄和一個(gè) Dockerfile

mkdir y109-sshd
vim Dockerfile

2. Dockerfile 的內(nèi)容如下

# docker sshd
FROM ubuntu:14.04
MAINTAINER y109 y109@qq.com 
#  使用  163.com  的源
COPY sources.list.163.txt /etc/apt/sources.list
RUN apt-get -y update
#  設(shè)置  root  密碼
RUN echo  root:bMg5kesfdsfesx9gD  | chpasswd
#  安裝  openssh-server
RUN apt-get -y install openssh-server
RUN mkdir /var/run/sshd
# SSH login fix. Otherwise user is kicked off after login
RUN 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/.ssh
RUN echo  ssh-rsa YOU_PUB_KEY    /root/authorized_keys
#  容器啟動(dòng)后運(yùn)行的程序
CMD [/usr/sbin/sshd ,  -D]
#  打開  22  端口
EXPOSE 22

sources.list.163.txt 的內(nèi)容如下

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

二. 構(gòu)建 Image

使用 docker build 來生成鏡像
-t 參數(shù)是給這個(gè)鏡像的 TAG

sudo docker build -t  y109/sshd  ./
Sending build context to Docker daemon 4.608 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:14.04
 ---  9cbaf023786c
Step 1 : MAINTAINER y109 y109@qq.com 
 ---  Using cache
 ---  2256ab1cc931
Step 2 : COPY sources.list.163.txt /etc/apt/sources.list
 ---  Using cache
 ---  65536ca26964
Step 3 : RUN apt-get -y update
 ---  Using cache
 ---  60639e42f098
Step 4 : RUN echo  root:pass123456  | chpasswd
 ---  Using cache
 ---  8644dd20854f
Step 5 : RUN apt-get -y install openssh-server
 ---  Using cache
 ---  98039327bca7
Step 6 : RUN mkdir /var/run/sshd
 ---  Using cache
 ---  9bd3b3fc7828
Step 7 : RUN sed  s@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g  -i /etc/pam.d/sshd
 ---  Using cache
 ---  d748cb9428a0
Step 8 : RUN echo  export VISIBLE=now    /etc/profile
 ---  Using cache
 ---  e975cd819243
Step 9 : RUN mkdir /root/.ssh
 ---  Using cache
 ---  e561acc07675
Step 10 : RUN echo  ssh-rsa YOU_PUBLIC_KEY 
 ---  Using cache
 ---  8f6882a72037
Step 11 : CMD [/usr/sbin/sshd ,  -D]
 ---  Using cache
 ---  48cbd2c4aa70
Step 12 : EXPOSE 22
 ---  Using cache
 ---  3101a36f0084
Successfully built 3101a36f0084

使用 docker images 命令查看鏡像, 確認(rèn)鏡像構(gòu)建成功了

sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
y109/sshd latest 3101a36f0084 22 minutes ago 226.1 MB
 none   none  23f604e547b8 28 minutes ago 226.1 MB
 none   none  50647a1fb746 36 minutes ago 226.1 MB
...

y109/sshd 就是我們剛才構(gòu)建的鏡像

三. 創(chuàng)建 Container

使用  docker run  來用鏡像創(chuàng)建一個(gè) Container

-d : Detached mode,  使  Container  在  background  模式運(yùn)行
-p :  把  22  端口映射到主機(jī)的網(wǎng)卡上,  格式: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort
–name :  給  Container  指定一個(gè)名字,  一旦指定了名稱這個(gè)名稱就和這個(gè)  Container  綁定了,  可以用  docker ps -a  列出來 

sudo docker run -d -p 10922:22 –name y109-sshd y109/sshd
我用的外網(wǎng)端口是 10922, 可以根據(jù)需要修改, 下一步需要確認(rèn) Container 是否正常執(zhí)行了

sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc37b83d343e y109/sshd:latest  /usr/sbin/sshd -D  9 seconds ago Up 9 seconds 0.0.0.0:10922- 22/tcp y109-sshd

看來執(zhí)行成功了, 連接試試看看

ssh root@localhost -p10922
The 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)? yes
Warning: 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 the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
root@80f07ad418fe:~#

已經(jīng)成功連接進(jìn)入 Container 了

四. 關(guān)閉 Container

sudo docker stop fc3 fc3 是 Container Id fc37b83d343e 的縮寫, 只要能夠唯一標(biāo)識(shí)這個(gè) Container 就可以了。或者 sudo docker stop y109-sshd

五. 運(yùn)行 Container

sudo docker start y109-sshd

感謝你能夠認(rèn)真閱讀完這篇文章,希望丸趣 TV 小編分享的“怎么用 Dockerfile 構(gòu)建 SSH Server”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持丸趣 TV,關(guān)注丸趣 TV 行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-08-16發(fā)表,共計(jì)4418字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒有評(píng)論)
主站蜘蛛池模板: 日土县| 商丘市| 文昌市| 正定县| 红安县| 富蕴县| 山西省| 诸城市| 丹东市| 澄城县| 博客| 大余县| 封开县| 岗巴县| 山阴县| 高清| 宜阳县| 石门县| 泌阳县| 陇西县| 齐齐哈尔市| 台中市| 额济纳旗| 东乌| 察隅县| 阜阳市| 昆山市| 满洲里市| 会同县| 昌图县| 礼泉县| 吉水县| 武功县| 和龙市| 呼图壁县| 龙门县| 陆丰市| 汾西县| 泾阳县| 温泉县| 噶尔县|