Dockerfile入门

1.Dockerfile介绍

在之前Docker的使用中,我们直接从仓库下载需要的镜像到本地,然后稍加配置就可以应用了,通常从仓库下载下来的镜像都是通用的,无任何私有化的东西,我们拿过来就需要加很多的配置,每次使用就很麻烦。如果我们想定制化某一个镜像可以吗?比如Nginx,我不想每次使用都加很多的配置,而是定制化后,我再按照定制化的规则去使用,及时的安装部署我需要的Nginx环境。DockerFile就是做这个工作的,一个用基础镜像来构建新镜像的文本文件,里面包含构建镜像需要的各种指令。

1.1 编写Dockerfile

使用Dockerfile构建Nginx镜像,在一个空目录下创建Dockerfile文件

[root@hadoop101 nginx]# pwd
/home/docker_test/docker_files/nginx
[root@hadoop101 nginx]# vim Dockerfile
[root@hadoop101 nginx]# ll
total 4
-rw-r--r-- 1 root root 153 Mar 29 23:33 Dockerfile

Dockerfile内容如下:

FROM nginx
​
VOLUME ["volume01","volume02"]
​
RUN echo 'welcome,this is a nginx image of build local.' > /usr/share/nginx/html/index.html
​
RUN echo '----end----'
​
RUN /bin/bash

1.2 构建镜像

[root@hadoop101 nginx]# docker build -t zhangbao/nginx001:1.0 .

[root@hadoop101 nginx]# docker build -t zhangbao/nginx001:1.0 .
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM nginx
 ---> 605c77e624dd
Step 2/5 : VOLUME ["volume01","volume02"]
 ---> Running in d9ff85f068bf
Removing intermediate container d9ff85f068bf
 ---> 801484ddbb09
Step 3/5 : RUN echo '构建本地nginx镜像' > /usr/share/nginx/html/index.html
 ---> Running in 6614ebace633
Removing intermediate container 6614ebace633
 ---> 29a385ed17b6
Step 4/5 : RUN echo '----end----'
 ---> Running in 3449cc6cf814
----end----
Removing intermediate container 3449cc6cf814
 ---> 5d6941c830a4
Step 5/5 : RUN /bin/bash
 ---> Running in 1f106444b093
Removing intermediate container 1f106444b093
 ---> 440fd4b943c7
Successfully built 440fd4b943c7
Successfully tagged zhangbao/nginx001:1.0
[root@hadoop101 nginx]#

1.3 运行镜像

docker run -d -p 80:80 zhangbao/nginx001:1.0

访问:http://192.168.88.71/

首页可以查看 welcome,this is a nginx image of build local.

2. Dockerfile构建过程

构建一个新镜像,我们可以大致分为三个步骤:编写Dockerfile文件 > docker build > docker run

2.1 基础知识

Dockerfile的基础知识

  1. 每个关键字(指令)都必须大写

  2. 执行过程从上到下,顺序执行

  3. #为注释

  4. 每个指令代表一层,都会创建提交一个新的镜像层

Dockerfile指令

FROM            指定基础镜像
MAINTAINER      指定作者
RUN             执行参数中定义的命令,构建镜像时需要的命令
EXPOSE          向容器外部公开的端口号
WORKDIR         设置容器内默认工作目录
USER            指定用户
ENTROYPOINT     指定一个容器启动时运行的命令
ENV             设置环境变量
ADD|COPY        复制文件到镜像中
VOLUME          容器数据卷,向镜像创建的容器添加卷
CMD             容器启动时要运行的命令,可以有多个,但只有最后一个生效

2.2 构建过程

Dockerfile的构建过程就是build的执行过程

1.把当前目录和子目录当做上下文传递给docker服务,命令最后的点表示当前上下文。
2.从当前目录(不包括子目录)找到Dockerfile文件,如果不指定文件,必须是此文件名。
3.检查docker语法。
4.从基础镜像运行一个容器。
5.执行指令,修改容器,如上面操作添加数据卷,修改首页。
6.对修改后的容器提交一个新的镜像层,也可叫做中间层镜像。
7.针对中间层生成的镜像,运行新的容器。
8.重复执行修改容器、提交镜像、运行容器指令,直到所有指令执行完成。

注:每条指令都会生成新的镜像,也就是生成中间层镜像,这样执行速度其实会很慢,可以不使用缓存。在构建命令中加上--no-cache=true 即可。可根据实际场景选择是否需要缓存。

3. 构建一个自己的centos

我们已经熟悉了构建Dockerfile的基础指令,现在可以做一些基本实践了。

3.1 运行一个官方centos

首先我们运行一个原生的centos,然后做一些linux的基本操作

[root@hadoop101 ~]# docker run -it centos /bin/bash
[root@a0db59573b95 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@a0db59573b95 /]# ll
bash: ll: command not found
[root@a0db59573b95 /]# vim test
bash: vim: command not found
[root@a0db59573b95 /]# ifconfig
bash: ifconfig: command not found
[root@a0db59573b95 /]#

可以看到,我们已经运行一个centos,然后发现一些基础的命令是没有的,这就说明这个contos是一个压缩版的,如果我们想运行一个有这些命令的镜像呢?这就需要在Dockerfile中做一些操作了?

3.2 编写Dockerfile文件

记住在一个空白的目录下创建Dockerfile文件,我这里文件名dockerfile-mycentos,内容如下:

FROM centos
#指定作者
MAINTAINER  zhangbao<zhangbaohpu@163.com>
​
ENV MYPATH /usr/local
WORKDIR $MYPATH
​
#下载需要的工具类
RUN yum -y install vim
RUN yum -y install net-tools
​
EXPOSE 80
​
CMD echo "--end--"
CMD /bin/bash

3.3 构建新镜像

如果是自定义Dockerfile文件名,则构建时必须指定文件名,否则文件名必须是:Dockerfile

[root@hadoop101 mycentos]# docker build -f dockerfile-mycentos -t mysentos:1.0 .
Sending build context to Docker daemon  2.048kB
Step 1/9 : FROM centos
 ---> 5d0da3dc9764
Step 2/9 : MAINTAINER  zhangbao<zhangbaohpu@163.com>
 ---> Running in d9b4afe40ef6
Removing intermediate container d9b4afe40ef6
 ---> 87e522a4dcb6
Step 3/9 : ENV MYPATH /usr/local
 ---> Running in 2fe975209f1e
Removing intermediate container 2fe975209f1e
 ---> 30491c2e1634
Step 4/9 : WORKDIR $MYPATH
 ---> Running in cd9b1810f244
Removing intermediate container cd9b1810f244
 ---> d2addbefe4f6
Step 5/9 : RUN yum -y install vim
 ---> Running in d6a8caa012e9
CentOS Linux 8 - AppStream                       64  B/s |  38  B     00:00
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
The command '/bin/sh -c yum -y install vim' returned a non-zero code: 1
[root@hadoop101 mycentos]#

3.4 问题及修复

这时候发现失败了,查问题是centos8的官方源已经下线了,导致yum下载失败。那刚好,我们在自定义镜像的时候可以修复这些问题。只需修改yum的默认源就可以了,这个操作也放在Dockerfile文件内。我们修复后Dockerfile文件如下:

FROM centos
#指定作者
MAINTAINER  zhangbao<zhangbaohpu@163.com>

#ENV kv格式,这里指定工作目录,及默认进入容器的目录
ENV MYPATH /usr/local
WORKDIR $MYPATH

#将原有的yum配置备份下
RUN tar cvf /etc/yum.repos.d.tar /etc/yum.repos.d

#修改yum源配置,
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

#删除掉多于的repo文件
RUN rm -rf /etc/yum.repos.d/CentOS-Linux-*

#使yum配置更新
RUN yum clean all
RUN yum makecache

#下载需要的工具类
RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE 80

CMD echo "--end--"
CMD /bin/bash

3.5 重新构建镜像

重新构建下修改后的Dockerfile文件

docker build -f dockerfile-mycentos -t mysentos:1.0 .

[root@hadoop101 mycentos]# docker build -f dockerfile-mycentos -t mysentos:1.0 .
Sending build context to Docker daemon   2.56kB
Step 1/14 : FROM centos
 ---> 5d0da3dc9764
Step 2/14 : MAINTAINER  zhangbao<zhangbaohpu@163.com>
 ---> Running in 79ff0ddc554d
Removing intermediate container 79ff0ddc554d
 ---> 0cc1bf4a4ba0
Step 3/14 : ENV MYPATH /usr/local
 ---> Running in 59d3d432c58c
Removing intermediate container 59d3d432c58c
 ---> 839ed1e4b944
Step 4/14 : WORKDIR $MYPATH
 ---> Running in d085e4887d92
Removing intermediate container d085e4887d92
 ---> 41c10774900b
Step 5/14 : RUN tar cvf /etc/yum.repos.d.tar /etc/yum.repos.d
 ---> Running in 1ab58ab850c0
/etc/yum.repos.d/
/etc/yum.repos.d/CentOS-Linux-AppStream.repo
/etc/yum.repos.d/CentOS-Linux-BaseOS.repo
/etc/yum.repos.d/CentOS-Linux-ContinuousRelease.repo
/etc/yum.repos.d/CentOS-Linux-Debuginfo.repo
/etc/yum.repos.d/CentOS-Linux-Devel.repo
/etc/yum.repos.d/CentOS-Linux-Extras.repo
/etc/yum.repos.d/CentOS-Linux-FastTrack.repo
/etc/yum.repos.d/CentOS-Linux-HighAvailability.repo
/etc/yum.repos.d/CentOS-Linux-Media.repo
/etc/yum.repos.d/CentOS-Linux-Plus.repo
/etc/yum.repos.d/CentOS-Linux-PowerTools.repo
/etc/yum.repos.d/CentOS-Linux-Sources.repo
tar: Removing leading `/' from member names
Removing intermediate container 1ab58ab850c0
 ---> bfd1c4cd694c
Step 6/14 : RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
 ---> Running in 73ebef623f8a
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0  12351      0 --:--:-- --:--:-- --:--:-- 12351
Removing intermediate container 73ebef623f8a
 ---> 22812c68f967
Step 7/14 : RUN rm -rf /etc/yum.repos.d/CentOS-Linux-*
 ---> Running in eb8426f499b0
Removing intermediate container eb8426f499b0
 ---> 8dc6bd0b4e84
Step 8/14 : RUN yum clean all
 ---> Running in a508fb503158
0 files removed
Removing intermediate container a508fb503158
 ---> c6831cfda20f
Step 9/14 : RUN yum makecache
 ---> Running in 67c3f0dc0a6a
CentOS-8.5.2111 - Base - mirrors.aliyun.com      16 MB/s | 4.6 MB     00:00
CentOS-8.5.2111 - Extras - mirrors.aliyun.com    45 kB/s |  10 kB     00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.co  16 MB/s | 8.4 MB     00:00
Metadata cache created.
Removing intermediate container 67c3f0dc0a6a
 ---> 6ca3b2fbeb13
Step 10/14 : RUN yum -y install vim
 ---> Running in e81b9f1a4705
Last metadata expiration check: 0:00:05 ago on Tue Apr  5 09:35:43 2022.
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               base            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        235 kB/s |  39 kB     00:00
(2/5): which-2.21-16.el8.x86_64.rpm             203 kB/s |  49 kB     00:00
(3/5): vim-filesystem-8.0.1763-16.el8.noarch.rp 776 kB/s |  49 kB     00:00
(4/5): vim-common-8.0.1763-16.el8.x86_64.rpm     16 MB/s | 6.3 MB     00:00
(5/5): vim-enhanced-8.0.1763-16.el8.x86_64.rpm  5.4 MB/s | 1.4 MB     00:00
--------------------------------------------------------------------------------
Total                                            19 MB/s | 7.8 MB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : vim-filesystem-2:8.0.1763-16.el8.noarch                1/5
  Installing       : vim-common-2:8.0.1763-16.el8.x86_64                    2/5
  Installing       : gpm-libs-1.20.7-17.el8.x86_64                          3/5
  Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64                          3/5
  Installing       : which-2.21-16.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        : which-2.21-16.el8.x86_64                               1/5
  Verifying        : gpm-libs-1.20.7-17.el8.x86_64                          2/5
  Verifying        : vim-common-2:8.0.1763-16.el8.x86_64                    3/5
  Verifying        : vim-enhanced-2:8.0.1763-16.el8.x86_64                  4/5
  Verifying        : vim-filesystem-2:8.0.1763-16.el8.noarch                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 e81b9f1a4705
 ---> 3d731b0904e6
Step 11/14 : RUN yum -y install net-tools
 ---> Running in f45f6c7c9c8c
Last metadata expiration check: 0:00:17 ago on Tue Apr  5 09:35:43 2022.
Dependencies resolved.
================================================================================
 Package         Architecture Version                          Repository  Size
================================================================================
Installing:
 net-tools       x86_64       2.0-0.52.20160912git.el8         base       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   934 kB/s | 322 kB     00:00
--------------------------------------------------------------------------------
Total                                           930 kB/s | 322 kB     00:00
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 f45f6c7c9c8c
 ---> adc0f183e07e
Step 12/14 : EXPOSE 80
 ---> Running in 9422041e18c2
Removing intermediate container 9422041e18c2
 ---> e94ae254891f
Step 13/14 : CMD echo "--end--"
 ---> Running in 9183b6a1444d
Removing intermediate container 9183b6a1444d
 ---> c3978bb3cb17
Step 14/14 : CMD /bin/bash
 ---> Running in 536c8de964d3
Removing intermediate container 536c8de964d3
 ---> 68f0ddc7c99f
Successfully built 68f0ddc7c99f
Successfully tagged mysentos:1.0
[root@hadoop101 mycentos]#

总共分为14步,即每个指令一步,也表示每一步算一层。整个构建过程还是很详细的。

3.6 运行自定义镜像

自定义镜像已经构建完成,下面我们开始运行镜像,并检测下基础命令

[root@hadoop101 mycentos]# docker run -it mysentos:1.0
[root@2f70cc22b493 local]# pwd
/usr/local
[root@2f70cc22b493 local]# ll
bash: ll: command not found
[root@2f70cc22b493 local]# vim test
[root@2f70cc22b493 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  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<UP,LOOPBACK,RUNNING>  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@2f70cc22b493 local]#

可以看到 vimifconfig 命令已经可以使用,ll 命令还需要下载对应的工具包,这里就不在下载了。

更多请在某公号平台搜索:选手一号位,本文编号:2003,回复即可获取。

    原文作者:选手一号位
    原文地址: https://www.cnblogs.com/zhangbaohpu/p/16104567.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞