第七講 Docker案例實戰(一)

一. BusyBox工具箱

        Linux系統的瑞士軍刀,集成了100多個常用的軟件工具箱,但大小卻只有幾兆,十分精巧。首先,下載 BusyBox 鏡像:

[root@localhost ~]# docker pull 192.168.255.128:5000/busybox
Using default tag: latest
latest: Pulling from busybox
e2334dd9fee4: Pull complete 
Digest: sha256:a2490cec4484ee6c1068ba3a05f89934010c85242f736280b35343483b2264b6
Status: Downloaded newer image for 192.168.255.128:5000/busybox:latest

[root@localhost ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
192.168.255.128:5000/busybox   latest              be5888e67be6        7 days ago          1.22MB

        此處小編還是從自己配的私服上下載的,當然也可以使用阿里雲鏡像加速器或者DaoCloud鏡像市場下載。下載完成後,啓動 busybox 容器,進入 busybox 容器終端 /bin/ash,可以發現 busybox 集成了很多 linux 命令。

[root@localhost ~]# docker run -itd --name busybox 192.168.255.128:5000/busybox
f8a44c4c3b31a545bd5a9e66a5209a13d74ff147cf017d4bafc0ada742ba842f
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                          COMMAND             CREATED             STATUS              PORTS               NAMES
f8a44c4c3b31        192.168.255.128:5000/busybox   "sh"                5 seconds ago       Up 4 seconds                            busybox
[root@localhost ~]# docker exec -it busybox /bin/ash
/ # pwd
/
/ # ls | grep a
var
/ # ifconfig 
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
...
/ # mount --help
BusyBox v1.31.1 (2020-04-14 01:09:51 UTC) multi-call binary.
Usage: mount [OPTIONS] [-o OPT] DEVICE NODE
Mount a filesystem. Filesystem autodetection requires /proc.
	-a		Mount all filesystems in fstab
	-f		Dry run
	...
	ro		Same as -r
There are filesystem-specific -o flags.
/ # exit
[root@localhost ~]# 

二. Tomcat 應用服務器

        關於 tomcat 鏡像的下載此處不再贅述,注意啓動Tomcat容器時,進行端口的映射,並掛載宿主機上的目錄到容器中tomcat的 webapps 目錄。

[root@localhost ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
192.168.255.128:5000/tomcat    latest              31a47677561a        5 days ago          529MB
192.168.255.128:5000/busybox   latest              be5888e67be6        7 days ago          1.22MB
[root@localhost ~]# docker run -itd --name tomcat -p 8080:8080 -v /opt/docker/tomcat/webapps:/usr/local/tomcat/webapps 192.168.255.128:5000/tomcat
697193985a92166ec45c6fff6dbf141f866e41e80c05410c8bb3ae98bd0c5579
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                          COMMAND             CREATED             STATUS              PORTS                    NAMES
697193985a92        192.168.255.128:5000/tomcat    "catalina.sh run"   4 seconds ago       Up 3 seconds        0.0.0.0:8080->8080/tcp   tomcat
f8a44c4c3b31        192.168.255.128:5000/busybox   "sh"                9 minutes ago       Up 9 minutes                                 busybox
[root@localhost ~]# cd /opt/docker/tomcat/webapps/
[root@localhost webapps]# mkdir abc
[root@localhost webapps]# cd abc
[root@localhost abc]# touch index.html
[root@localhost abc]# vim index.html
[root@localhost abc]# cat index.html 
<h1>Hello, Docker!</P>

  注:-p 8080:8080 -> 將主機的8080端口映射到容器的8080端口;-v /opt/docker/tomcat/webapps:/usr/local/tomcat/webapps -> 掛載主機上的目錄到webapps。 

在瀏覽器地址欄中輸入 http://192.168.255.128:8080/abc/index.html 訪問結果如下:

三. Mysql 數據庫服務器

        首先創建一個臨時的 mysql 鏡像,以便在掛載數據卷之前確認好相關的文件路徑,必要時還可以將相關文件拷貝下來,以便在宿主機上使用:

[root@localhost ~]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
redis                latest              df5748206578        2 days ago          98.3MB
mongo                latest              4e9495ea1bc6        2 days ago          388MB
mysql                latest              9b51d9275906        7 weeks ago         547MB
registry             latest              708bc6af7e5e        3 months ago        25.7MB
scrapinghub/splash   latest              241c7dde86d9        14 months ago       1.22GB
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@localhost ~]# docker run -itd --rm -e MYSQL_ROOT_PASSWORD=root123 mysql
f4a6c895c3dc61076a3530184b16b741a3a362605c775f7d8a57e7336189843b
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
f4a6c895c3dc        mysql               "docker-entrypoint..."   5 seconds ago       Up 4 seconds        3306/tcp, 33060/tcp   condescending_leavitt
[root@localhost ~]# docker exec -it f4 /bin/bash
root@f4a6c895c3dc:/# cd /var/lib/mysql/
root@f4a6c895c3dc:/var/lib/mysql# ls
'#innodb_temp'	 binlog.000002	 ca.pem		   f4a6c895c3dc.err   ib_logfile1   mysql		 private_key.pem   server-key.pem   undo_002
 auto.cnf	 binlog.index	 client-cert.pem   ib_buffer_pool     ibdata1	    mysql.ibd		 public_key.pem    sys
 binlog.000001	 ca-key.pem	 client-key.pem    ib_logfile0	      ibtmp1	    performance_schema	 server-cert.pem   undo_001
root@f4a6c895c3dc:/var/lib/mysql# cd /etc/mysql/conf.d/
root@f4a6c895c3dc:/etc/mysql/conf.d# ls
docker.cnf  mysql.cnf
root@f4a6c895c3dc:/etc/mysql/conf.d# exit
exit

[root@localhost ~]# docker cp f4a6c895c3dc:/etc/mysql/conf.d/mysql.cnf /opt/docker/mysql/mysql.cnf
[root@localhost docker]# ls mysql/
mysql.cnf

  注:docker run 添加 --rm 參數後,可以創建並運行一個臨時的容器,當容器停止後,會自動將容器刪除。

        下面,我們修改宿主機上的 /opt/docker/mysql/mysql.cnf 配置文件,修改 mysql 默認的字符集爲utf,在修改之前,我們不妨先查看下 mysql 的默認字符集:

[root@localhost docker]# docker exec -it mysql /bin/bash
root@a2991f6f5ea2:/# mysql -uroot -proot123
...
Server version: 8.0.19 MySQL Community Server - GPL
...
mysql> show variables like 'char%'
    -> ;
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | latin1                         |
| character_set_connection | latin1                         |
| character_set_database   | utf8mb4                        |
| character_set_filesystem | binary                         |
| character_set_results    | latin1                         |
| character_set_server     | utf8mb4                        |
| character_set_system     | utf8                           |
| character_sets_dir       | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.00 sec)

  備註:要在 Mysql 中保存 4 字節長度的 UTF-8 字符,需要使用 utf8mb4 字符集,但只有 5.5.3 版本以後的才支持。低版本的MySQL支持的utf8編碼,最大字符長度爲 3 字節,如果遇到 4 字節的字符就會出現錯誤了。三個字節的 UTF-8 最大能編碼的 Unicode 字符是 0xFFFF,也就是 Unicode 中的基本多文平面(BMP)。任何不在基本多文平面的 Unicode字符,都無法使用MySQL原有的 utf8 字符集存儲。這些不在BMP中的字符包括哪些呢?最常見的就是Emoji 表情(Emoji 是一種特殊的 Unicode 編碼,常見於 ios 和 android 手機上),和一些不常用的漢字,以及任何新增的 Unicode 字符等等。如果實際用途上來看,可以給要用到emoji的庫或者說表,設置utf8mb4。比如評論要支持emoji可以用到。

        打開我們拷貝到宿主機 /opt/docker/mysql/mysql.cnf 的文件,添加如下內容:

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

  接下來,讓我們停止剛剛創建的 mysql 臨時容器:

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
fd648a0e2101        mysql               "docker-entrypoint..."   13 minutes ago      Up 13 minutes       3306/tcp, 33060/tcp   stoic_almeida
[root@localhost ~]# docker stop fd
fd
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost ~]# 

 創建一個具有端口映射和掛載數據卷的正式 mysql 容器:

[root@localhost docker]# docker run -itd --name mysql -p 3306:3306 -v /opt/docker/mysql/mysql.cnf:/etc/mysql/conf.d/mysql.cnf -v /opt/docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root123 mysql
a2991f6f5ea2fd0a01b286ae7e99301132dce9430ff63a23f9faa48704ce9f24
[root@localhost docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
a2991f6f5ea2        mysql               "docker-entrypoint..."   5 seconds ago       Up 3 seconds        0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

[root@localhost docker]# docker exec -it mysql /bin/bash
root@a2991f6f5ea2:/# mysql -uroot -proot123
...
Server version: 8.0.19 MySQL Community Server - GPL
...
mysql> show variables like 'char%'
    -> ;
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | utf8                           |
| character_set_connection | utf8                           |
| character_set_database   | utf8                           |
| character_set_filesystem | binary                         |
| character_set_results    | utf8                           |
| character_set_server     | utf8                           |
| character_set_system     | utf8                           |
| character_sets_dir       | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.02 sec)

  注:啓動過程中,出現任何問題可以使用 docker logs mysql 查看 mysql 容器的輸出日誌。

我們掛載的宿主機目錄下也將產生 mysql 的數據文件:

四. 創建支持SSH服務的鏡像

        本節的最後一部分我們介紹如何爲Docker容器啓用ssh服務:首先使用傳統的 docker commit 方式爲Docker容器安裝ssh服務,然後將此修改提交爲新的鏡像;接着我們會討論使用Dockerfile創建上述的鏡像,以此來拋磚引玉,第七講 Docker案例實戰(二)將詳細介紹 Dockerfile 的使用以及注意事項。

4.1 基於 docker commit 方式

首先下載 centos 鏡像,並啓動爲 centos 容器:

[root@localhost docker]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
[root@localhost docker]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8a29a15cefae: Pull complete 
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:latest
[root@localhost docker]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
centos               latest              470671670cac        3 months ago        237MB
[root@localhost docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost docker]# docker run -itd --name centos centos
4dc83818fba06e41b17ab6f58c15734477a4e7592708d9677791e0afc5ed3866
[root@localhost docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4dc83818fba0        centos              "/bin/bash"         4 seconds ago       Up 2 seconds                            centos

進入 centos 容器,下載、安裝、配置並啓動 ssh 服務:

[root@localhost docker]# docker exec -it centos /bin/bash
[root@4dc83818fba0 /]# yum install -y openssh-server sudo
Failed to set locale, defaulting to C.UTF-8
... 略
Installed:
  openssh-server-8.0p1-4.el8_1.x86_64      sudo-1.8.25p1-8.el8_1.1.x86_64       
  fipscheck-1.5.0-4.el8.x86_64             fipscheck-lib-1.5.0-4.el8.x86_64     
  openssh-8.0p1-4.el8_1.x86_64            

Complete!

[root@4dc83818fba0 /]# sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config 
[root@4dc83818fba0 /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
Generating public/private dsa key pair.
... 略
The key's randomart image is:
+---[DSA 1024]----+
|             . *E|
|            . #.o|
|            .+.@.|
|         . . o=.B|
|        S .  +.O*|
|         o  . + %|
|        + .    =+|
|       + . .  ..=|
|        o..    +X|
+----[SHA256]-----+
[root@4dc83818fba0 /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
Generating public/private rsa key pair.
... 略
The key's randomart image is:
+---[RSA 3072]----+
|oo .o.+.oo.o   ..|
|..+o.+.= E=..... |
|. o++o* o. .+ o. |
|  o+oo + o + o.  |
| .  .   S o +  .o|
|         . o o..o|
|            o .o |
|             oo. |
|              oo |
+----[SHA256]-----+

[root@4dc83818fba0 /]# mkdir /var/run/sshd
[root@4dc83818fba0 /]# /usr/sbin/sshd -D

 在宿主機上查看 centos 容器的IP地址,並使用 ssh 命令登錄容器測試:

[root@localhost ~]# docker inspect centos | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
[root@localhost ~]# ssh [email protected]
[email protected]'s password:  # 輸入我們設置的root用戶密碼12345678
Last login: Fri Apr 24 05:46:42 2020 from 172.17.0.1
[root@4dc83818fba0 ~]#

提交鏡像:

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4dc83818fba0        centos              "/bin/bash"         About an hour ago   Up About an hour                        centos
[root@localhost ~]# docker commit -m "my centos with ssh" centos centos-ssh:1.0
sha256:4f27624e33f2fe6d85b18564d29937333283dd5821cb57d63d336d6aa54e0abe
[root@localhost ~]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
centos-ssh           1.0                 4f27624e33f2        4 seconds ago       274MB
centos               latest              470671670cac        3 months ago        237MB

 運行新的鏡像 centos-ssh:1.0 ,並指定端口映射及啓動容器時的命令:

[root@localhost ~]# docker run -itd --name centos-ssh -p 2222:22 centos-ssh:1.0 /usr/sbin/sshd -D
758080715cee484b8d271b72049f59597121becad5991524680bb2d0e122a130
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                  NAMES
758080715cee        centos-ssh:1.0      "/usr/sbin/sshd -D"   4 seconds ago       Up 4 seconds        0.0.0.0:2222->22/tcp   centos-ssh
4dc83818fba0        centos              "/bin/bash"           About an hour ago   Up About an hour                           centos

 在局域網的其它主機上即可使用 ssh 命令登錄到我們剛剛創建的容器:

[root@localhost ~]# ssh -p2222 [email protected]
The authenticity of host '[192.168.255.128]:2222 ([192.168.255.128]:2222)' can't be established.
RSA key fingerprint is SHA256:Q6AN5pejJINAc8taZ1F1Wgv9+H7gz8RlQ5QaOstgSNw.
RSA key fingerprint is MD5:48:e0:fa:bb:9f:98:8e:be:3f:36:b3:55:37:00:a6:20.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.255.128]:2222' (RSA) to the list of known hosts.
[email protected]'s password: 
Last login: Fri Apr 24 05:58:56 2020 from 172.17.0.1
[root@758080715cee ~]# 

總結:基於 docker commit 命令創建鏡像只適合於創建臨時的鏡像,一旦軟件需要更新或者要修改容器的其它內容,我們都必須進入容器再進行修改,不利於擴展。Dockerfile 則是一種更加推薦的方式,可以將我們創建鏡像的命令記錄下來,當需要修改鏡像時,只需要更新該腳本文件即可,非常簡潔。下面就讓我們領會下Dockerfile的強大吧! 

4.2 基於 Dockerfile 方式

        首先創建一個空目錄,在目錄下新建文件 Dockerfile ,文件內容如下:

[root@localhost dockerfile]# ls
Dockerfile
[root@localhost dockerfile]# cat Dockerfile 
FROM centos:latest
LABEL maintainer="miali [email protected]" description="centos with sshd"

RUN yum install -y openssh-server sudo
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN echo "root:12345678"|chpasswd
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN mkdir /var/run/sshd

EXPOSE 22

CMD ["/usr/sbin/sshd","-D"]
[root@localhost dockerfile]#

上述的 Dockerfile 中記錄了我們在4.1中的操作,因此 Dockerfile 創建鏡像的方式具有很好的擴展性。上述 Dockerfile 中用到的指令總結如下:

指令 出現位置 描述
FROM

FROM centos:latest  # 基於centos最新版本構建

指定一個鏡像作爲構建自定義鏡像的基礎鏡像,在這個基礎鏡像之上進行修改定製
LABEL LABEL maintainer="miali [email protected]# 維護者信息description="centos with sshd" # 鏡像描述 用於指定一個鏡像的描述信息,如維護者、描述等
RUN RUN yum install -y openssh-server sudo # sshd的安裝和配置過程
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN echo "root:12345678"|chpasswd
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN mkdir /var/run/sshd
在容器內執行 shell 命令,默認會是用 /bin/sh -c 的方式執行。每一個 RUN 的行爲,就和4.1中我們手工建立鏡像的過程一樣:新建立一層,在其上執行這些命令,執行結束後,commit 這一層的修改,構成新的鏡像。因此,寫 Dockerfile 的時候,要經常提醒自己,這並不是在寫 Shell 腳本,而是在定義每一層該如何構建
EXPOSE

EXPOSE 22 # 容器向外部暴露的端口爲22

指定容器向外部暴露的端口
CMD CMD ["/usr/sbin/sshd","-D"] # 啓動 sshd 服務 指定容器啓動時默認執行的指令,並且啓動容器時用戶指定的CMD優先;Dockerfile 中只能有一條CMD指令。如果列出多個,CMD 則只有最後一個CMD會生效

這裏只拋磚引玉介紹了Dockerfile最常用的幾個指令,下一講中,我們將詳細介紹Dockerfile的更多細節。注意RUN和CMD的區別:不要混淆 RUN和 CMD。RUN 實際上運行一個命令並提交結果; CMD 在構建時不執行任何操作,但指定鏡像的默認命令。

        Dockerfile 文件編輯完成後,即可使用 dcoker build 命令來創建鏡像了:

[root@localhost dockerfile]# ls
Dockerfile
[root@localhost dockerfile]# docker build -t centos-ssh:2.0 .
Sending build context to Docker daemon  2.048kB
Step 1/10 : FROM centos:latest
 ---> 470671670cac
Step 2/10 : LABEL maintainer "miali [email protected]" description "centos with sshd"
 ---> Running in 85876864991a
 ---> fd93b5681921
Removing intermediate container 85876864991a
Step 3/10 : RUN yum install -y openssh-server sudo
 ---> Running in 4e24b0f735ba
CentOS-8 - AppStream                            2.1 MB/s | 5.7 MB     00:02    
CentOS-8 - Base                                 267 kB/s | 2.2 MB     00:08    
CentOS-8 - Extras                               7.8 kB/s | 5.5 kB     00:00    
Dependencies resolved.
... 略
Installed:
  openssh-server-8.0p1-4.el8_1.x86_64      sudo-1.8.25p1-8.el8_1.1.x86_64       
  fipscheck-1.5.0-4.el8.x86_64             fipscheck-lib-1.5.0-4.el8.x86_64     
  openssh-8.0p1-4.el8_1.x86_64            

Complete!
 ---> 354e1b84c6a1
Removing intermediate container 4e24b0f735ba
Step 4/10 : RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
 ---> Running in 6e2bf574016c
 ---> 84436db4d051
Removing intermediate container 6e2bf574016c
Step 5/10 : RUN echo "root:12345678"|chpasswd
 ---> Running in aa22909bac6d
 ---> 16a97d3c55f0
Removing intermediate container aa22909bac6d
Step 6/10 : RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
 ---> Running in e4a787153c89
Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private dsa key pair.
... 略
The key's randomart image is:
+---[DSA 1024]----+
|  o=.o.          |
|  +.=o..         |
|.  Eo..          |
|ooB.=.   .       |
|*oB*  . S        |
|o%.+.  .         |
|*=*o             |
|+O+              |
|o.+o.            |
+----[SHA256]-----+
 ---> 0735d70f817e
Removing intermediate container e4a787153c89
Step 7/10 : RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
 ---> Running in 7b101987969c
Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private rsa key pair.
... 略
The key's randomart image is:
+---[RSA 3072]----+
|        ..    ..=|
|       o.     .X=|
|      . o.   +O=B|
|       ...  . =**|
|        S. . o o+|
|        o o ..ooE|
|       = . . .*o+|
|      o .    .o=+|
|             .o=o|
+----[SHA256]-----+
 ---> ea08f06c0e6c
Removing intermediate container 7b101987969c
Step 8/10 : RUN mkdir /var/run/sshd
 ---> Running in 983401b4c171
 ---> 5375b063e253
Removing intermediate container 983401b4c171
Step 9/10 : EXPOSE 22
 ---> Running in dd837d3c9d17
 ---> d699d8eee6a3
Removing intermediate container dd837d3c9d17
Step 10/10 : CMD /usr/sbin/sshd -D
 ---> Running in d43d558687b7
 ---> f086b91faede
Removing intermediate container d43d558687b7
Successfully built f086b91faede
Successfully tagged centos-ssh:2.0
[root@localhost dockerfile]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
centos-ssh           2.0                 f086b91faede        12 seconds ago      274MB
[root@localhost dockerfile]# docker run -itd --name centos-ssh2.0 -p 2223:22 centos-ssh:2.0
5ccd02e765a69e260f34d1b95c59393e1b1f0f3e2f416c61c40241077c9a5b41
[root@localhost dockerfile]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                  NAMES
5ccd02e765a6        centos-ssh:2.0      "/usr/sbin/sshd -D"   9 seconds ago       Up 7 seconds        0.0.0.0:2223->22/tcp   centos-ssh2.0

        仔細觀察上面 Dockerfile 的執行過程,你會發現,每一個RUN命令都會創建一個容器,進行修改,然後提交一個新的鏡像,並刪除當前容器;然後下一個RUN命令運行剛剛創建的鏡像,依次進行修改、提交、刪除容器;依次類推,直到所有的RUN指令執行完畢。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章