久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      Dockerfile與Dockerfile實(shí)戰(zhàn)

      前言

      ?先前我們?cè)谥v述docker的鏡像構(gòu)建時(shí)對(duì)Dockerfile做了初步介紹,本文將結(jié)合上次的內(nèi)容做更加細(xì)致的介紹,從回顧到實(shí)戰(zhàn)演練講述Dockerfile,本文先通過三個(gè)簡單的案例感受、理解Dockerfile,主要是寫httpd、sshd、以及systemctl的Dockerfile,之后會(huì)進(jìn)行對(duì)其他服務(wù)如Nginx、Tomcat以及MySQL編寫并且測試Dockerfile。

      • 揭開Docker的面紗 – 基礎(chǔ)理論梳理和安裝流程演示  http://www.sfodin.cn/Linux/2020-04/163003.htm
      • Docker基礎(chǔ)命令詳解 – 鏡像及容器操作  http://www.sfodin.cn/Linux/2020-04/163005.htm
      • 深入理解Docker的硬件資源控制與驗(yàn)證  http://www.sfodin.cn/Linux/2020-04/163006.htm
      • Docker網(wǎng)絡(luò)模式與配置Docker自定義網(wǎng)絡(luò)(bridge模式)  http://www.sfodin.cn/Linux/2020-04/163007.htm
      • Docker構(gòu)建鏡像的三種方式(Dockerfile初步)  http://www.sfodin.cn/Linux/2020-04/163008.htm
      • 教你如何搭建Docker私有倉庫  http://www.sfodin.cn/Linux/2020-04/163009.htm
      • 深入理解Docker數(shù)據(jù)管理與端口映射  http://www.sfodin.cn/Linux/2020-04/163010.htm

      回顧Dockerfile

      ? 說到Dockerfile,就離不開Dockerfile的核心組件,尤其是鏡像。鏡像是運(yùn)行容器的基礎(chǔ)環(huán)境,也就是說鏡像是docker容器創(chuàng)建的關(guān)鍵,而創(chuàng)建鏡像的三種方式之一的Dockerfile是最為靈活的。

      什么是Dockerfile?

      ? Dockerfile可以看做是被Docker程序所解釋翻譯的腳本,由一組命令集合而成,每一條命令都對(duì)應(yīng)一條操作命令,有其翻譯為Linux下的具體命令。用戶可以通過自定義內(nèi)容來快速構(gòu)建鏡像。

      ? 其實(shí)說簡單點(diǎn),你可以認(rèn)為Dockerfile是“專門用于構(gòu)建鏡像的shell腳本”。

      ? 還記得Dockerfile的嚴(yán)格格式嗎?我們先來看一下這個(gè)表格。

      Dockerfile與Dockerfile實(shí)戰(zhàn)

      ? Dockerfile是一種分層結(jié)構(gòu),其中的指令每一條都會(huì)構(gòu)建一層鏡像及容器,只不過這是臨時(shí)的,除了基礎(chǔ)鏡像,其他中間產(chǎn)生的容器最后都會(huì)被清除。當(dāng)然有時(shí)候會(huì)出現(xiàn)一些“無名氏”鏡像,標(biāo)志就是使用docker images命令時(shí)ID和tag都是none。

      ? 出現(xiàn)none鏡像的原因有兩類:一則是好的none鏡像;二則是壞的none鏡像。好的none鏡像:代表的是中間鏡像,你可以認(rèn)為是父鏡像的子鏡像,可以使用docker images -a查看。這類鏡像不會(huì)占用磁盤空間,但是占用了屏幕顯示空間。壞的none鏡像:這類可能會(huì)導(dǎo)致磁盤空間問題。一般這種情況是舊鏡像更新產(chǎn)生。

      ? 因此,一般情況我們都可以通過命令將none鏡像刪除(實(shí)戰(zhàn)中會(huì)給出)。

      Dockerfile的作用是什么?

      ? Dockerfile的核心作用就是用戶可以靈活、快速、支持自定義構(gòu)建所需鏡像。

      簡述docker執(zhí)行Dockerfile流程以及構(gòu)建使用鏡像過程

      docker執(zhí)行Dockerfile流程

      (1)docker從基礎(chǔ)鏡像運(yùn)行一個(gè)容器;
      (2)執(zhí)行一條指令并對(duì)容器作出修改;
      (3)執(zhí)行類似docker commit的操作提交一個(gè)新的鏡像層;
      (4)docker再基于剛提交的鏡像運(yùn)行一個(gè)新容器;
      (5)執(zhí)行dockerfile中的下一條指令直到所有指令都執(zhí)行完成。

      構(gòu)建使用鏡像過程

      構(gòu)建鏡像命令舉例:docker build -t image_name . (不要忽視這個(gè)點(diǎn))

      使用鏡像命令舉例:docker run -d -P image_name

      最后使用docker ps -a 查看容器運(yùn)行狀態(tài),如果是up狀態(tài)就可以鏡像測試驗(yàn)證了。

      Dockerfile實(shí)戰(zhàn)

      1、構(gòu)建httpd服務(wù)鏡像

      首先創(chuàng)建工作目錄

      mkdir apache  cd apache

      編寫Dockerfile

      vim Dockerfile

      #基于的基礎(chǔ)鏡像  FROM CentOS  #維護(hù)鏡像的用戶信息  MAINTAINER lokott@lokott.org  #鏡像操作指令安裝Apache軟件  RUN yum -y update  RUN yum -y install httpd  #開啟 80端口  EXPOSE 80  #復(fù)制網(wǎng)站首頁文件  ADD index.html /var/www/html/index.html  #將執(zhí)行腳本復(fù)制到鏡像中  ADD run.sh /run.sh  RUN chmod 755 /run.sh  #啟動(dòng)容器是執(zhí)行腳本  CMD ["/run.sh"]

      其中注意:run 命令可以有多條CMD只能有一條,若有多條則只會(huì)執(zhí)行最后一條

      編寫啟動(dòng)httpd服務(wù)的shell腳本

      vim run.sh

      #!/bin/bash  rm -rf /run/httpd/*  exec /usr/sbin/apachectl -D FOREGROUND  

      編寫測試頁面

      vim index.html

      <h1>this is docker httpd web</h1> 

      使用tree命令查看目錄的文件結(jié)構(gòu)

      [root@localhost apache]# tree ./  ./  ├── Dockerfile  ├── index.html  └── run.sh    0 directories, 3 files  

      構(gòu)建和使用鏡像(創(chuàng)建運(yùn)行容器)

      [root@localhost apache]# docker build -t httpd:new .  #因?yàn)槲抑耙呀?jīng)構(gòu)建過,所以很快,此處案例顯示過程是為了體現(xiàn)Dockerfile執(zhí)行時(shí)的特征:分層和中間容器及鏡像  Sending build context to Docker daemon  4.096kB  Step 1/9 : FROM centos:7   ---> 5e35e350aded  Step 2/9 : MAINTAINER lokott@123.com   ---> Using cache  ---> 3a68b2812314  Step 3/9 : RUN yum -y update   ---> Using cache  ---> ecf1ecb0a774  Step 4/9 : RUN yum install -y httpd   ---> Using cache  ---> ae8c1ee32fbd  Step 5/9 : EXPOSE 80   ---> Using cache  ---> 29f12f1f7490  Step 6/9 : ADD index.html /var/www/html/index.html   ---> Using cache  ---> f56113e6b984  Step 7/9 : ADD run.sh /run.sh   ---> Using cache  ---> 886bf9e654ab  Step 8/9 : RUN chmod +x /run.sh   ---> Using cache  ---> bf53e19ad44f  Step 9/9 : CMD ["/run.sh"]   ---> Using cache  ---> 9500f0aefd1d  Successfully built 9500f0aefd1d  Successfully tagged httpd:new    [root@localhost apache]# docker images  REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE  httpd                 new                 9500f0aefd1d        35 seconds ago      524MB  centos                7                   5e35e350aded        5 months ago        203MB  ...//之后的案例將不再查看鏡像了哈!  #基于構(gòu)建的鏡像創(chuàng)建并運(yùn)行容器,給容器取名為test  [root@localhost apache]# docker run --name test -d -P httpd:new    b7ec122849c61e36adb4a8891a87126afb53b1d5edfa2fda2a1ea18afa1a3169  [root@localhost apache]# docker ps -a  CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                      PORTS                   NAMES  b7ec122849c6        httpd:new             "/run.sh"                3 seconds ago       Up 3 seconds                0.0.0.0:32768->80/tcp   test  

      這樣我們進(jìn)入容器中檢查一下這個(gè)頁面文件是否存在

      [root@localhost apache]# docker exec -it test /bin/bash  [root@b7ec122849c6 /]# cat /var/www/html/index.html   <h1>this is docker httpd web</h1>  

      那么此時(shí)我們可以通過瀏覽器訪問宿主機(jī)ip地址結(jié)合端口號(hào)(32768)來訪問網(wǎng)站,結(jié)果如下:

      Dockerfile與Dockerfile實(shí)戰(zhàn)

      下面的案例構(gòu)建就直接給出Dockerfile和構(gòu)建測試命令了。主要介紹其中的關(guān)鍵點(diǎn)。

      2、構(gòu)建sshd鏡像

      mkdir sshd     cd sshd
      #sshd服務(wù)的鏡像構(gòu)建——基于Dockerfile  #首先先下載基礎(chǔ)鏡像centos,創(chuàng)建對(duì)應(yīng)的工作目錄  #開始編寫nginx的Dockerfile  #第一步:基礎(chǔ)鏡像  FROM centos:7  #第二步:維護(hù)者信息  MAINTAINER lokott@123.com  #第三步:指令集  RUN yum -y update  RUN yum -y install openssh* net-tools lsof telnet passwd   RUN echo '123123' | passwd --stdin root  #不以PAM認(rèn)證登錄而是以密鑰對(duì)登錄(非對(duì)稱密鑰),即禁用ssh的PAM認(rèn)證  RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config  RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key  #禁用ssh中PAM會(huì)話模塊  RUN sed -i '/^sessions+requireds+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd  #創(chuàng)建ssh工作目錄和權(quán)限設(shè)置  RUN mkdir -p /root/.ssh && chown root:root /root && chmod 700 /root/.ssh  #開放22端口  EXPOSE 22  #第四步:啟動(dòng)容器時(shí)執(zhí)行指令  CMD ["/usr/sbin/sshd","-D"]  

      構(gòu)建鏡像和運(yùn)行容器

      [root@localhost sshd]# docker build -t sshd:new .  [root@localhost sshd]# docker run -d -P sshd:new   c7991648efebd192eb29f1d4e3503e47e0581f55381ff7a23e545041ef5d3e67  [root@localhost sshd]# docker ps -a  CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES  c7991648efeb        sshd:new            "/usr/sbin/sshd -D"   20 seconds ago      Up 20 seconds       0.0.0.0:32769->22/tcp   jolly_ishizaka  b7ec122849c6        httpd:new           "/run.sh"             20 minutes ago      Up 20 minutes       0.0.0.0:32768->80/tcp   test  

      測試

      [root@localhost sshd]# ssh 20.0.0.149 -p 32769  The authenticity of host '[20.0.0.149]:32769 ([20.0.0.149]:32769)' can't be established.  RSA key fingerprint is SHA256:XLezVGFvOKIKW3fTBD0sIE9rsdz4021taphmcCo8IJM.  RSA key fingerprint is MD5:1e:86:94:2a:f5:a3:6c:e2:b4:b1:e4:50:9c:ad:8e:fb.  Are you sure you want to continue connecting (yes/no)? yes  Warning: Permanently added '[20.0.0.149]:32769' (RSA) to the list of known hosts.  root@20.0.0.149's password:   [root@c7991648efeb ~]# exit  logout  Connection to 20.0.0.149 closed.  

      此時(shí)我們登錄該容器(ssh或者docker exec命令)查看sshd服務(wù)的狀態(tài)(但是systemctl無法使用)

      [root@c7991648efeb ~]# systemctl status sshd  Failed to get D-Bus connection: Operation not permitted  

      一則我們可以使用下面的命令使用該命令,二則我們可以基于上面構(gòu)建的鏡像作為基礎(chǔ)鏡像構(gòu)建systemctl的鏡像來測試驗(yàn)證。

      [root@localhost sshd]# docker run --privileged -itd -P sshd:new  /usr/sbin/init   8dafa05dc12fc02f91dce93c6ab3085ab55eff1ee6b18c24731205e5c2ed37a9  [root@localhost sshd]# docker ps -a  CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES  8dafa05dc12f        sshd:new            "/usr/sbin/init"      3 seconds ago       Up 3 seconds        0.0.0.0:32770->22/tcp   hardcore_mccarthy  c7991648efeb        sshd:new            "/usr/sbin/sshd -D"   20 minutes ago      Up 20 minutes       0.0.0.0:32769->22/tcp   jolly_ishizaka  b7ec122849c6        httpd:new           "/run.sh"             40 minutes ago      Up 40 minutes       0.0.0.0:32768->80/tcp   test  [root@localhost sshd]# ssh 20.0.0.149 -p 32770  The authenticity of host '[20.0.0.149]:32770 ([20.0.0.149]:32770)' can't be established.  ECDSA key fingerprint is SHA256:LU81jNjOCKaiWrCsxTLPmx+YsUMVOBa2rG/XLXQsv9E.  ECDSA key fingerprint is MD5:03:15:aa:8a:65:8a:cc:b4:fb:66:f8:f6:6c:89:84:7b.  Are you sure you want to continue connecting (yes/no)? yes  Warning: Permanently added '[20.0.0.149]:32770' (ECDSA) to the list of known hosts.  root@20.0.0.149's password:   [root@8dafa05dc12f ~]# systemctl status sshd  ● sshd.service - OpenSSH server daemon     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)     Active: active (running) since Wed 2020-04-22 02:28:49 UTC; 33s ago       Docs: man:sshd(8)             man:sshd_config(5)  

      –privileged表示提權(quán),使得容器真正具備root的權(quán)限

      3、構(gòu)建systemctl鏡像

      mkdir systemctl  cd systemctl

      創(chuàng)建Dockerfile

      vim Dockerfile

      FROM sshd:new  MAINTAINER lokott@123.com  ENV container docker  #下面的命令是放在一個(gè)鏡像層中執(zhí)行的,可以減少鏡像層  #括號(hào)中的指令含義是遍歷進(jìn)入的目錄文件,刪除除了systemd-tmpfiles-setup.service的所有文件,之后刪除一些其他文件  RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done);   rm -f /lib/systemd/system/multi-user.target.wants/*;   rm -f /etc/systemd/system/*.wants/*;   rm -f /lib/systemd/system/local-fs.target.wants/*;   rm -f /lib/systemd/system/sockets.target.wants/*udev*;   rm -f /lib/systemd/system/sockets.target.wants/*initctl*;   rm -f /lib/systemd/system/basic.target.wants/*;   rm -f /lib/systemd/system/anaconda.target.wants/*;  VOLUME [ "/sys/fs/cgroup" ]  CMD ["/usr/sbin/init"]  

      構(gòu)建運(yùn)行及測試

      [root@localhost systemctl]# docker build -t systemctl:new .  [root@localhost systemctl]# docker run --privileged -it -v /sys/fs/cgroup/:/sys/fs/cgroup:ro systemctl:new /usr/sbin/init  systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN)  Detected virtualization docker.  Detected architecture x86-64.    Welcome to CentOS Linux 7 (Core)!    Set hostname to <e99fd581042a>.  [  OK  ] Reached target Paths.  [  OK  ] Reached target Local File Systems.  [  OK  ] Reached target Swap.  [  OK  ] Created slice Root Slice.  [  OK  ] Listening on Journal Socket.  [  OK  ] Created slice System Slice.           Starting Create Volatile Files and Directories...  [  OK  ] Listening on Delayed Shutdown Socket.  [  OK  ] Reached target Slices.           Starting Journal Service...  [  OK  ] Started Create Volatile Files and Directories.  [ INFO ] Update UTMP about System Boot/Shutdown is not active.  [DEPEND] Dependency failed for Update UTMP about System Runlevel Changes.  Job systemd-update-utmp-runlevel.service/start failed with result 'dependency'.  [  OK  ] Started Journal Service.  [  OK  ] Reached target System Initialization.  [  OK  ] Started Daily Cleanup of Temporary Directories.  [  OK  ] Reached target Timers.  [  OK  ] Listening on D-Bus System Message Bus Socket.  [  OK  ] Reached target Sockets.  [  OK  ] Reached target Basic System.  [  OK  ] Reached target Multi-User System.  

      重新開啟一個(gè)終端進(jìn)行測試

      [root@localhost systemctl]# docker ps -a  CONTAINER ID        IMAGE               COMMAND               CREATED              STATUS              PORTS                   NAMES  e99fd581042a        systemctl:new       "/usr/sbin/init"      About a minute ago   Up About a minute   22/tcp                  gifted_edison  8dafa05dc12f        sshd:new            "/usr/sbin/init"      6 minutes ago        Up 6 minutes        0.0.0.0:32770->22/tcp   hardcore_mccarthy  c7991648efeb        sshd:new            "/usr/sbin/sshd -D"   27 minutes ago       Up 27 minutes       0.0.0.0:32769->22/tcp   jolly_ishizaka  b7ec122849c6        httpd:new           "/run.sh"             46 minutes ago       Up 46 minutes       0.0.0.0:32768->80/tcp   test  [root@localhost systemctl]# docker exec -it gifted_edison /bin/bash  [root@e99fd581042a /]# systemctl status sshd  ● sshd.service - OpenSSH server daemon     Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)     Active: inactive (dead)       Docs: man:sshd(8)             man:sshd_config(5)  [root@e99fd581042a /]# systemctl start sshd  [root@e99fd581042a /]# systemctl status sshd  ● sshd.service - OpenSSH server daemon     Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)     Active: active (running) since Wed 2020-04-22 02:36:18 UTC; 1s ago       Docs: man:sshd(8)             man:sshd_config(5)   Main PID: 51 (sshd)     CGroup: /docker/e99fd581042af009c4a15e9ab7bdd231c0052056051a1b18e9996f57eb7f2c6b/system.slice/sshd.service             └─51 /usr/sbin/sshd -D    Apr 22 02:36:18 e99fd581042a systemd[1]: Starting OpenSSH server daemon...  Apr 22 02:36:18 e99fd581042a sshd[51]: WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several problems.  Apr 22 02:36:18 e99fd581042a sshd[51]: Server listening on 0.0.0.0 port 22.  Apr 22 02:36:18 e99fd581042a sshd[51]: Server listening on :: port 22.  Apr 22 02:36:18 e99fd581042a systemd[1]: Started OpenSSH server daemon.  #開啟sshd服務(wù)后進(jìn)行訪問宿主機(jī)測試成功  [root@e99fd581042a /]# ssh 20.0.0.149 -p 22  The authenticity of host '20.0.0.149 (20.0.0.149)' can't be established.  ECDSA key fingerprint is SHA256:pT/8N0H/tNaMm4Zqh7u28Jm5EtmDkidSaih4lWzFIQY.  ECDSA key fingerprint is MD5:16:47:40:1f:40:1b:34:e9:ff:f9:15:7b:0b:f4:02:8b.  Are you sure you want to continue connecting (yes/no)? yes  Warning: Permanently added '20.0.0.149' (ECDSA) to the list of known hosts.  root@20.0.0.149's password:   Last login: Wed Apr 22 10:35:20 2020 from 20.0.0.149  [root@localhost ~]# exit  登出  Connection to 20.0.0.149 closed.  [root@e99fd581042a /]# exit  exit  

      總結(jié)

      ? 本文回顧了Dockerfile,結(jié)合三個(gè)案例(httpd服務(wù)、sshd服務(wù)、systemd服務(wù))來深入理解Dockerfile構(gòu)建鏡像的過程。其中我們需要理解的是

      1、每個(gè)服務(wù)都需要有自己的目錄和文件

      2、Dockerfile的分層和中間緩存鏡像和容器的創(chuàng)建和刪除特點(diǎn)

      3、–privileged的作用

      4、體會(huì)整個(gè)從構(gòu)建鏡像、創(chuàng)建運(yùn)行容器到測試驗(yàn)證的過程

      謝謝閱讀!

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)