什么是DockerFile
来看一下官方文档的描述
Docker can build images automatically by reading the instructions from a
Dockerfile
. ADockerfile
is a text document that contains all the commands a user could call on the command line to assemble an image. Usingdocker build
users can create an automated build that executes several command-line instructions in succession.
简单来说就是
- DockerFile是一个用来创建镜像的脚本文件
- 可以通过build命令执行DockerFile生成用户自己的镜像
DockerFile基础知识
- DockerFile是case-insensitive的,但为了区分,我们通常大写命令,小写参数
- DockerFile执行命令为自上而下
- DockerFile的命令
FROM #从指定的镜像为基础进行构建
MAINTAINER #制作者
RUN #执行命令
ADD #添加内容
WORKDIR #镜像工作目录
VOLUME #挂载的目录
EXPOSE #保留的端口配置
CMD #指定容器启动时需要运行的命令,只有最后一个生效
ENTRYPOINT #指定此容器启动时需要运行的命令,可追加
ONBUILD #增加一个触发器,当此镜像作为其他镜像的build基础时触发
COPY #复制文件到镜像中
ENV #设置环境变量
DockerFile的简单使用
新建一个centos容器,测试
root@ggls-server:~# docker run -it centos
[root@e9af84271c14 /]# vim
bash: vim: command not found
发现官方的镜像中并无vim,所以接下来我们要做的就是构建一个自己的带vim功能的centos镜像
编写DockerFile文件,编写完后查看其内容
root@ggls-server:/home/dockerfiles# cat df-for-centos
FROM centos
MAINTAINER ggls<gglsisthebestman@gmail.com>
ENV MYPATH /home/test
WORKDIR $MYPATH
RUN yum -y install vim
RUN echo "install vim test"
CMD echo $MYPATH
CMD echo "---end---"
# run bash shell after container start
CMD /bin/bash
执行docker build
命令
root@ggls-server:/home/dockerfiles# docker build -f df-for-centos -t gglscentos:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/9 : FROM centos
---> 5d0da3dc9764
Step 2/9 : MAINTAINER ggls<gglsisthebestman@gmail.com>
---> Running in f0d9e2ca5ac6
Removing intermediate container f0d9e2ca5ac6
---> 0c4e8536f35d
Step 3/9 : ENV MYPATH /home/test
---> Running in 1edec1f65f2f
Removing intermediate container 1edec1f65f2f
---> 39354c32fe6a
Step 4/9 : WORKDIR $MYPATH
---> Running in 4970653b534c
Removing intermediate container 4970653b534c
---> 51f86866097c
Step 5/9 : RUN yum -y install vim
---> Running in c4523a38541b
CentOS Linux 8 - AppStream 8.0 MB/s | 8.1 MB 00:01
CentOS Linux 8 - BaseOS 1.3 MB/s | 3.5 MB 00:02
CentOS Linux 8 - Extras 17 kB/s | 10 kB 00:00
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 239 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-16.el8.noarch.rp 570 kB/s | 49 kB 00:00
(3/5): vim-enhanced-8.0.1763-16.el8.x86_64.rpm 3.3 MB/s | 1.4 MB 00:00
(4/5): which-2.21-16.el8.x86_64.rpm 197 kB/s | 49 kB 00:00
(5/5): vim-common-8.0.1763-16.el8.x86_64.rpm 12 MB/s | 6.3 MB 00:00
--------------------------------------------------------------------------------
Total 2.4 MB/s | 7.8 MB 00:03
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream 298 kB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:
Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>"
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 c4523a38541b
---> 282c3f60b87f
Step 6/9 : RUN echo "install vim test"
---> Running in ebd3ade2ad58
install vim test
Removing intermediate container ebd3ade2ad58
---> 5716f5cb17ba
Step 7/9 : CMD echo $MYPATH
---> Running in 23dca67ade36
Removing intermediate container 23dca67ade36
---> 3001949b91e0
Step 8/9 : CMD echo "---end---"
---> Running in a0838606b0b1
Removing intermediate container a0838606b0b1
---> 135f12bf436c
Step 9/9 : CMD /bin/bash
---> Running in 5c9757764858
Removing intermediate container 5c9757764858
---> 639972af2847
Successfully built 639972af2847
Successfully tagged gglscentos:1.0
可以看到我们已经成功创建了自己的镜像,接下来说明docker build -f df-for-centos -t gglscentos:1.0 .
的参数
-f 指定DockerFile文件名
-t 指定生成的镜像名和版本号
. 在当前目录下执行
测试一下自己的镜像吧
#不指定版本号无法找到
root@ggls-server:/home/dockerfiles# docker run -it gglscentos
Unable to find image 'gglscentos:latest' locally
^C
root@ggls-server:/home/dockerfiles# docker run -it gglscentos:1.0
#当前目录在我们之前指定的/home/test下
[root@14ab1831e5cb test]# ls
[root@14ab1831e5cb test]# whatis vim
bash: whatis: command not found
#可以发现,vim被成功的安装了
[root@14ab1831e5cb test]# vim abctxt
[root@14ab1831e5cb test]# ls
abctxt
#可以发现gglscentos比centos大得多
root@ggls-server:/home/dockerfiles# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gglscentos 1.0 639972af2847 6 minutes ago 295MB
alpine latest c059bfaa849c 4 days ago 5.59MB
nginx latest ea335eea17ab 12 days ago 141MB
halohub/halo 1.4.13 07dafc8f2711 3 weeks ago 325MB
centos latest 5d0da3dc9764 2 months ago 231MB
portainer/portainer latest 580c0e4e98b0 8 months ago 79.1MB
使用docker history
可以查看所生的image创建过程
root@ggls-server:/home/dockerfiles# docker history gglscentos:1.0
IMAGE CREATED CREATED BY SIZE COMMENT
639972af2847 11 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
135f12bf436c 11 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
3001949b91e0 11 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
5716f5cb17ba 11 minutes ago /bin/sh -c echo "install vim test" 0B
282c3f60b87f 11 minutes ago /bin/sh -c yum -y install vim 63.9MB
51f86866097c 11 minutes ago /bin/sh -c #(nop) WORKDIR /home/test 0B
39354c32fe6a 11 minutes ago /bin/sh -c #(nop) ENV MYPATH=/home/test 0B
0c4e8536f35d 11 minutes ago /bin/sh -c #(nop) MAINTAINER ggls<gglsisthe… 0B
5d0da3dc9764 2 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 2 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 2 months ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0… 231MB
恭喜你,你已经知道如何创建自己的镜像了! :)