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 daemon2.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的基础知识
- 每个关键字(指令)都必须大写
- 执行过程从上到下,顺序执行
- #为注释
- 每个指令代表一层,都会创建提交一个新的镜像层
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.重复执行修改容器、提交镜像、运行容器指令,直到所有指令执行完成。
注:每条指令都会生成新的镜像,也就是生成中间层镜像,这样执行速度其实会很慢,可以不使用缓存。在构建命令中加上3. 构建一个自己的centos 我们已经熟悉了构建Dockerfile的基础指令,现在可以做一些基本实践了。--no-cache=true
即可。可根据实际场景选择是否需要缓存。
3.1 运行一个官方centos 首先我们运行一个原生的centos,然后做一些linux的基本操作
[root@hadoop101 ~]# docker run -it centos /bin/bash
[root@a0db59573b95 /]# ls
bindevetchomeliblib64lost+foundmediamntoptprocrootrunsbinsrvsystmpusrvar
[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
#指定作者
MAINTAINERzhangbao
?
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 daemon2.048kB
Step 1/9 : FROM centos
---> 5d0da3dc9764
Step 2/9 : MAINTAINERzhangbao
---> 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 - AppStream64B/s |38B00: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
#指定作者
MAINTAINERzhangbao#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-toolsEXPOSE 80CMD 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 daemon2.56kB
Step 1/14 : FROM centos
---> 5d0da3dc9764
Step 2/14 : MAINTAINERzhangbao
---> 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 % XferdAverage SpeedTimeTimeTimeCurrent
DloadUploadTotalSpentLeftSpeed
1002495100249500123510 --:--:-- --:--:-- --:--:-- 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.com16 MB/s | 4.6 MB00:00
CentOS-8.5.2111 - Extras - mirrors.aliyun.com45 kB/s |10 kB00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.co16 MB/s | 8.4 MB00: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 Apr5 09:35:43 2022.
Dependencies resolved.
================================================================================
PackageArchVersionRepositorySize
================================================================================
Installing:
vim-enhancedx86_642:8.0.1763-16.el8AppStream1.4 M
Installing dependencies:
gpm-libsx86_641.20.7-17.el8AppStream39 k
vim-commonx86_642:8.0.1763-16.el8AppStream6.3 M
vim-filesystemnoarch2:8.0.1763-16.el8AppStream49 k
whichx86_642.21-16.el8base49 kTransaction Summary
================================================================================
Install5 PackagesTotal download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm235 kB/s |39 kB00:00
(2/5): which-2.21-16.el8.x86_64.rpm203 kB/s |49 kB00:00
(3/5): vim-filesystem-8.0.1763-16.el8.noarch.rp 776 kB/s |49 kB00:00
(4/5): vim-common-8.0.1763-16.el8.x86_64.rpm16 MB/s | 6.3 MB00:00
(5/5): vim-enhanced-8.0.1763-16.el8.x86_64.rpm5.4 MB/s | 1.4 MB00:00
--------------------------------------------------------------------------------
Total19 MB/s | 7.8 MB00: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.noarch1/5
Installing: vim-common-2:8.0.1763-16.el8.x86_642/5
Installing: gpm-libs-1.20.7-17.el8.x86_643/5
Running scriptlet: gpm-libs-1.20.7-17.el8.x86_643/5
Installing: which-2.21-16.el8.x86_644/5
Installing: vim-enhanced-2:8.0.1763-16.el8.x86_645/5
Running scriptlet: vim-enhanced-2:8.0.1763-16.el8.x86_645/5
Running scriptlet: vim-common-2:8.0.1763-16.el8.x86_645/5
Verifying: which-2.21-16.el8.x86_641/5
Verifying: gpm-libs-1.20.7-17.el8.x86_642/5
Verifying: vim-common-2:8.0.1763-16.el8.x86_643/5
Verifying: vim-enhanced-2:8.0.1763-16.el8.x86_644/5
Verifying: vim-filesystem-2:8.0.1763-16.el8.noarch5/5Installed:
gpm-libs-1.20.7-17.el8.x86_64vim-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_64Complete!
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 Apr5 09:35:43 2022.
Dependencies resolved.
================================================================================
PackageArchitecture VersionRepositorySize
================================================================================
Installing:
net-toolsx86_642.0-0.52.20160912git.el8base322 kTransaction Summary
================================================================================
Install1 PackageTotal download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm934 kB/s | 322 kB00:00
--------------------------------------------------------------------------------
Total930 kB/s | 322 kB00: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_641/1
Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_641/1
Verifying: net-tools-2.0-0.52.20160912git.el8.x86_641/1Installed:
net-tools-2.0-0.52.20160912git.el8.x86_64Complete!
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=4163mtu 1500
inet 172.17.0.2netmask 255.255.0.0broadcast 172.17.255.255
ether 02:42:ac:11:00:02txqueuelen 0(Ethernet)
RX packets 8bytes 656 (656.0 B)
RX errors 0dropped 0overruns 0frame 0
TX packets 0bytes 0 (0.0 B)
TX errors 0dropped 0 overruns 0carrier 0collisions 0lo: flags=73mtu 65536
inet 127.0.0.1netmask 255.0.0.0
looptxqueuelen 1000(Local Loopback)
RX packets 0bytes 0 (0.0 B)
RX errors 0dropped 0overruns 0frame 0
TX packets 0bytes 0 (0.0 B)
TX errors 0dropped 0 overruns 0carrier 0collisions 0[root@2f70cc22b493 local]#
可以看到
vim
和 ifconfig
命令已经可以使用,ll
命令还需要下载对应的工具包,这里就不在下载了。【Dockerfile入门】更多请在某公号平台搜索:选手一号位,本文编号:2003,回复即可获取。