這篇“Docker如何定制容器鏡像”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“Docker如何定制容器鏡像”文章吧。
成都創(chuàng)新互聯(lián)公司-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比昂仁網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式昂仁網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋昂仁地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
一、需求
由于在測(cè)試環(huán)境中使用了docker官網(wǎng)的centos 鏡像,但是該鏡像里面默認(rèn)沒(méi)有安裝ssh服務(wù),在做測(cè)試時(shí)又需要開(kāi)啟ssh。所以上網(wǎng)也查了查資料。
二、鏡像定制
第一種方式(手動(dòng)修改容器鏡像)
1.先下載centos鏡像
[root@docker ~]# docker pull centos
2.啟動(dòng)容器并進(jìn)行配置
啟動(dòng)容器,
[root@docker ~]# docker run -it -d --name test-centos1 centos d72250ecaa5e3e36226a1edd749f494d9f00eddc4143c81ac3565aa4e551791a
命令注釋?zhuān)?it : 進(jìn)行交互式操作
-d : 等同于 -d=true,容器將會(huì)在后臺(tái)運(yùn)行,不然執(zhí)行一次命令后,退出后,便是exit狀態(tài)了。
--name : 容器啟動(dòng)后的名字,默認(rèn)不指定,將會(huì)隨機(jī)產(chǎn)生一個(gè)名字。或者使用 -name="containers_name"
centos:使用的鏡像名稱
進(jìn)入容器,安裝ssh server,以及配置開(kāi)機(jī)啟動(dòng)
[root@docker ~]# docker exec -it test-centos1 /bin/bash [root@d72250ecaa5e /]# ifconfig bash: ifconfig: command not found
注:命令最后參數(shù) /bin/bash: 指進(jìn)入容器時(shí)執(zhí)行的命令(command)
我們檢查了下容器,暫時(shí)安裝以下必用的軟件吧 net-tools,openssh-server
[root@d72250ecaa5e /]# yum install openssh-server net-tools -y
創(chuàng)建ssh 所需的目錄,并在根目錄創(chuàng)建sshd 啟動(dòng)腳本
[root@d72250ecaa5e /]# mkdir -pv /var/run/sshd mkdir: created directory '/var/run/sshd' [root@d72250ecaa5e /]# cat /auto_sshd.sh #!/bin/bash /usr/sbin/sshd -d [root@d72250ecaa5e /]# chmod +x /auto_sshd.sh
修改容器內(nèi)root 的賬戶密碼
[root@d72250ecaa5e /]# echo "root:iloveworld" | chpasswd
生成ssh 主機(jī)dsa 密鑰(不然ssh 該容器時(shí),會(huì)出現(xiàn)錯(cuò)誤。)
[root@d72250ecaa5e /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key [root@d72250ecaa5e /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
我們加一個(gè)history記錄的時(shí)間功能吧,這樣方便后期查看
echo 'export histtimeformat="%f %t `whoami` "' >> /etc/profile
ok,配置基本完畢咯。清理命令歷史紀(jì)錄,之后退出容器?,F(xiàn)在可以生成一個(gè)新的docker 鏡像了。
3.配置完成后,進(jìn)行打包成新的鏡像
[root@docker ~]# docker commit test-centos1 centos_sshd:7.0 sha256:6e3330b30dfff5f029f102874e54cfffffbc37dcf2a4eb7304c817148fbc944d [root@docker ~]# docker images repository tag image id created size centos_sshd 7.0 6e3330b30dff 8 seconds ago 310.1 mb docker.io/ubuntu latest e4415b714b62 12 days ago 128.1 mb
命令注釋?zhuān)篶ommit: 提交一個(gè)具有新配置的容器成為鏡像,后面跟容器的name 或者容器id ,最后是生成新鏡像的名字
更新:這條命令更方便以后啟動(dòng),如下:
[root@docker ~]# docker commit --change='cmd ["/auto_sshd.sh"]' -c "expose 22" test-centos1 centos_sshd:7.0 sha256:7bb4efd82c4ff1f241cbc57ee45aab1b05d214b1e9fcd51196696c67d480e70b
命令注釋?zhuān)?--change : 將后期使用此鏡像運(yùn)行容器時(shí)的命令參數(shù)、開(kāi)放的容器端口提前設(shè)置好。
4.驗(yàn)證
查看鏡像,并啟動(dòng)新的容器
[root@docker ~]# docker images repository tag image id created size centos_sshd 7.0 7bb4efd82c4f 4 minutes ago 310.1 mb docker.io/ubuntu latest e4415b714b62 12 days ago 128.1 mb [root@docker ~]# docker run -d -it --name centos_7.0-1 centos_sshd:7.0 ec17e553d5c4c60865afeb99df8dfd1f4e7d4ba6e1b0d5516f9127f09d1d6356 [root@docker ~]# docker ps -a container id image command created status ports names ec17e553d5c4 centos_sshd:7.0 "/auto_sshd.sh" 6 seconds ago up 5 seconds 22/tcp centos_7.0-1
進(jìn)行ssh測(cè)試,先查看一下該容器的ip,之后ssh。ok
[root@docker ~]# docker exec centos_7.0-1 hostname -i 172.17.0.4 [root@docker ~]# ssh root@172.17.0.4 the authenticity of host '172.17.0.4 (172.17.0.4)' can't be established. rsa key fingerprint is 87:88:07:12:ac:0a:90:28:10:e1:9e:eb:1f:d6:c9:9d. are you sure you want to continue connecting (yes/no)? yes warning: permanently added '172.17.0.4' (rsa) to the list of known hosts. root@172.17.0.4's password: last login: tue nov 29 16:00:49 2016 from gateway [root@ec17e553d5c4 ~]# w 16:34:17 up 63 days, 7:49, 1 user, load average: 0.00, 0.02, 0.05 user tty from login@ idle jcpu pcpu what root pts/0 gateway 16:34 1.00s 0.00s 0.00s w [root@ec17e553d5c4 ~]# ping gateway ping gateway (172.17.0.1) 56(84) bytes of data. 64 bytes from gateway (172.17.0.1): icmp_seq=1 ttl=64 time=0.048 ms
第二種方式(推薦:利用dockerfile文件)
我的認(rèn)為它就像ansible的playbook一樣。dockerfile包含創(chuàng)建鏡像所需要的全部指令?;谠赿ockerfile中的指令,我們可以使用docker build命令來(lái)創(chuàng)建鏡像。通過(guò)減少鏡像和容器的創(chuàng)建過(guò)程來(lái)簡(jiǎn)化部署。
1.創(chuàng)建dockerfile文件
新建一個(gè)目錄,在里面新建一個(gè)dockerfile文件(新建一個(gè)的目錄,主要是為了和以防和其它dockerfile混亂 )
[root@docker ~]# mkdir centos7-dockerfile [root@docker centos7-dockerfile]# cat dockerfile # the dockerfile has change add sshd services on centos7.0 #centos7:latest image from centos:latest maintainer yifeng,http://www.cnblogs.com/hanyifeng #install sshd net-tools run yum install openssh-server net-tools -y run mkdir /var/run/sshd #set password for root run echo 'root:iloveworld' | chpasswd run sed -i 's/permitrootlogin prohibit-password/permitrootlogin yes/' /etc/ssh/sshd_config #set history record env histtimeformat "%f %t " #fix sshd service:read from socket failed: connection reset by peer? run ssh-keygen -a #change timezone cst run \cp /usr/share/zoneinfo/asia/shanghai /etc/localtime #open 22 port expose 22 #auto running sshd service cmd ["/usr/sbin/sshd","-d"]
上述文件內(nèi)容就是一個(gè)dockerfile 常見(jiàn)的命令組合。開(kāi)頭帶#號(hào)的為注釋
文件解釋?zhuān)?/strong>
from: 必不可少的命令,從某個(gè)鏡像作為基。如 from
maintainer:標(biāo)明該dockerfile作者及聯(lián)系方式,可忽略不寫(xiě)
run:建立新的鏡像時(shí),可以執(zhí)行在系統(tǒng)里的命令,如安裝特定的軟件以及設(shè)置環(huán)境變量。
env:設(shè)置系統(tǒng)環(huán)境變量(注意:寫(xiě)在/etc/profile里的命令在dockerfile這里會(huì)不生效,所以為改成env的方式)
expose:開(kāi)放容器內(nèi)的端口,但不和宿主機(jī)進(jìn)行映射。方便在宿主機(jī)上進(jìn)行開(kāi)發(fā)測(cè)試。(如需映射到宿主機(jī)端口,可在運(yùn)行容器時(shí)使用 -p host_port:container_port)
cmd:設(shè)置執(zhí)行的命令,經(jīng)常用于容器啟動(dòng)時(shí)指定的某個(gè)操作。如執(zhí)行自定義腳本服務(wù),或者是執(zhí)行系統(tǒng)命令。cmd 只能存在一條,如在dockerfile中有多條cmd的話,只有最后一條cmd生效!
2.執(zhí)行build 創(chuàng)建鏡像
使用docker build命令來(lái)創(chuàng)建鏡像
[root@docker centos7-dockerfile]# docker build -t centos_sshd_1 .
-t 選項(xiàng)來(lái)docker build新的鏡像以便于標(biāo)記構(gòu)建的鏡像,. 表示當(dāng)前目錄,也可以指定dockerfile 文件所在目錄。
下面縮略的內(nèi)容是構(gòu)建鏡像時(shí)的輸出,可以看下。
[root@docker centos7-dockerfile]# docker build -t centos_sshd_1 . sending build context to docker daemon 4.096 kb step 1 : from centos:latest ---> 0584b3d2cf6d step 2 : maintainer yifeng,http://www.cnblogs.com/hanyifeng ---> running in da643b55dc77 ---> 1087074d44e4 removing intermediate container da643b55dc77 step 3 : run yum install openssh-server net-tools -y ---> running in 5626d8f0f892 loaded plugins: fastestmirror, ovl determining fastest mirrors * base: mirrors.btte.net * extras: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.btte.net resolving dependencies --> running transaction check ---> package net-tools.x86_64 0:2.0-0.17.20131004git.el7 will be installed ---> package openssh-server.x86_64 0:6.6.1p1-25.el7_2 will be installed --> processing dependency: openssh = 6.6.1p1-25.el7_2 for package: openssh-server-6.6.1p1-25.el7_2.x86_64 --> processing dependency: fipscheck-lib(x86-64) >= 1.3.0 for package: openssh-server-6.6.1p1-25.el7_2.x86_64 --> processing dependency: libwrap.so.0()(64bit) for package: openssh-server-6.6.1p1-25.el7_2.x86_64 --> processing dependency: libfipscheck.so.1()(64bit) for package: openssh-server-6.6.1p1-25.el7_2.x86_64 --> running transaction check ---> package fipscheck-lib.x86_64 0:1.4.1-5.el7 will be installed --> processing dependency: /usr/bin/fipscheck for package: fipscheck-lib-1.4.1-5.el7.x86_64 ---> package openssh.x86_64 0:6.6.1p1-25.el7_2 will be installed ---> package tcp_wrappers-libs.x86_64 0:7.6-77.el7 will be installed --> running transaction check ---> package fipscheck.x86_64 0:1.4.1-5.el7 will be installed --> finished dependency resolution dependencies resolved ================================================================================ package arch version repository size ================================================================================ installing: net-tools x86_64 2.0-0.17.20131004git.el7 base 304 k openssh-server x86_64 6.6.1p1-25.el7_2 updates 436 k installing for dependencies: fipscheck x86_64 1.4.1-5.el7 base 21 k fipscheck-lib x86_64 1.4.1-5.el7 base 11 k openssh x86_64 6.6.1p1-25.el7_2 updates 435 k tcp_wrappers-libs x86_64 7.6-77.el7 base 66 k transaction summary ================================================================================ install 2 packages (+4 dependent packages) total download size: 1.2 m installed size: 3.4 m downloading packages: public key for fipscheck-lib-1.4.1-5.el7.x86_64.rpm is not installed warning: /var/cache/yum/x86_64/7/base/packages/fipscheck-lib-1.4.1-5.el7.x86_64.rpm: header v3 rsa/sha256 signature, key id f4a80eb5: nokey public key for openssh-6.6.1p1-25.el7_2.x86_64.rpm is not installed -------------------------------------------------------------------------------- total 593 kb/s | 1.2 mb 00:02 retrieving key from file:///etc/pki/rpm-gpg/rpm-gpg-key-centos-7 importing gpg key 0xf4a80eb5: userid : "centos-7 key (centos 7 official signing key)" fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 package : centos-release-7-2.1511.el7.centos.2.10.x86_64 (@centos) from : /etc/pki/rpm-gpg/rpm-gpg-key-centos-7 running transaction check running transaction test transaction test succeeded running transaction installing : fipscheck-1.4.1-5.el7.x86_64 1/6 installing : fipscheck-lib-1.4.1-5.el7.x86_64 2/6 installing : openssh-6.6.1p1-25.el7_2.x86_64 3/6 installing : tcp_wrappers-libs-7.6-77.el7.x86_64 4/6 installing : openssh-server-6.6.1p1-25.el7_2.x86_64 5/6 installing : net-tools-2.0-0.17.20131004git.el7.x86_64 6/6 verifying : openssh-6.6.1p1-25.el7_2.x86_64 1/6 verifying : openssh-server-6.6.1p1-25.el7_2.x86_64 2/6 verifying : net-tools-2.0-0.17.20131004git.el7.x86_64 3/6 verifying : tcp_wrappers-libs-7.6-77.el7.x86_64 4/6 verifying : fipscheck-lib-1.4.1-5.el7.x86_64 5/6 verifying : fipscheck-1.4.1-5.el7.x86_64 6/6 installed: net-tools.x86_64 0:2.0-0.17.20131004git.el7 openssh-server.x86_64 0:6.6.1p1-25.el7_2 dependency installed: fipscheck.x86_64 0:1.4.1-5.el7 fipscheck-lib.x86_64 0:1.4.1-5.el7 openssh.x86_64 0:6.6.1p1-25.el7_2 tcp_wrappers-libs.x86_64 0:7.6-77.el7 complete! ---> 7b249ed8cb54 removing intermediate container 5626d8f0f892 step 4 : run mkdir /var/run/sshd ---> running in fc94a139d438 ---> ea2826eccc91 removing intermediate container fc94a139d438 step 5 : run echo 'root:iloveworld' | chpasswd ---> running in ba53283081a7 ---> 7ce1ddb5d9c0 removing intermediate container ba53283081a7 step 6 : run sed -i 's/permitrootlogin prohibit-password/permitrootlogin yes/' /etc/ssh/sshd_config ---> running in 4112281a5bf0 ---> be21fb6b5b1e removing intermediate container 4112281a5bf0 step 7 : env histtimeformat "%f %t " ---> running in f2081726e403 ---> f3fafca42170 removing intermediate container f2081726e403 step 8 : run ssh-keygen -a ---> running in 2ca9e743dee7 ssh-keygen: generating new host keys: rsa1 rsa dsa ecdsa ed25519 ---> 1a927943bee7 removing intermediate container 2ca9e743dee7 step 9 : run \cp /usr/share/zoneinfo/asia/shanghai /etc/localtime ---> running in afd43cc6d4d6 ---> 4a0cacf6cd72 removing intermediate container afd43cc6d4d6 step 10 : expose 22 ---> running in a03551bc3bcb ---> 3af544106bf4 removing intermediate container a03551bc3bcb step 11 : cmd /usr/sbin/sshd -d ---> running in f45fe5eb5561 ---> d4620c9949b8 removing intermediate container f45fe5eb5561 successfully built d4620c9949b8
3.查看鏡像列表,并創(chuàng)建容器
[root@docker centos7-dockerfile]# docker images repository tag image id created size centos_sshd_1 latest d4620c9949b8 4 minutes ago 308.4 mb centos_sshd 7.0 7bb4efd82c4f 2 days ago 310.1 mb
我們剛剛新建的容器已經(jīng)存在了,現(xiàn)在用它來(lái)創(chuàng)建容器
[root@docker centos7-dockerfile]# docker run -d -it --name centos-two centos_sshd_1 7ae51091c138d249b5e97f6957073e748db278c0f1cf856e968ca78a4aec1a5b
查看容器
[root@docker centos7-dockerfile]# docker ps container id image command created status ports names 7ae51091c138 centos_sshd_1 "/usr/sbin/sshd -d" 16 seconds ago up 15 seconds 22/tcp centos-two
可以看到容器的command 就是我們之前定義啟動(dòng)ssh 服務(wù)的,并且開(kāi)放了22端口。
現(xiàn)在我們?cè)谒拗鳈C(jī)上查看下該容器的ip,然后用ssh 鏈接進(jìn)去。
[root@docker ~]# docker exec centos-two hostname -i 172.17.0.7 [root@docker ~]# ssh root@172.17.0.7 the authenticity of host '172.17.0.7 (172.17.0.7)' can't be established. ecdsa key fingerprint is 7a:38:69:d7:5e:f4:db:e8:3c:ea:92:a4:1a:a1:7b:9a. are you sure you want to continue connecting (yes/no)? yes warning: permanently added '172.17.0.7' (ecdsa) to the list of known hosts. root@172.17.0.7's password: [root@7ae51091c138 ~]# w 11:19:34 up 65 days, 18:34, 1 user, load average: 0.01, 0.04, 0.05 user tty from login@ idle jcpu pcpu what root pts/0 gateway 11:19 6.00s 0.00s 0.00s w
以上就是關(guān)于“Docker如何定制容器鏡像”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。