【Docker】镜像制作和管理

一、Docker镜像说明

二、基于容器通过 docker commit 手动制作镜像

1、基于容器手动制作镜像步骤

1、下载官方系统镜像

2、基于官方基础镜像启动容器,并进入容器

3、在容器中进行配置操作

  3.1、安装基础工具

  3.2、配置运行环境

  3.3、安装并配置服务

  3.4、存放业务程序代码

4、docker commit 提交生成新镜像

5、基于定制镜像生成容器并测试

2、基于centos7官方镜像,手动制作yum安装nginx定制镜像

2.1、下载centos7官方镜像,并启动进入容器

centos官方镜像:https://hub.docker.com/_/centos/tags

[root@Docker-Ubu1804-p11:~]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete 
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@Docker-Ubu1804-p11:~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              c059bfaa849c        17 months ago       5.59MB
hello-world         latest              feb5d9fea6a5        19 months ago       13.3kB
centos              7                   eeb6ee3f44bd        19 months ago       204MB
[root@Docker-Ubu1804-p11:~]# docker run -d --name centos-nginx centos:7 tail -f /etc/hosts
502a7a7faae8b7a3c3fa177baec1009d1bc9caabb8b782f887767b35e16ecb8a
[root@Docker-Ubu1804-p11:~]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
502a7a7faae8        centos:7            "tail -f /etc/hosts"   8 seconds ago       Up 7 seconds                            centos-nginx
[root@Docker-Ubu1804-p11:~]# docker exec -it centos-nginx bash
[root@502a7a7faae8 /]# 

 

2.2、在容器内安装基础工具,并配置环境

 

##配置容器内 yum
[root@502a7a7faae8 /]# rm -rf /etc/yum.repos.d/*
[root@502a7a7faae8 /]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2523  100  2523    0     0  28084      0 --:--:-- --:--:-- --:--:-- 28348
[root@502a7a7faae8 /]# ls /etc/yum.repos.d/
CentOS-Base.repo
[root@502a7a7faae8 /]# yum clean all
Loaded plugins: fastestmirror, ovl
Cleaning repos: base extras updates
[root@502a7a7faae8 /]# yum makecache fast
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                     | 3.6 kB  00:00:00     
extras                                                                                                                   | 2.9 kB  00:00:00     
updates                                                                                                                  | 2.9 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                                                            | 153 kB  00:00:00     
(2/4): extras/7/x86_64/primary_db                                                                                        | 249 kB  00:00:00     
(3/4): updates/7/x86_64/primary_db                                                                                       |  20 MB  00:00:08     
base/7/x86_64/primary_db       FAILED                                          
http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/6d0c3a488c282fe537794b5946b01e28c7f44db79097bb06826e1c0c88bad5ef-primary.sqlite.bz2: [Errno 14] curl#7 - "Failed connect to mirrors.aliyuncs.com:80; Connection refused"
Trying other mirror.
(4/4): base/7/x86_64/primary_db                                                                                          | 6.1 MB  00:00:01     
Metadata Cache Created


##yum安装常用基础工具
[root@502a7a7faae8 /]# yum install -y psmisc tree wget vim net-tools bash-completion
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package bash-completion.noarch 1:2.1-8.el7 will be installed
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
---> Package psmisc.x86_64 0:22.20-17.el7 will be installed
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
---> Package vim-enhanced.x86_64 2:7.4.629-8.el7_9 will be installed
--> Processing Dependency: vim-common = 2:7.4.629-8.el7_9 for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: which for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: perl(:MODULE_COMPAT_5.16.3) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: libperl.so()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: libgpm.so.2()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
--> Running transaction check
---> Package gpm-libs.x86_64 0:1.20.7-6.el7 will be installed
---> Package perl.x86_64 4:5.16.3-299.el7_9 will be installed
--> Processing Dependency: perl(Socket) >= 1.3 for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Scalar::Util) >= 1.10 for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl-macros for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(threads::shared) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(threads) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(constant) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Time::Local) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Time::HiRes) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Storable) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Socket) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Scalar::Util) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Pod::Simple::XHTML) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Pod::Simple::Search) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Getopt::Long) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Filter::Util::Call) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Temp) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Spec::Unix) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Spec::Functions) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Spec) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Path) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Exporter) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Cwd) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Carp) for package: 4:perl-5.16.3-299.el7_9.x86_64
---> Package perl-libs.x86_64 4:5.16.3-299.el7_9 will be installed
---> Package vim-common.x86_64 2:7.4.629-8.el7_9 will be installed
--> Processing Dependency: vim-filesystem for package: 2:vim-common-7.4.629-8.el7_9.x86_64
---> Package which.x86_64 0:2.20-7.el7 will be installed
--> Running transaction check
---> Package perl-Carp.noarch 0:1.26-244.el7 will be installed
---> Package perl-Exporter.noarch 0:5.68-3.el7 will be installed
---> Package perl-File-Path.noarch 0:2.09-2.el7 will be installed
---> Package perl-File-Temp.noarch 0:0.23.01-3.el7 will be installed
---> Package perl-Filter.x86_64 0:1.49-3.el7 will be installed
---> Package perl-Getopt-Long.noarch 0:2.40-3.el7 will be installed
--> Processing Dependency: perl(Pod::Usage) >= 1.14 for package: perl-Getopt-Long-2.40-3.el7.noarch
--> Processing Dependency: perl(Text::ParseWords) for package: perl-Getopt-Long-2.40-3.el7.noarch
---> Package perl-PathTools.x86_64 0:3.40-5.el7 will be installed
---> Package perl-Pod-Simple.noarch 1:3.28-4.el7 will be installed
--> Processing Dependency: perl(Pod::Escapes) >= 1.04 for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
--> Processing Dependency: perl(Encode) for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
---> Package perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 will be installed
---> Package perl-Socket.x86_64 0:2.010-5.el7 will be installed
---> Package perl-Storable.x86_64 0:2.45-3.el7 will be installed
---> Package perl-Time-HiRes.x86_64 4:1.9725-3.el7 will be installed
---> Package perl-Time-Local.noarch 0:1.2300-2.el7 will be installed
---> Package perl-constant.noarch 0:1.27-2.el7 will be installed
---> Package perl-macros.x86_64 4:5.16.3-299.el7_9 will be installed
---> Package perl-threads.x86_64 0:1.87-4.el7 will be installed
---> Package perl-threads-shared.x86_64 0:1.43-6.el7 will be installed
---> Package vim-filesystem.x86_64 2:7.4.629-8.el7_9 will be installed
--> Running transaction check
---> Package perl-Encode.x86_64 0:2.51-7.el7 will be installed
---> Package perl-Pod-Escapes.noarch 1:1.04-299.el7_9 will be installed
---> Package perl-Pod-Usage.noarch 0:1.63-3.el7 will be installed
--> Processing Dependency: perl(Pod::Text) >= 3.15 for package: perl-Pod-Usage-1.63-3.el7.noarch
--> Processing Dependency: perl-Pod-Perldoc for package: perl-Pod-Usage-1.63-3.el7.noarch
---> Package perl-Text-ParseWords.noarch 0:3.29-4.el7 will be installed
--> Running transaction check
---> Package perl-Pod-Perldoc.noarch 0:3.20-4.el7 will be installed
--> Processing Dependency: perl(parent) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
--> Processing Dependency: perl(HTTP::Tiny) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
--> Processing Dependency: groff-base for package: perl-Pod-Perldoc-3.20-4.el7.noarch
---> Package perl-podlators.noarch 0:2.5.1-3.el7 will be installed
--> Running transaction check
---> Package groff-base.x86_64 0:1.22.2-8.el7 will be installed
---> Package perl-HTTP-Tiny.noarch 0:0.033-3.el7 will be installed
---> Package perl-parent.noarch 1:0.225-244.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================
 Package                                  Arch                     Version                                      Repository                 Size
================================================================================================================================================
Installing:
 bash-completion                          noarch                   1:2.1-8.el7                                  base                       87 k
 net-tools                                x86_64                   2.0-0.25.20131004git.el7                     base                      306 k
 psmisc                                   x86_64                   22.20-17.el7                                 base                      141 k
 tree                                     x86_64                   1.6.0-10.el7                                 base                       46 k
 vim-enhanced                             x86_64                   2:7.4.629-8.el7_9                            updates                   1.1 M
 wget                                     x86_64                   1.14-18.el7_6.1                              base                      547 k
Installing for dependencies:
 gpm-libs                                 x86_64                   1.20.7-6.el7                                 base                       32 k
 groff-base                               x86_64                   1.22.2-8.el7                                 base                      942 k
 perl                                     x86_64                   4:5.16.3-299.el7_9                           updates                   8.0 M
 perl-Carp                                noarch                   1.26-244.el7                                 base                       19 k
 perl-Encode                              x86_64                   2.51-7.el7                                   base                      1.5 M
 perl-Exporter                            noarch                   5.68-3.el7                                   base                       28 k
 perl-File-Path                           noarch                   2.09-2.el7                                   base                       26 k
 perl-File-Temp                           noarch                   0.23.01-3.el7                                base                       56 k
 perl-Filter                              x86_64                   1.49-3.el7                                   base                       76 k
 perl-Getopt-Long                         noarch                   2.40-3.el7                                   base                       56 k
 perl-HTTP-Tiny                           noarch                   0.033-3.el7                                  base                       38 k
 perl-PathTools                           x86_64                   3.40-5.el7                                   base                       82 k
 perl-Pod-Escapes                         noarch                   1:1.04-299.el7_9                             updates                    52 k
 perl-Pod-Perldoc                         noarch                   3.20-4.el7                                   base                       87 k
 perl-Pod-Simple                          noarch                   1:3.28-4.el7                                 base                      216 k
 perl-Pod-Usage                           noarch                   1.63-3.el7                                   base                       27 k
 perl-Scalar-List-Utils                   x86_64                   1.27-248.el7                                 base                       36 k
 perl-Socket                              x86_64                   2.010-5.el7                                  base                       49 k
 perl-Storable                            x86_64                   2.45-3.el7                                   base                       77 k
 perl-Text-ParseWords                     noarch                   3.29-4.el7                                   base                       14 k
 perl-Time-HiRes                          x86_64                   4:1.9725-3.el7                               base                       45 k
 perl-Time-Local                          noarch                   1.2300-2.el7                                 base                       24 k
 perl-constant                            noarch                   1.27-2.el7                                   base                       19 k
 perl-libs                                x86_64                   4:5.16.3-299.el7_9                           updates                   690 k
 perl-macros                              x86_64                   4:5.16.3-299.el7_9                           updates                    44 k
 perl-parent                              noarch                   1:0.225-244.el7                              base                       12 k
 perl-podlators                           noarch                   2.5.1-3.el7                                  base                      112 k
 perl-threads                             x86_64                   1.87-4.el7                                   base                       49 k
 perl-threads-shared                      x86_64                   1.43-6.el7                                   base                       39 k
 vim-common                               x86_64                   2:7.4.629-8.el7_9                            updates                   5.9 M
 vim-filesystem                           x86_64                   2:7.4.629-8.el7_9                            updates                    11 k
 which                                    x86_64                   2.20-7.el7                                   base                       41 k

Transaction Summary
================================================================================================================================================
Install  6 Packages (+32 Dependent packages)

Total download size: 20 M
Installed size: 67 M
Downloading packages:
groff-base-1.22.2-8.el7.x86_64 FAILED                                          
http://mirrors.cloud.aliyuncs.com/centos/7/os/x86_64/Packages/groff-base-1.22.2-8.el7.x86_64.rpm: [Errno 14] curl#6 - "Could not resolve host: mirrors.cloud.aliyuncs.com; Unknown error"
Trying other mirror.
warning: /var/cache/yum/x86_64/7/base/packages/gpm-libs-1.20.7-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY-:-- ETA 
Public key for gpm-libs-1.20.7-6.el7.x86_64.rpm is not installed
(1/38): gpm-libs-1.20.7-6.el7.x86_64.rpm                                                                                 |  32 kB  00:00:00     
(2/38): bash-completion-2.1-8.el7.noarch.rpm                                                                             |  87 kB  00:00:00     
(3/38): net-tools-2.0-0.25.20131004git.el7.x86_64.rpm                                                                    | 306 kB  00:00:00     
(4/38): perl-Carp-1.26-244.el7.noarch.rpm                                                                                |  19 kB  00:00:00     
(5/38): perl-Exporter-5.68-3.el7.noarch.rpm                                                                              |  28 kB  00:00:00     
(6/38): perl-File-Path-2.09-2.el7.noarch.rpm                                                                             |  26 kB  00:00:00     
(7/38): perl-File-Temp-0.23.01-3.el7.noarch.rpm                                                                          |  56 kB  00:00:00     
(8/38): perl-Encode-2.51-7.el7.x86_64.rpm                                                                                | 1.5 MB  00:00:00     
(9/38): perl-Filter-1.49-3.el7.x86_64.rpm                                                                                |  76 kB  00:00:00     
(10/38): perl-HTTP-Tiny-0.033-3.el7.noarch.rpm                                                                           |  38 kB  00:00:00     
(11/38): perl-Getopt-Long-2.40-3.el7.noarch.rpm                                                                          |  56 kB  00:00:00     
(12/38): perl-Pod-Perldoc-3.20-4.el7.noarch.rpm                                                                          |  87 kB  00:00:00     
(13/38): perl-Pod-Simple-3.28-4.el7.noarch.rpm                                                                           | 216 kB  00:00:00     
(14/38): perl-PathTools-3.40-5.el7.x86_64.rpm                                                                            |  82 kB  00:00:00     
(15/38): perl-Pod-Usage-1.63-3.el7.noarch.rpm                                                                            |  27 kB  00:00:00     
(16/38): perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm                                                                  |  36 kB  00:00:00     
Public key for perl-5.16.3-299.el7_9.x86_64.rpm is not installed
(17/38): perl-5.16.3-299.el7_9.x86_64.rpm                                                                                | 8.0 MB  00:00:01     
(18/38): perl-Pod-Escapes-1.04-299.el7_9.noarch.rpm                                                                      |  52 kB  00:00:00     
(19/38): perl-Socket-2.010-5.el7.x86_64.rpm                                                                              |  49 kB  00:00:00     
(20/38): perl-Storable-2.45-3.el7.x86_64.rpm                                                                             |  77 kB  00:00:00     
(21/38): perl-Time-HiRes-1.9725-3.el7.x86_64.rpm                                                                         |  45 kB  00:00:00     
(22/38): perl-Text-ParseWords-3.29-4.el7.noarch.rpm                                                                      |  14 kB  00:00:00     
(23/38): perl-Time-Local-1.2300-2.el7.noarch.rpm                                                                         |  24 kB  00:00:00     
(24/38): perl-constant-1.27-2.el7.noarch.rpm                                                                             |  19 kB  00:00:00     
(25/38): perl-podlators-2.5.1-3.el7.noarch.rpm                                                                           | 112 kB  00:00:00     
(26/38): perl-parent-0.225-244.el7.noarch.rpm                                                                            |  12 kB  00:00:00     
(27/38): perl-threads-shared-1.43-6.el7.x86_64.rpm                                                                       |  39 kB  00:00:00     
(28/38): perl-macros-5.16.3-299.el7_9.x86_64.rpm                                                                         |  44 kB  00:00:00     
(29/38): psmisc-22.20-17.el7.x86_64.rpm                                                                                  | 141 kB  00:00:00     
(30/38): perl-libs-5.16.3-299.el7_9.x86_64.rpm                                                                           | 690 kB  00:00:00     
(31/38): perl-threads-1.87-4.el7.x86_64.rpm                                                                              |  49 kB  00:00:00     
(32/38): tree-1.6.0-10.el7.x86_64.rpm                                                                                    |  46 kB  00:00:00     
(33/38): vim-enhanced-7.4.629-8.el7_9.x86_64.rpm                                                                         | 1.1 MB  00:00:00     
(34/38): vim-filesystem-7.4.629-8.el7_9.x86_64.rpm                                                                       |  11 kB  00:00:00     
(35/38): which-2.20-7.el7.x86_64.rpm                                                                                     |  41 kB  00:00:00     
(36/38): wget-1.14-18.el7_6.1.x86_64.rpm                                                                                 | 547 kB  00:00:00     
(37/38): vim-common-7.4.629-8.el7_9.x86_64.rpm                                                                           | 5.9 MB  00:00:00     
(38/38): groff-base-1.22.2-8.el7.x86_64.rpm                                                                              | 942 kB  00:00:00     
------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                           7.0 MB/s |  20 MB  00:00:02     
Retrieving key from http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <[email protected]>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 From       : http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : gpm-libs-1.20.7-6.el7.x86_64                                                                                                1/38 
  Installing : 2:vim-filesystem-7.4.629-8.el7_9.x86_64                                                                                     2/38 
  Installing : 2:vim-common-7.4.629-8.el7_9.x86_64                                                                                         3/38 
  Installing : which-2.20-7.el7.x86_64                                                                                                     4/38 
install-info: No such file or directory for /usr/share/info/which.info.gz
  Installing : groff-base-1.22.2-8.el7.x86_64                                                                                              5/38 
  Installing : 1:perl-parent-0.225-244.el7.noarch                                                                                          6/38 
  Installing : perl-HTTP-Tiny-0.033-3.el7.noarch                                                                                           7/38 
  Installing : perl-podlators-2.5.1-3.el7.noarch                                                                                           8/38 
  Installing : perl-Pod-Perldoc-3.20-4.el7.noarch                                                                                          9/38 
  Installing : 1:perl-Pod-Escapes-1.04-299.el7_9.noarch                                                                                   10/38 
  Installing : perl-Encode-2.51-7.el7.x86_64                                                                                              11/38 
  Installing : perl-Text-ParseWords-3.29-4.el7.noarch                                                                                     12/38 
  Installing : perl-Pod-Usage-1.63-3.el7.noarch                                                                                           13/38 
  Installing : 4:perl-macros-5.16.3-299.el7_9.x86_64                                                                                      14/38 
  Installing : perl-Storable-2.45-3.el7.x86_64                                                                                            15/38 
  Installing : perl-Exporter-5.68-3.el7.noarch                                                                                            16/38 
  Installing : perl-constant-1.27-2.el7.noarch                                                                                            17/38 
  Installing : perl-Socket-2.010-5.el7.x86_64                                                                                             18/38 
  Installing : perl-Time-Local-1.2300-2.el7.noarch                                                                                        19/38 
  Installing : perl-Carp-1.26-244.el7.noarch                                                                                              20/38 
  Installing : perl-PathTools-3.40-5.el7.x86_64                                                                                           21/38 
  Installing : perl-Scalar-List-Utils-1.27-248.el7.x86_64                                                                                 22/38 
  Installing : 1:perl-Pod-Simple-3.28-4.el7.noarch                                                                                        23/38 
  Installing : perl-File-Temp-0.23.01-3.el7.noarch                                                                                        24/38 
  Installing : perl-File-Path-2.09-2.el7.noarch                                                                                           25/38 
  Installing : perl-threads-shared-1.43-6.el7.x86_64                                                                                      26/38 
  Installing : perl-threads-1.87-4.el7.x86_64                                                                                             27/38 
  Installing : 4:perl-Time-HiRes-1.9725-3.el7.x86_64                                                                                      28/38 
  Installing : perl-Filter-1.49-3.el7.x86_64                                                                                              29/38 
  Installing : 4:perl-libs-5.16.3-299.el7_9.x86_64                                                                                        30/38 
  Installing : perl-Getopt-Long-2.40-3.el7.noarch                                                                                         31/38 
  Installing : 4:perl-5.16.3-299.el7_9.x86_64                                                                                             32/38 
  Installing : 2:vim-enhanced-7.4.629-8.el7_9.x86_64                                                                                      33/38 
  Installing : psmisc-22.20-17.el7.x86_64                                                                                                 34/38 
  Installing : wget-1.14-18.el7_6.1.x86_64                                                                                                35/38 
install-info: No such file or directory for /usr/share/info/wget.info.gz
  Installing : 1:bash-completion-2.1-8.el7.noarch                                                                                         36/38 
  Installing : tree-1.6.0-10.el7.x86_64                                                                                                   37/38 
  Installing : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                  38/38 
  Verifying  : perl-HTTP-Tiny-0.033-3.el7.noarch                                                                                           1/38 
  Verifying  : perl-threads-shared-1.43-6.el7.x86_64                                                                                       2/38 
  Verifying  : perl-Storable-2.45-3.el7.x86_64                                                                                             3/38 
  Verifying  : groff-base-1.22.2-8.el7.x86_64                                                                                              4/38 
  Verifying  : perl-Exporter-5.68-3.el7.noarch                                                                                             5/38 
  Verifying  : perl-constant-1.27-2.el7.noarch                                                                                             6/38 
  Verifying  : 2:vim-enhanced-7.4.629-8.el7_9.x86_64                                                                                       7/38 
  Verifying  : 4:perl-macros-5.16.3-299.el7_9.x86_64                                                                                       8/38 
  Verifying  : perl-PathTools-3.40-5.el7.x86_64                                                                                            9/38 
  Verifying  : 1:perl-parent-0.225-244.el7.noarch                                                                                         10/38 
  Verifying  : perl-Socket-2.010-5.el7.x86_64                                                                                             11/38 
  Verifying  : which-2.20-7.el7.x86_64                                                                                                    12/38 
  Verifying  : 2:vim-filesystem-7.4.629-8.el7_9.x86_64                                                                                    13/38 
  Verifying  : perl-File-Temp-0.23.01-3.el7.noarch                                                                                        14/38 
  Verifying  : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                  15/38 
  Verifying  : 1:perl-Pod-Simple-3.28-4.el7.noarch                                                                                        16/38 
  Verifying  : tree-1.6.0-10.el7.x86_64                                                                                                   17/38 
  Verifying  : perl-Time-Local-1.2300-2.el7.noarch                                                                                        18/38 
  Verifying  : 1:perl-Pod-Escapes-1.04-299.el7_9.noarch                                                                                   19/38 
  Verifying  : perl-Carp-1.26-244.el7.noarch                                                                                              20/38 
  Verifying  : 1:bash-completion-2.1-8.el7.noarch                                                                                         21/38 
  Verifying  : 2:vim-common-7.4.629-8.el7_9.x86_64                                                                                        22/38 
  Verifying  : perl-Scalar-List-Utils-1.27-248.el7.x86_64                                                                                 23/38 
  Verifying  : perl-Pod-Usage-1.63-3.el7.noarch                                                                                           24/38 
  Verifying  : perl-Encode-2.51-7.el7.x86_64                                                                                              25/38 
  Verifying  : perl-Pod-Perldoc-3.20-4.el7.noarch                                                                                         26/38 
  Verifying  : perl-podlators-2.5.1-3.el7.noarch                                                                                          27/38 
  Verifying  : 4:perl-5.16.3-299.el7_9.x86_64                                                                                             28/38 
  Verifying  : perl-File-Path-2.09-2.el7.noarch                                                                                           29/38 
  Verifying  : perl-threads-1.87-4.el7.x86_64                                                                                             30/38 
  Verifying  : 4:perl-Time-HiRes-1.9725-3.el7.x86_64                                                                                      31/38 
  Verifying  : gpm-libs-1.20.7-6.el7.x86_64                                                                                               32/38 
  Verifying  : perl-Filter-1.49-3.el7.x86_64                                                                                              33/38 
  Verifying  : perl-Getopt-Long-2.40-3.el7.noarch                                                                                         34/38 
  Verifying  : perl-Text-ParseWords-3.29-4.el7.noarch                                                                                     35/38 
  Verifying  : wget-1.14-18.el7_6.1.x86_64                                                                                                36/38 
  Verifying  : 4:perl-libs-5.16.3-299.el7_9.x86_64                                                                                        37/38 
  Verifying  : psmisc-22.20-17.el7.x86_64                                                                                                 38/38 

Installed:
  bash-completion.noarch 1:2.1-8.el7     net-tools.x86_64 0:2.0-0.25.20131004git.el7  psmisc.x86_64 0:22.20-17.el7  tree.x86_64 0:1.6.0-10.el7 
  vim-enhanced.x86_64 2:7.4.629-8.el7_9  wget.x86_64 0:1.14-18.el7_6.1               

Dependency Installed:
  gpm-libs.x86_64 0:1.20.7-6.el7                 groff-base.x86_64 0:1.22.2-8.el7                   perl.x86_64 4:5.16.3-299.el7_9             
  perl-Carp.noarch 0:1.26-244.el7                perl-Encode.x86_64 0:2.51-7.el7                    perl-Exporter.noarch 0:5.68-3.el7          
  perl-File-Path.noarch 0:2.09-2.el7             perl-File-Temp.noarch 0:0.23.01-3.el7              perl-Filter.x86_64 0:1.49-3.el7            
  perl-Getopt-Long.noarch 0:2.40-3.el7           perl-HTTP-Tiny.noarch 0:0.033-3.el7                perl-PathTools.x86_64 0:3.40-5.el7         
  perl-Pod-Escapes.noarch 1:1.04-299.el7_9       perl-Pod-Perldoc.noarch 0:3.20-4.el7               perl-Pod-Simple.noarch 1:3.28-4.el7        
  perl-Pod-Usage.noarch 0:1.63-3.el7             perl-Scalar-List-Utils.x86_64 0:1.27-248.el7       perl-Socket.x86_64 0:2.010-5.el7           
  perl-Storable.x86_64 0:2.45-3.el7              perl-Text-ParseWords.noarch 0:3.29-4.el7           perl-Time-HiRes.x86_64 4:1.9725-3.el7      
  perl-Time-Local.noarch 0:1.2300-2.el7          perl-constant.noarch 0:1.27-2.el7                  perl-libs.x86_64 4:5.16.3-299.el7_9        
  perl-macros.x86_64 4:5.16.3-299.el7_9          perl-parent.noarch 1:0.225-244.el7                 perl-podlators.noarch 0:2.5.1-3.el7        
  perl-threads.x86_64 0:1.87-4.el7               perl-threads-shared.x86_64 0:1.43-6.el7            vim-common.x86_64 2:7.4.629-8.el7_9        
  vim-filesystem.x86_64 2:7.4.629-8.el7_9        which.x86_64 0:2.20-7.el7                         

Complete!

 

 

2.3、在容器内安装配置nginx,生成业务数据,设置服务前台运行

## 安装epel源
[root@502a7a7faae8 /]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
--2023-04-24 14:23:54--  https://mirrors.aliyun.com/repo/epel-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 120.226.58.184, 120.226.58.182, 120.226.58.185, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|120.226.58.184|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 664 [application/octet-stream]
Saving to: '/etc/yum.repos.d/epel.repo'

100%[======================================================================================================>] 664         --.-K/s   in 0s      

2023-04-24 14:23:54 (124 MB/s) - '/etc/yum.repos.d/epel.repo' saved [664/664]

[root@502a7a7faae8 /]# yum makecache fast
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                     | 3.6 kB  00:00:00     
epel                                                                                                                     | 4.7 kB  00:00:00     
extras                                                                                                                   | 2.9 kB  00:00:00     
updates                                                                                                                  | 2.9 kB  00:00:00     
(1/3): epel/x86_64/group_gz                                                                                              |  99 kB  00:00:00     
(2/3): epel/x86_64/updateinfo                                                                                            | 1.0 MB  00:00:00     
(3/3): epel/x86_64/primary_db                                                                                            | 7.0 MB  00:00:01     
Metadata Cache Created

##安装nginx
[root@502a7a7faae8 /]# yum install -y nginx
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.20.1-10.el7 will be installed
--> Processing Dependency: nginx-filesystem = 1:1.20.1-10.el7 for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: openssl for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: redhat-indexhtml for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: system-logos for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Running transaction check
---> Package centos-indexhtml.noarch 0:7-9.el7.centos will be installed
---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed
---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed
---> Package nginx-filesystem.noarch 1:1.20.1-10.el7 will be installed
---> Package openssl.x86_64 1:1.0.2k-26.el7_9 will be installed
--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-26.el7_9 for package: 1:openssl-1.0.2k-26.el7_9.x86_64
--> Processing Dependency: make for package: 1:openssl-1.0.2k-26.el7_9.x86_64
---> Package openssl11-libs.x86_64 1:1.1.1k-5.el7 will be installed
--> Running transaction check
---> Package make.x86_64 1:3.82-24.el7 will be installed
---> Package openssl-libs.x86_64 1:1.0.2k-19.el7 will be updated
---> Package openssl-libs.x86_64 1:1.0.2k-26.el7_9 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================
 Package                               Arch                        Version                                   Repository                    Size
================================================================================================================================================
Installing:
 nginx                                 x86_64                      1:1.20.1-10.el7                           epel                         588 k
Installing for dependencies:
 centos-indexhtml                      noarch                      7-9.el7.centos                            base                          92 k
 centos-logos                          noarch                      70.0.6-3.el7.centos                       base                          21 M
 gperftools-libs                       x86_64                      2.6.1-1.el7                               base                         272 k
 make                                  x86_64                      1:3.82-24.el7                             base                         421 k
 nginx-filesystem                      noarch                      1:1.20.1-10.el7                           epel                          24 k
 openssl                               x86_64                      1:1.0.2k-26.el7_9                         updates                      494 k
 openssl11-libs                        x86_64                      1:1.1.1k-5.el7                            epel                         1.5 M
Updating for dependencies:
 openssl-libs                          x86_64                      1:1.0.2k-26.el7_9                         updates                      1.2 M

Transaction Summary
================================================================================================================================================
Install  1 Package  (+7 Dependent packages)
Upgrade             ( 1 Dependent package)

Total download size: 26 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/9): centos-indexhtml-7-9.el7.centos.noarch.rpm                                                                        |  92 kB  00:00:00     
(2/9): gperftools-libs-2.6.1-1.el7.x86_64.rpm                                                                            | 272 kB  00:00:00     
(3/9): make-3.82-24.el7.x86_64.rpm                                                                                       | 421 kB  00:00:00     
(4/9): openssl-libs-1.0.2k-26.el7_9.x86_64.rpm                                                                           | 1.2 MB  00:00:00     
(5/9): nginx-filesystem-1.20.1-10.el7.noarch.rpm                                                                         |  24 kB  00:00:00     
(6/9): openssl-1.0.2k-26.el7_9.x86_64.rpm                                                                                | 494 kB  00:00:00     
(7/9): nginx-1.20.1-10.el7.x86_64.rpm                                                                                    | 588 kB  00:00:00     
(8/9): openssl11-libs-1.1.1k-5.el7.x86_64.rpm                                                                            | 1.5 MB  00:00:00     
(9/9): centos-logos-70.0.6-3.el7.centos.noarch.rpm                                                                       |  21 MB  00:00:02     
------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                            12 MB/s |  26 MB  00:00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : centos-logos-70.0.6-3.el7.centos.noarch                                                                                     1/10 
  Installing : centos-indexhtml-7-9.el7.centos.noarch                                                                                      2/10 
  Installing : 1:make-3.82-24.el7.x86_64                                                                                                   3/10 
  Installing : 1:openssl11-libs-1.1.1k-5.el7.x86_64                                                                                        4/10 
  Installing : gperftools-libs-2.6.1-1.el7.x86_64                                                                                          5/10 
  Installing : 1:nginx-filesystem-1.20.1-10.el7.noarch                                                                                     6/10 
  Updating   : 1:openssl-libs-1.0.2k-26.el7_9.x86_64                                                                                       7/10 
  Installing : 1:openssl-1.0.2k-26.el7_9.x86_64                                                                                            8/10 
  Installing : 1:nginx-1.20.1-10.el7.x86_64                                                                                                9/10 
  Cleanup    : 1:openssl-libs-1.0.2k-19.el7.x86_64                                                                                        10/10 
  Verifying  : 1:openssl-libs-1.0.2k-26.el7_9.x86_64                                                                                       1/10 
  Verifying  : 1:nginx-filesystem-1.20.1-10.el7.noarch                                                                                     2/10 
  Verifying  : gperftools-libs-2.6.1-1.el7.x86_64                                                                                          3/10 
  Verifying  : 1:openssl11-libs-1.1.1k-5.el7.x86_64                                                                                        4/10 
  Verifying  : 1:nginx-1.20.1-10.el7.x86_64                                                                                                5/10 
  Verifying  : 1:make-3.82-24.el7.x86_64                                                                                                   6/10 
  Verifying  : centos-indexhtml-7-9.el7.centos.noarch                                                                                      7/10 
  Verifying  : 1:openssl-1.0.2k-26.el7_9.x86_64                                                                                            8/10 
  Verifying  : centos-logos-70.0.6-3.el7.centos.noarch                                                                                     9/10 
  Verifying  : 1:openssl-libs-1.0.2k-19.el7.x86_64                                                                                        10/10 

Installed:
  nginx.x86_64 1:1.20.1-10.el7                                                                                                                  

Dependency Installed:
  centos-indexhtml.noarch 0:7-9.el7.centos        centos-logos.noarch 0:70.0.6-3.el7.centos        gperftools-libs.x86_64 0:2.6.1-1.el7       
  make.x86_64 1:3.82-24.el7                       nginx-filesystem.noarch 1:1.20.1-10.el7          openssl.x86_64 1:1.0.2k-26.el7_9           
  openssl11-libs.x86_64 1:1.1.1k-5.el7           

Dependency Updated:
  openssl-libs.x86_64 1:1.0.2k-26.el7_9                                                                                                         

Complete!

##创建业务数据
[root@502a7a7faae8 /]# nginx -v
nginx version: nginx/1.20.1
[root@502a7a7faae8 /]# cat /etc/nginx/nginx.conf | grep root
        root         /usr/share/nginx/html;
#        root         /usr/share/nginx/html;
[root@502a7a7faae8 /]# ll /usr/share/nginx/html/index.html 
lrwxrwxrwx 1 root root 25 Apr 24 14:24 /usr/share/nginx/html/index.html -> ../../doc/HTML/index.html
[root@502a7a7faae8 /]# echo "nginix-1.20.1 on in docker" > /usr/share/doc/HTML/index.html
bash: /usr/share/doc/HTML/index.html: No such file or directory
[root@502a7a7faae8 /]# mkdir -p /usr/share/doc/HTML/
[root@502a7a7faae8 /]# echo "nginix-1.20.1 on in docker" > /usr/share/doc/HTML/index.html
[root@502a7a7faae8 /]# 


##启动nginx服务,验证服务正常启动
[root@502a7a7faae8 /]# nginx
[root@502a7a7faae8 /]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN     
[root@502a7a7faae8 /]# hostname -i
172.17.0.2
[root@502a7a7faae8 /]# curl 172.17.0.2
nginix-1.20.1 on in docker


##修改nginx服务配置daemon off;,设置服务前台运行
[root@502a7a7faae8 /]# vim /etc/nginx/nginx.conf
[root@502a7a7faae8 /]# cat /etc/nginx/nginx.conf | grep daemon
daemon off;
[root@502a7a7faae8 /]# nginx

 

2.4、提交生成镜像

## 提交生成镜像:
# 作者 janzen.com
# 描述 Base centos7 yum install nginx-1.20.1 v1.0
# 暴露端口 80 443
# 引用容器 502a7a7faae8
# 生成镜像名称 janzen/nginx-centos7
# 镜像版本 1.20.1-v1.0

[root@Docker-Ubu1804-p11:~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 502a7a7faae8 centos:7 "tail -f /etc/hosts" About an hour ago Up About an hour centos-nginx [root@Docker-Ubu1804-p11:~]# docker commit -a "janzen.com" -m "Base centos7 yum install nginx-1.20.1 v1.0" -c "EXPOSE 80 443" 502a7a7faae8 janzen/nginx-centos7:1.20.1-v1.0 sha256:fd0513c9ae5e01e5be7122c7df7a1eb81a9ebce14a4752552cdd47b3be70f821 [root@Docker-Ubu1804-p11:~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE janzen/nginx-centos7 1.20.1-v1.0 fd0513c9ae5e 12 seconds ago 559MB alpine latest c059bfaa849c 17 months ago 5.59MB hello-world latest feb5d9fea6a5 19 months ago 13.3kB centos 7 eeb6ee3f44bd 19 months ago 204MB

 

2.5、基于生成的镜像,启动容器

[root@Docker-Ubu1804-p11:~]# docker run -d -p 80:80 --name nginx_v1.0 janzen/nginx-centos7:1.20.1-v1.0 nginx
571850aae7dfdfcdd24ddff07e9e98c3da1ceed03e0c89b9bc6bb66caa184ce5
[root@Docker-Ubu1804-p11:~]# docker ps
CONTAINER ID        IMAGE                              COMMAND             CREATED             STATUS              PORTS                         NAMES
571850aae7df        janzen/nginx-centos7:1.20.1-v1.0   "nginx"             5 seconds ago       Up 4 seconds        0.0.0.0:80->80/tcp, 443/tcp   nginx_v1.0

 

2.6、测试容器访问

[root@Docker-CentOS8-12 ~]# curl 10.0.0.11
nginix-1.20.1 on in docker
[root@Docker-CentOS8-12 ~]# 

 

三、利用 Dockerfile 文件执行 docker build 自动构建镜像

1、Dockerfile 介绍

Dockerfile 文件是一种由 Docker程序解释执行的文本文件,文件内容由一条条的Dockerfile指令构成,每条指令将通过Docker程序解释为对应为相应的Linux命令,Docker程序通过这些指令执行生成Docker镜像,相比于手动创建的镜像,Dockerfile更加直观地展示每一个镜像创建的过程。有了Dockerfile 后期在原有镜像上出现新的需求时,可以直接通过修改Dockerfile内容进行调整,并生成新的镜像,避免重复手动制作镜像的过程,实现类似于shell格式脚本。

2、Dockerfile 镜像制作和使用流程

 

3、Dockerfile 文件制作镜像的分层结构

 

#根据不同的镜像层级进行目录创建,便于后期管理镜像
[root@Docker-Ubu1804-p11:~]# mkdir -p /images/{system/{centos,ubuntu,alpine},webserver/{nginx,apache,tomcat},dev/python,database/{mysql,redis,mongdb}} [root@Docker-Ubu1804-p11:~]# tree /images/ /images/ ├── database │ ├── mongdb │ ├── mysql │ └── redis ├── dev │ └── python ├── system │ ├── alpine │ ├── centos │ └── ubuntu └── webserver ├── apache ├── nginx └── tomcat 14 directories, 0 files [root@Docker-Ubu1804-p11:~]#

 

4、Dockerfile 文件格式

Dockerfile 是一种有特定语法格式的文本文件

官方文档:https://docs.docker.com/engine/reference/builder/

Dockerfile文件说明

  每一行都以Dockerfile指令开头,指令不区分大小写格式,通常采用 全大写格式

  使用 # 作为注释

  每一行仅允许一条指令,但是可以携带多个参数

  指令按照文件内容从上至下顺序执行

  每一条指令执行都将生成一个新的镜像层,为了减少层和镜像大小,尽可能多的将多条指令合并为一条执行

  每一条指令都是独立运行的,不会相互影响。例如:RUN cd /tmp 的结果不会对下一行 RUN echo > file 产生影响,file还是会被创建在工作目录下,而不是 /tmp 目录下

  每次执行Dockerfile都将按照顺序执行,从头开始,执行过的指令将会生成缓存,再下次进行制作镜像时无需执行。但是如果其中一行内容发生变化,变化行之后的全部命令缓存将会被清空,重新执行,因此通常应该尽可能将变化性大的指令置于最后执行。

5、Dockerfile 命令

#Dockerfile 常用命令
FROM
LABEL
ENV
ADD
COPY
RUN
CMD
ENTRYPOINT

Dockerfile构建过程

1、从基础镜像运行一个容器

2、执行对应的指令对容器做出修改

3、执行类似docker commit 操作,提交一个新的中间层镜像(可以利用中间层镜像创建容器进行调试与排错)

4、再基于上一步生成的中间层镜像运行一个容器

5、执行下一条指令对上一步运行的容器进行修改,直至完成全部命令

Dockerfile指令总结

 

6、构建镜像命令 docker build 和 docker history

docker build 命令语法

Usage:    docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)  
      --build-arg list          Set build-time variables
      --cache-from strings      Images to consider as cache sources
      --cgroup-parent string    Optional parent cgroup for the container
      --compress                Compress the build context using gzip
      --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int          CPU shares (relative weight)
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust   Skip image verification (default true)
  -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')    #指定要使用的Dockerfile路径,可以是文件路径也可以是URL,默认为 PATH/Dockerfile, - 表示从标准输入
      --force-rm                Always remove intermediate containers    #设置镜像过程中删除中间容器
      --iidfile string          Write the image ID to the file    
      --isolation string        Container isolation technology    #使用容器隔离技术
      --label list              Set metadata for an image
  -m, --memory bytes            Memory limit
      --memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --network string          Set the networking mode for the RUN instructions during build (default "default")
      --no-cache                Do not use cache when building the image
  -o, --output stringArray      Output destination (format: type=local,dest=path)
      --platform string         Set platform if server is multi-platform capable
      --progress string         Set type of progress output (auto, plain, tty). Use plain to show container output (default "auto")
      --pull                    Always attempt to pull a newer version of the image
  -q, --quiet                   Suppress the build output and print image ID on success
      --rm                      Remove intermediate containers after a successful build (default true)
      --secret stringArray      Secret file to expose to the build (only if BuildKit enabled): id=mysecret,src=/local/secret
      --security-opt strings    Security options
      --shm-size bytes          Size of /dev/shm
      --squash                  Squash newly built layers into a single new layer
      --ssh stringArray         SSH agent socket or keys to expose to the build (only if BuildKit enabled) (format:
                                default|<id>[=<socket>|<key>[,<key>]])
      --stream                  Stream attaches to server to negotiate build context
  -t, --tag list                Name and optionally a tag in the 'name:tag' format    #镜像的名字及标签,通常为 <项目名称 或 作者>/<镜像名>:<版本,默认为 latest>
      --target string           Set the target build stage to build.
      --ulimit ulimit           Ulimit options (default [])

docker history 命令用法

Usage:    docker history [OPTIONS] IMAGE

Show the history of an image

Options:
      --format string   Pretty-print images using a Go template
  -H, --human           Print sizes and dates in human readable format (default true)
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs

 

7、Dockerfile 案例:构建基于centos7基础镜像yum安装nginx服务镜像

# 调用官方基础镜像 centos7
FROM centos:7

# 执行命令
## 清空原yum源
## 下载阿里云源
## 下载阿里云epel源
## 重建yum数据缓存
## 安装基础工具及nginx
## 重设时区设置
## 写入网页文件
## 在nginx配置文件中增加 daemon off; 配置禁用后台运行
RUN     rm -rf /etc/yum.repos.d/* && \
        curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
        curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo && \
        yum clean all && \
        yum makecache fast && \
        yum install -y psmisc tree wget vim net-tools bash-completion iproute nginx-1.20.1-10.el7 && \
        rm -rf /etc/localtime && \
        ln -s ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
        mkdir /usr/share/doc/HTML -p && \
        echo "<h1>nginx-1.20.1 base centOS 7 on docker</h1>" > /usr/share/doc/HTML/index.html && \
        sed -i '/^pid/a daemon off;' /etc/nginx/nginx.conf

# 添加标签
## 作者:janzen<janzen.com>
## 版本:v1.0
## 描述 通过yum安装 nginx-1.20.1-10.el7
LABEL author="janzen<janzen.com>" version="v1.0" description="Installed nginx-1.20.1-10.el7 by yum"


# 容器对外暴露端口 80 443
EXPOSE 80 443

# 启动容器执行命令
ENTRYPOINT ["nginx"]

 

 

 

基于nginx镜像基础上创建app1镜像

[root@Docker-Ubu1804-p11:/images/app1]# ls
Dockerfile  index.html  www.tar
[root@Docker-Ubu1804-p11:/images/app1]# docker build -t janzen/app1:v3.0 .
Sending build context to Docker daemon  9.964MB
Step 1/9 : FROM centos:7
 ---> eeb6ee3f44bd
Step 2/9 : RUN    rm -rf /etc/yum.repos.d/* &&     curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &&      curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo &&     yum clean all &&     yum makecache fast &&     yum install -y psmisc tree wget vim net-tools bash-completion iproute nginx-1.20.1-10.el7 &&     rm -rf /etc/localtime &&     ln -s ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&     mkdir /usr/share/doc/HTML -p &&     echo "<h1>nginx-1.20.1 base centOS 7 on docker</h1>" > /usr/share/doc/HTML/index.html &&     sed -i '/^pid/a daemon off;' /etc/nginx/nginx.conf
 ---> Using cache
 ---> ecd44cfdf119
Step 3/9 : ENV user="janzen"
 ---> Running in cb9417981cb1
Removing intermediate container cb9417981cb1
 ---> ec209bf6726a
Step 4/9 : RUN `ls /usr/share/nginx/html | grep  ^[^0-9] `| xargs rm -rf
 ---> Running in bb6ba13888db
/bin/sh: en-US: command not found
Removing intermediate container bb6ba13888db
 ---> 2ba81f7248c1
Step 5/9 : COPY index.html /usr/share/nginx/html
 ---> 2a73d62713d0
Step 6/9 : ADD www.tar /usr/share/nginx/html
 ---> 77b6b9afdb4b
Step 7/9 : LABEL author="${user}<janzen.com>" version="v3.0" description="Run APP1 on nginx-1.20.1-10.el7 by yum"
 ---> Running in 642b5d444277
Removing intermediate container 642b5d444277
 ---> 3e73ebcf125b
Step 8/9 : EXPOSE 80 443
 ---> Running in 6846995f59c0
Removing intermediate container 6846995f59c0
 ---> 1f261302a9aa
Step 9/9 : ENTRYPOINT ["nginx"]
 ---> Running in 2f1113442ef0
Removing intermediate container 2f1113442ef0
 ---> f65a9531bfc7
Successfully built f65a9531bfc7
Successfully tagged janzen/app1:v3.0

 

Dockerfile文件

# 调用官方基础镜像 centos7
FROM centos:7

# 执行命令
## 清空原yum源
## 下载阿里云源
## 下载阿里云epel源
## 重建yum数据缓存
## 安装基础工具及nginx
## 重设时区设置
## 写入网页文件
## 在nginx配置文件中增加 daemon off; 配置禁用后台运行
RUN    rm -rf /etc/yum.repos.d/* && \
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
    curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo && \
    yum clean all && \
    yum makecache fast && \
    yum install -y psmisc tree wget vim net-tools bash-completion iproute nginx-1.20.1-10.el7 && \
    rm -rf /etc/localtime && \
    ln -s ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    mkdir /usr/share/doc/HTML -p && \
    echo "<h1>nginx-1.20.1 base centOS 7 on docker</h1>" > /usr/share/doc/HTML/index.html && \
    sed -i '/^pid/a daemon off;' /etc/nginx/nginx.conf

# 添加变量 user=janzen
ENV user="janzen"

# 运行命令:清空/usr/share/nginx/html目录下非数字开头的文件
RUN `ls /usr/share/nginx/html | grep  ^[^0-9] `| xargs rm -rf 

# 复制宿主机当前目录下的 index.html 到 容器的 /usr/share/nginx/html
COPY index.html /usr/share/nginx/html

# 复制 www.tar 到 容器的/usr/share/nginx/html 并解压
ADD www.tar /usr/share/nginx/html

# 添加标签
## 作者:调用变量$user <janzen.com>
## 版本:v3.0
## 描述 基于yum安装的 nginx-1.20.1-10.el7 运行APP1
LABEL author="${user}<janzen.com>" version="v3.0" description="Run APP1 on nginx-1.20.1-10.el7 by yum"

# 容器对外暴露端口 80 443
EXPOSE 80 443

# 启动容器执行命令
ENTRYPOINT ["nginx"]

 

运行容器,并查看结果

[root@Docker-Ubu1804-p11:/images/app1]# docker run -d -p 8080:80 janzen/app1:v3.0 
b78238dfc17151a5ce2d3ec94bfa1da045112bb1833387d304ea505d8bb40601
[root@Docker-Ubu1804-p11:/images/app1]# 

 

 

热门相关:帝少的专属:小甜心,太缠人   横行霸道   战神   夫人你马甲又掉了   战神