您好,登錄后才能下訂單哦!
本篇內容主要講解“Docker中怎么構建SSH鏡像”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Docker中怎么構建SSH鏡像”吧!
[root@test01 ~]# yum install docker[root@test01 ~]# systemctl enable docker[root@test01 ~]# systemctl start docker
使用docker run命令時,Docker會自動的先查找本地的鏡像,如果沒有找到,會繼續向docker hub上查找并下載。我習慣先下載下來docker pull centos 默認本系統版本的最新版,如果指定版本,加上冒號和版本號
[root@test01 ~]# docker pull centos:7.4.1708[root@test01 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos 7.4.1708 3afd47092a0e 3 months ago 196.6 MB
[root@test01 ~]# docker run -it --name="ssh_server" centos:7.4.1708 /bin/bash[root@ffe61e183a6c /]#
通過yum安裝,檢查yum源配置是否正確,centos7的默認和主機的一樣
yum install openssh-server 安裝ssh服務程序 yum install net-tools 安裝網絡工具,用來查看端口,可不安裝 [root@ffe61e183a6c /]# yum install openssh-server net-tools
配置sshserver服務
使用ssh-keygen生成必要的密鑰
[root@ffe61e183a6c /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key[root@ffe61e183a6c /]# ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key[root@ffe61e183a6c /]# ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
啟動ssh服務器,并查看是否啟動成功
/usr/sbin/sshd -D &
此處的-D參數用于告訴SSH服務不以守護進程運行,而是和運行終端關聯,有了運行終端,容器就不會退出
[root@ffe61e183a6c /]# /usr/sbin/sshd -D &[1] 82 [root@ffe61e183a6c /]# netstat -tunplaActive Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 82/sshd tcp6 0 0 :::22 :::* LISTEN 82/sshd [root@ffe61e183a6c /]# pkill sshd
至此,我們可以證實ssh服務啟動沒有問題,接下來我們編寫啟動腳本,用于啟動容器的時候運行,因為容器啟動時只能運行一個命令,一般這個命令用來啟動腳本
[root@ffe61e183a6c ~]# cat run.sh#!/bin/bash/usr/sbin/sshd -D [root@ffe61e183a6c ~]# chmod 775 run.sh
使用docker commit將剛才的容器提交為一個新的鏡像
[root@ffe61e183a6c ~]# exitexit[root@test01 ~]#[root@test01 ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ffe61e183a6c centos:7.4.1708 "/bin/bash" 19 minutes ago Exited (0) 8 seconds ago ssh_server [root@test01 ~]# docker commit ffe61e183a6c ssh:commitsha256:be55c135e6141481aff3218b7a269b27d8f0faa295ed244849bf8ccf7ad1c7b1 [root@test01 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE ssh commit be55c135e614 11 seconds ago 296.5 MB docker.io/centos 7.4.1708 3afd47092a0e 3 months ago 196.6 MB
[root@test01 ~]# docker run -d -p 2022:22 ssh:commit /root/run.sh6d5628a2a336bc302fa45baf6e6a1d5ade2f6dd42a4697553c6e3dda1a0a3226 [root@test01 ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6d5628a2a336 ssh:commit "/root/run.sh" 8 seconds ago Up 6 seconds 0.0.0.0:2022->22/tcp prickly_bell
剛才忘記給docker鏡像設置密碼了,這次需要給設置一下密碼
[root@test01 ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6d5628a2a336 ssh:commit "/root/run.sh" 6 minutes ago Exited (137) 4 minutes ago prickly_bell ffe61e183a6c centos:7.4.1708 "/bin/bash" 29 minutes ago Exited (0) 9 minutes ago ssh_server [root@test01 ~]# docker run -it ssh:commit /bin/bash[root@0204e7257a24 /]# passwd rootChanging password for user root. New password: Retype new password: passwd: all authentication tokens updated successfully. [root@0204e7257a24 /]# exitexit[root@test01 ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0204e7257a24 ssh:commit "/bin/bash" 29 seconds ago Exited (0) 4 seconds ago trusting_borg 6d5628a2a336 ssh:commit "/root/run.sh" 9 minutes ago Exited (137) 7 minutes ago prickly_bell ffe61e183a6c centos:7.4.1708 "/bin/bash" 32 minutes ago Exited (0) 12 minutes ago ssh_server [root@test01 ~]# docker commit 0204e7257a24 ssh02:commitsha256:b92a3cde4c9162cf12ac9cf61a61ce0332d3755b7708e4037c4df09b4e794177
[root@test01 ~]# docker run -d -p 2022:22 ssh02:commit /root/run.sh357ed4074c5d7f1ec1fe0df6af9c9a3162c70fa5624f7742bf59f309d9842247
[root@test01 ~]# ssh root@192.168.1.60 -p2022root@192.168.1.60's password: [root@357ed4074c5d ~]# exit [root@test01 ~]# docker stop 357ed4074c5d
創建一個存放生成鏡像相關文件的目錄
該目錄下需要創建2個文件:Dockerfile、run.sh。Dockerfile用于構建鏡像,run.sh是啟動SSH服務的腳本
mkdir ssh_dockerfile && cd ssh_dockerfile
[root@test01 ssh_dockerfile]# cat Dockerfile#使用的基礎鏡像FROM centos:7.4.1708#添加作者信息MAINTAINER liuxin 842887233@qq.com#安裝SSH服務RUN yum install -y openssh-server#添加必要的密鑰RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key RUN ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key#添加啟動文件ADD run.sh /root/run.sh RUN chmod 775 /root/run.sh#導出端口EXPOSE 22#設置默認啟動命令CMD ["/root/run.sh"] [root@test01 ssh_dockerfile]# cat run.sh#!/bin/bash/usr/sbin/sshd -D
[root@test01 ssh_dockerfile]# docker build ./Sending build context to Docker daemon 3.072 kB Step 1 : FROM centos:7.4.1708 ---> 3afd47092a0e Step 2 : MAINTAINER liuxin 842887233@qq.com ---> Using cache ---> bd64810df0bc Step 3 : RUN yum install -y openssh-server ---> Using cache ---> 5dc6301a0304 Step 4 : RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key ---> Using cache ---> 0ce92e5baa9f Step 5 : RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key ---> Using cache ---> fcb2bcf78ea0 Step 6 : RUN ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key ---> Using cache ---> 7eae01e47ee2 Step 7 : ADD run.sh /root/run.sh ---> 4d07a723ffcf Removing intermediate container 0b137a9274be Step 8 : RUN chmod 775 /root/run.sh ---> Running in 1d5a9524da86 ---> 324868eb5780 Removing intermediate container 1d5a9524da86 Step 9 : EXPOSE 22 ---> Running in ada62bb87978 ---> a0b3df156e21 Removing intermediate container ada62bb87978 Step 10 : CMD /root/run.sh ---> Running in 4f5031577ff4 ---> 8679c00088ef Removing intermediate container 4f5031577ff4 Successfully built 8679c00088ef [root@test01 ssh_dockerfile]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE 8679c00088ef About a minute ago 295.9 MB ssh02 commit b92a3cde4c91 55 minutes ago 296.5 MB ssh commit be55c135e614 About an hour ago 296.5 MB docker.io/centos 7.4.1708 3afd47092a0e 3 months ago 196.6 MB
[root@test01 ssh_dockerfile]# docker run -d -p 2022:22 8679c00088efe73a441afc8df35f42a30974c8697278fe6d35c1ac711d13ec817e74ffbf4008 [root@test01 ssh_dockerfile]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e73a441afc8d 8679c00088ef "/root/run.sh" 14 seconds ago Up 12 seconds 0.0.0.0:2022->22/tcp fervent_yonath
到此,相信大家對“Docker中怎么構建SSH鏡像”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。