Dockerfile讲解和案例分享

目录

dockerfile是在容器外部

 构建三部曲

dockerfile基本知识

dockerfile执行流程

dockerfile 常用保留字

 FROM

MAINTAINER

RUN 

 EXPOSE

WORKDIR 

USER 

ENV

ADD

COPY

VOLUME

CMD

CMD 和 RUN 区别

ENTRYPOINT

 案例分享

自定义镜像mycentosjava8

初始的centos镜像不包含vim ifconfig 命令

准备好jdk文件

编写:新建Dockerfile文件

Dockerfile 内容如下:

构建

验证效果


dockerfile是在容器外部

Dockerfile讲解和案例分享_第1张图片

 构建三部曲

1、编写Dockerfile文件

2、docker build 构建新镜像

3、docker run 运行新镜像

dockerfile基本知识

1、保留字指令均为大写 且后面要跟随至少一个参数

2、指令从上到下顺序执行         

3、“#” 表示注解

4、每条指令都会创建一个新的镜像层并对镜像进行提交

dockerfile执行流程

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

2、执行一条 指令并对容器做出修改

3、执行类似docker commit的操作提交一个新的镜像层

4、docker 再基于刚提交的镜像运行一个新容器

5、执行dockerfile中的下一条指令,直到所有的指令都执行完成

dockerfile 常用保留字

 FROM

基础镜像,当前新镜像是基于哪个镜像的,指定一个已经存在的镜像作为模板,第一条必须是FROM

MAINTAINER

维护者

RUN 

容器构建时,需要运行的命令

两种格式:shell 和exec

      RUN  yum  -y install vim

      RUN   ["./test.php","dev","offline"]

RUN是在docker build时运行

 EXPOSE

当前容器对外暴露出来的端口

WORKDIR 

容器run后,默认的工作目录

USER 

指定该镜像以什么样的用户去执行,如果都不执行,默认是root

ENV

用来在构建镜像过程中,设置环境变量

ADD

将宿主机目录下的文件拷贝进镜像, 且会自动处理URL和tar压缩包     

相当于copy + 解压  

COPY

相对于 docker copy 

VOLUME

容器卷

CMD

指定容器启动后要干的事情

CMD指令的格式和RUN相似,也是两种格式

1、shell格式: CMD 命令

2、exec 格式: CMD [“可执行文件”,“参数1”,“参数2”,...]

3、参数列表格式:CMD ["参数1","参数2"......],在指定了ENTRYPOINT后,用CMD 指定具体的参数

注意:

Dockerfile 可以有多个CMD指令,但是只有最后一个生效,CMD会被docker run 之后的参数替换

CMD 和 RUN 区别

CMD 是在docker run的时候运行

RUN 是在docker build时运行

ENTRYPOINT

也是用来指定一个容器启动时要运行的命令

类似CMD命令,但是ENTRPOINT不会被docker run 后面的参数覆盖,而且这些命令行参数会被当作参数传给ENTRPOINT指令指定的程序

执行方式:

ENTRPOINT [执行的命令,参数1,参数2,....]

Dockerfile讲解和案例分享_第2张图片

 案例分享

自定义镜像mycentosjava8

初始的centos镜像不包含vim ifconfig 命令

[root@localhost ~]# docker images centos
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    5d0da3dc9764   10 months ago   231MB
[root@localhost ~]# docker run -it 5d0da3dc9764 /bin/bash
[root@d398654d99e2 /]# vim a.txt
bash: vim: command not found
[root@d398654d99e2 /]# ifconfig
bash: ifconfig: command not found
[root@d398654d99e2 /]# java -version
bash: java: command not found
[root@d398654d99e2 /]#

准备好jdk文件

[root@localhost myfile]# pwd
/myfile
[root@localhost myfile]# ls
jdk-8u171-linux-x64.tar.gz
[root@localhost myfile]#

[root@localhost myfile]# ll
total 186420
-rw-r--r--. 1 root root 190890122 May 28 10:56 jdk-8u171-linux-x64.tar.gz
[root@localhost myfile]#

编写:新建Dockerfile文件

Dockerfile 内容如下:

From centos

MAINTAINER wll

ENV MYPATH /usr/local

WORKDIR $MYPATH

#解决yum 安装包报问题,不报错可以删除
RUN  sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
RUN  sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*

#安装vim编辑器

RUN yum -y install vim

#安装ifconfig

RUN yum -y install net-tools

#安装java8和lib库

RUN yum -y install glibc.i686

RUN mkdir /usr/local/java

#ADD 是相对路径jar,把jdk压缩包添加到容器中,安装包必须要和Dockerfile 文件在同一位置

ADD jdk-8u171-linux-x64.tar.gz /usr/local/java/

#配置java环境变量

ENV JAVA_HOME /usr/local/java/jdk1.8.0_171

ENV JRE_HOME $JAVA_HOME/jre

ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$CLASSPATH

ENV PATH $JAVA_HOME/bin:$PATH

EXPOSE 80

CMD echo $MYPATH

CMD echo "sucess .............OK "

CMD /bin/bash


[root@localhost myfile]# cat Dockerfile
From centos

MAINTAINER wll 



ENV MYPATH /usr/local

WORKDIR $MYPATH


#解决yum 安装包报问题,不报错可以删除
RUN  sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
RUN  sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*


#安装vim编辑器

RUN yum -y install vim

#安装ifconfig

RUN yum -y install net-tools

#安装java8和lib库

RUN yum -y install glibc.i686

RUN mkdir /usr/local/java

#ADD 是相对路径jar,把jdk压缩包添加到容器中,安装包必须要和Dockerfile 文件在同一位置

ADD jdk-8u171-linux-x64.tar.gz /usr/local/java/

#配置java环境变量

ENV JAVA_HOME /usr/local/java/jdk1.8.0_171

ENV JRE_HOME $JAVA_HOME/jre

ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$CLASSPATH

ENV PATH $JAVA_HOME/bin:$PATH



EXPOSE 80

CMD echo $MYPATH

CMD echo "sucess .............OK "

CMD /bin/bash
[root@localhost myfile]#

构建

docker build -t 新镜像名称:TAG .

注意,上面TAG后面一个空格,有个点

[root@localhost myfile]# docker build -t mycentosjava8:1.1 .
Sending build context to Docker daemon  190.9MB
Step 1/19 : From centos
 ---> 5d0da3dc9764
Step 2/19 : MAINTAINER wll 
 ---> Using cache
 ---> 35b033456e12
Step 3/19 : ENV MYPATH /usr/local
 ---> Using cache
 ---> 1b2d6e2a07b7
Step 4/19 : WORKDIR $MYPATH
 ---> Using cache
 ---> 54a2b2a83fbe
Step 5/19 : RUN  sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
 ---> Running in 4d32c925b1ac
Removing intermediate container 4d32c925b1ac
 ---> 1884af156ad6
Step 6/19 : RUN  sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
 ---> Running in 27f77bd74efa
Removing intermediate container 27f77bd74efa
 ---> 34a3c4ff47cc
Step 7/19 : RUN yum -y install vim
 ---> Running in 6efc2f3d8a28
CentOS Linux 8 - AppStream                      1.4 MB/s | 8.4 MB     00:06
CentOS Linux 8 - BaseOS                         1.1 MB/s | 4.6 MB     00:04
CentOS Linux 8 - Extras                         5.5 kB/s |  10 kB     00:01
Dependencies resolved.
================================================================================
 Package             Arch        Version                   Repository      Size
================================================================================
Installing:
 vim-enhanced        x86_64      2:8.0.1763-16.el8         appstream      1.4 M
Installing dependencies:
 gpm-libs            x86_64      1.20.7-17.el8             appstream       39 k
 vim-common          x86_64      2:8.0.1763-16.el8         appstream      6.3 M
 vim-filesystem      noarch      2:8.0.1763-16.el8         appstream       49 k
 which               x86_64      2.21-16.el8               baseos          49 k

Transaction Summary
================================================================================
Install  5 Packages

Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm         37 kB/s |  39 kB     00:01
(2/5): vim-filesystem-8.0.1763-16.el8.noarch.rp 110 kB/s |  49 kB     00:00
(3/5): vim-enhanced-8.0.1763-16.el8.x86_64.rpm  749 kB/s | 1.4 MB     00:01
(4/5): which-2.21-16.el8.x86_64.rpm             117 kB/s |  49 kB     00:00
(5/5): vim-common-8.0.1763-16.el8.x86_64.rpm    1.2 MB/s | 6.3 MB     00:05
--------------------------------------------------------------------------------
Total                                           1.5 MB/s | 7.8 MB     00:05
warning: /var/cache/dnf/appstream-d7987f026ef99c82/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream                      971 kB/s | 1.6 kB     00:00
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) "
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : which-2.21-16.el8.x86_64                               1/5
  Installing       : vim-filesystem-2:8.0.1763-16.el8.noarch                2/5
  Installing       : vim-common-2:8.0.1763-16.el8.x86_64                    3/5
  Installing       : gpm-libs-1.20.7-17.el8.x86_64                          4/5
  Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64                          4/5
  Installing       : vim-enhanced-2:8.0.1763-16.el8.x86_64                  5/5
  Running scriptlet: vim-enhanced-2:8.0.1763-16.el8.x86_64                  5/5
  Running scriptlet: vim-common-2:8.0.1763-16.el8.x86_64                    5/5
  Verifying        : gpm-libs-1.20.7-17.el8.x86_64                          1/5
  Verifying        : vim-common-2:8.0.1763-16.el8.x86_64                    2/5
  Verifying        : vim-enhanced-2:8.0.1763-16.el8.x86_64                  3/5
  Verifying        : vim-filesystem-2:8.0.1763-16.el8.noarch                4/5
  Verifying        : which-2.21-16.el8.x86_64                               5/5

Installed:
  gpm-libs-1.20.7-17.el8.x86_64         vim-common-2:8.0.1763-16.el8.x86_64
  vim-enhanced-2:8.0.1763-16.el8.x86_64 vim-filesystem-2:8.0.1763-16.el8.noarch
  which-2.21-16.el8.x86_64

Complete!
Removing intermediate container 6efc2f3d8a28
 ---> 559287f16279
Step 8/19 : RUN yum -y install net-tools
 ---> Running in 616b1684e5bf
Last metadata expiration check: 0:00:13 ago on Sun Aug  7 04:30:43 2022.
Dependencies resolved.
================================================================================
 Package         Architecture Version                        Repository    Size
================================================================================
Installing:
 net-tools       x86_64       2.0-0.52.20160912git.el8       baseos       322 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm   147 kB/s | 322 kB     00:02
--------------------------------------------------------------------------------
Total                                           147 kB/s | 322 kB     00:02
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : net-tools-2.0-0.52.20160912git.el8.x86_64              1/1
  Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64              1/1
  Verifying        : net-tools-2.0-0.52.20160912git.el8.x86_64              1/1

Installed:
  net-tools-2.0-0.52.20160912git.el8.x86_64

Complete!
Removing intermediate container 616b1684e5bf
 ---> 1870b6ea21a2
Step 9/19 : RUN yum -y install glibc.i686
 ---> Running in dd7a267eaa04
Last metadata expiration check: 0:00:17 ago on Sun Aug  7 04:30:43 2022.
Dependencies resolved.
================================================================================
 Package                     Arch        Version              Repository   Size
================================================================================
Installing:
 glibc                       i686        2.28-164.el8         baseos      3.4 M
Upgrading:
 glibc                       x86_64      2.28-164.el8         baseos      3.6 M
 glibc-common                x86_64      2.28-164.el8         baseos      1.3 M
 glibc-minimal-langpack      x86_64      2.28-164.el8         baseos       58 k
Installing weak dependencies:
 glibc-langpack-en           x86_64      2.28-164.el8         baseos      828 k

Transaction Summary
================================================================================
Install  2 Packages
Upgrade  3 Packages

Total download size: 9.3 M
Downloading Packages:
(1/5): glibc-2.28-164.el8.x86_64.rpm            799 kB/s | 3.6 MB     00:04
(2/5): glibc-2.28-164.el8.i686.rpm              729 kB/s | 3.4 MB     00:04
(3/5): glibc-langpack-en-2.28-164.el8.x86_64.rp 165 kB/s | 828 kB     00:05
(4/5): glibc-minimal-langpack-2.28-164.el8.x86_ 149 kB/s |  58 kB     00:00
(5/5): glibc-common-2.28-164.el8.x86_64.rpm     1.4 MB/s | 1.3 MB     00:00
--------------------------------------------------------------------------------
Total                                           1.6 MB/s | 9.3 MB     00:05
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Upgrading        : glibc-common-2.28-164.el8.x86_64                       1/8
  Running scriptlet: glibc-2.28-164.el8.x86_64                              2/8
  Upgrading        : glibc-2.28-164.el8.x86_64                              2/8
  Running scriptlet: glibc-2.28-164.el8.x86_64                              2/8
  Installing       : glibc-langpack-en-2.28-164.el8.x86_64                  3/8
  Upgrading        : glibc-minimal-langpack-2.28-164.el8.x86_64             4/8
  Running scriptlet: glibc-2.28-164.el8.i686                                5/8
  Installing       : glibc-2.28-164.el8.i686                                5/8
  Running scriptlet: glibc-2.28-164.el8.i686                                5/8
  Cleanup          : glibc-2.28-151.el8.x86_64                              6/8
  Cleanup          : glibc-minimal-langpack-2.28-151.el8.x86_64             7/8
  Cleanup          : glibc-common-2.28-151.el8.x86_64                       8/8
  Running scriptlet: glibc-common-2.28-151.el8.x86_64                       8/8
  Running scriptlet: glibc-common-2.28-164.el8.x86_64                       8/8
  Verifying        : glibc-2.28-164.el8.i686                                1/8
  Verifying        : glibc-langpack-en-2.28-164.el8.x86_64                  2/8
  Verifying        : glibc-2.28-164.el8.x86_64                              3/8
  Verifying        : glibc-2.28-151.el8.x86_64                              4/8
  Verifying        : glibc-common-2.28-164.el8.x86_64                       5/8
  Verifying        : glibc-common-2.28-151.el8.x86_64                       6/8
  Verifying        : glibc-minimal-langpack-2.28-164.el8.x86_64             7/8
  Verifying        : glibc-minimal-langpack-2.28-151.el8.x86_64             8/8

Upgraded:
  glibc-2.28-164.el8.x86_64                   glibc-common-2.28-164.el8.x86_64
  glibc-minimal-langpack-2.28-164.el8.x86_64
Installed:
  glibc-2.28-164.el8.i686         glibc-langpack-en-2.28-164.el8.x86_64

Complete!
Removing intermediate container dd7a267eaa04
 ---> 695752145516
Step 10/19 : RUN mkdir /usr/local/java
 ---> Running in a7a66cc1983f
Removing intermediate container a7a66cc1983f
 ---> 2866cb535bf3
Step 11/19 : ADD jdk-8u171-linux-x64.tar.gz /usr/local/java/
 ---> 519a88ce1940
Step 12/19 : ENV JAVA_HOME /usr/local/java/jdk1.8.0_171
 ---> Running in be51591ffa59
Removing intermediate container be51591ffa59
 ---> b26553b3d016
Step 13/19 : ENV JRE_HOME $JAVA_HOME/jre
 ---> Running in 2aa8366dbd94
Removing intermediate container 2aa8366dbd94
 ---> 12a110b4ea87
Step 14/19 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$CLASSPATH
 ---> Running in 1900dc0cd058
Removing intermediate container 1900dc0cd058
 ---> 817d4f9c1ec3
Step 15/19 : ENV PATH $JAVA_HOME/bin:$PATH
 ---> Running in d6baf14c4e48
Removing intermediate container d6baf14c4e48
 ---> b3df19ae38ec
Step 16/19 : EXPOSE 80
 ---> Running in c4e310a8a43a
Removing intermediate container c4e310a8a43a
 ---> 65f5e0b23914
Step 17/19 : CMD echo $MYPATH
 ---> Running in 0066a690ccb9
Removing intermediate container 0066a690ccb9
 ---> 13145c283528
Step 18/19 : CMD echo "sucess .............OK "
 ---> Running in b184f89ca033
Removing intermediate container b184f89ca033
 ---> 58075b69c554
Step 19/19 : CMD /bin/bash
 ---> Running in 7efc04efd875
Removing intermediate container 7efc04efd875
 ---> 1bfb672d66a3
Successfully built 1bfb672d66a3
Successfully tagged mycentosjava8:1.1
[root@localhost myfile]#

 查看,已经形成了新的镜像文件

[root@localhost myfile]# docker images
REPOSITORY                 TAG       IMAGE ID       CREATED         SIZE
mycentosjava8              1.1       1bfb672d66a3   3 minutes ago   786MB
mysql                      5.7       3147495b3a5c   11 days ago     431MB

验证效果

[root@localhost myfile]# docker run -it mycentosjava8:1.1 /bin/bash
[root@8a2307e66a49 local]# pwd
/usr/local
[root@8a2307e66a49 local]# vim 123.txt
[root@8a2307e66a49 local]# ifconfig
eth0: flags=4163  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@8a2307e66a49 local]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
[root@8a2307e66a49 local]#

你可能感兴趣的:(docker,docker,容器,kubernetes)