DockerFile
DockerFile介绍
dockerfile 是用来构建docker镜像的文件!命令参数脚本!
构建步骤:
1、编写一个docker文件
2、docker build 构建成为一个镜像
3、docker run 运行镜像
4、docker push 发布镜像 (DockerHub,阿里云镜像仓库!)
官方的centos7.0
很多官方镜像都是基础包,很多功能没有,我们通常会自己搭建自己的镜像!
DockerFile构建过程
基础知识:
1、每个保留关键字(指令)都必须是大写字母
2、指令从上到下顺序执行
3、# 表示注释
4、每一个指令都会创建提交一个新的镜像层,并提交!
步骤:开发,部署,运维
DockerFile: 构建文件,定义了一切的步骤,源代码
DockerImages : 通过DockerFile构建生成的镜像,最终发布和运行的产品!
Docker容器 : 容器就是镜像运行起来提供服务
DockerFile的指令
常用指令
FROM # 基础镜像,一切从这里开始构建
MANTAINER # 镜像是谁写的,姓名+邮箱
RUN # 镜像构建的时候需要运行的命令
ADD # 添加内容,如添加一个tomcat的压缩包
WORKDIR # 镜像的工作目录
VOLUME # 挂载的目录
EXPOE # 暴露端口的配置
CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代!
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令!
ONBUILD # 当构建一个被继承 DockerFile 这个时候就会运行 ONBUILD 的指令,触发指令!
COPY # 类似ADD ,将文件拷贝到镜像中!
ENV # 构建的时候设置环境变量!
实战测试
Docker Hub中 99% 的镜像都是从 FROM scratch 这个基础镜像过来的!然后配置需要的软件和配置来进行构建 !
创建一个自己的centos
在home目录下创建一个文件夹
[root@iz2zeaet7s13lfkc8r3e2kz home]# mkdir dockerfile
[root@iz2zeaet7s13lfkc8r3e2kz home]# ls
ceshi dockerfile docker-test-volume mysql www ysl
在创建的文件夹中创建dockerfile文件,并编写
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# vim mydockerfile-centos
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# cat mydockerfile-centos
FROM centos
MAINTAINER ysl<493487822@qq.com>
WORKDIR /usr/local
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo "---end---"
CMD /bin/bash
通过这个文件构建镜像
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# docker build -f mydockerfile-centos -t mycentos:0.1 .
Sending build context to Docker daemon 2.048kB
Step 1/8 : FROM centos
---> 470671670cac
Step 2/8 : MAINTAINER ysl<493487822@qq.com>
---> Running in 2ce4c938a188
Removing intermediate container 2ce4c938a188
---> fd5e9920907d
Step 3/8 : WORKDIR /usr/local
---> Running in 98327d3050d5
Removing intermediate container 98327d3050d5
---> 167672c4b746
Step 4/8 : RUN yum -y install vim
---> Running in 7db13d6a1a95
CentOS-8 - AppStream 1.9 MB/s | 7.0 MB 00:03
CentOS-8 - Base 1.1 MB/s | 2.2 MB 00:02
CentOS-8 - Extras 7.1 kB/s | 5.9 kB 00:00
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-13.el8 AppStream 1.4 M
Installing dependencies:
gpm-libs x86_64 1.20.7-15.el8 AppStream 39 k
vim-common x86_64 2:8.0.1763-13.el8 AppStream 6.3 M
vim-filesystem noarch 2:8.0.1763-13.el8 AppStream 48 k
which x86_64 2.21-10.el8 BaseOS 49 k
Transaction Summary
================================================================================
Install 5 Packages
Total download size: 7.8 M
Installed size: 31 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm 285 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 813 kB/s | 48 kB 00:00
(3/5): which-2.21-10.el8.x86_64.rpm 294 kB/s | 49 kB 00:00
(4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 1.6 MB/s | 1.4 MB 00:00
(5/5): vim-common-8.0.1763-13.el8.x86_64.rpm 2.1 MB/s | 6.3 MB 00:03
--------------------------------------------------------------------------------
Total 1.3 MB/s | 7.8 MB 00:05
warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - AppStream 1.6 MB/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-10.el8.x86_64 1/5
Installing : vim-filesystem-2:8.0.1763-13.el8.noarch 2/5
Installing : vim-common-2:8.0.1763-13.el8.x86_64 3/5
Installing : gpm-libs-1.20.7-15.el8.x86_64 4/5
Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64 4/5
Installing : vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5
Running scriptlet: vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5
Running scriptlet: vim-common-2:8.0.1763-13.el8.x86_64 5/5
Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5
Verifying : vim-common-2:8.0.1763-13.el8.x86_64 2/5
Verifying : vim-enhanced-2:8.0.1763-13.el8.x86_64 3/5
Verifying : vim-filesystem-2:8.0.1763-13.el8.noarch 4/5
Verifying : which-2.21-10.el8.x86_64 5/5
Installed:
vim-enhanced-2:8.0.1763-13.el8.x86_64 gpm-libs-1.20.7-15.el8.x86_64
vim-common-2:8.0.1763-13.el8.x86_64 vim-filesystem-2:8.0.1763-13.el8.noarch
which-2.21-10.el8.x86_64
Complete!
Removing intermediate container 7db13d6a1a95
---> 46d4742d96c6
Step 5/8 : RUN yum -y install net-tools
---> Running in 3a8b3b61455f
Last metadata expiration check: 0:00:14 ago on Tue May 26 02:28:01 2020.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
net-tools x86_64 2.0-0.51.20160912git.el8 BaseOS 323 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 323 k
Installed size: 1.0 M
Downloading Packages:
net-tools-2.0-0.51.20160912git.el8.x86_64.rpm 2.0 MB/s | 323 kB 00:00
--------------------------------------------------------------------------------
Total 211 kB/s | 323 kB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : net-tools-2.0-0.51.20160912git.el8.x86_64 1/1
Running scriptlet: net-tools-2.0-0.51.20160912git.el8.x86_64 1/1
Verifying : net-tools-2.0-0.51.20160912git.el8.x86_64 1/1
Installed:
net-tools-2.0-0.51.20160912git.el8.x86_64
Complete!
Removing intermediate container 3a8b3b61455f
---> 0a0a8b5370f2
Step 6/8 : EXPOSE 80
---> Running in e136862cbcf1
Removing intermediate container e136862cbcf1
---> 28b29770c09a
Step 7/8 : CMD echo "---end---"
---> Running in db998e281478
Removing intermediate container db998e281478
---> e7090ba90a77
Step 8/8 : CMD /bin/bash
---> Running in 291bec834681
Removing intermediate container 291bec834681
---> a68a7a44878e
Successfully built a68a7a44878e
Successfully tagged mycentos:0.1
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 a68a7a44878e About a minute ago 321MB
ysl/centos 1.0 bf97aef123f3 23 hours ago 237MB
mysql 5.7 a4fdfd462add 4 days ago 448MB
nginx latest 9beeba249f3e 10 days ago 127MB
centos latest 470671670cac 4 months ago 237MB
运行
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# docker run -it mycentos:0.1
[root@49326d559289 local]# pwd
/usr/local
官方的工作目录是 / ,不能使用vim,和ifconfig等命令!我自己的镜像安装了vim,和ifconfig!测试能否使用
测试官方的centos
列出本地进行的变更历史
docker history 镜像id
CMD 和 ENTRYPOINT 的区别
CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代!
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令!
测试CMD
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# vim dockerfile-cmd-test
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# cat dockerfile-cmd-test
FROM centos
CMD ["ls","-a"]
# 构建
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# docker build -f dockerfile-cmd-test -t cmdtest .
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM centos
---> 470671670cac
Step 2/2 : CMD ["ls","-a"]
---> Running in 08576b2fe689
Removing intermediate container 08576b2fe689
---> 380b0c95615c
Successfully built 380b0c95615c
Successfully tagged cmdtest:latest
# 执行
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# docker run 380b0c95615c
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
# 想追加一个命令 -l 期望的是 ls -al
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# docker run 380b0c95615c -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"-l\": executable file not found in $PATH": unknown.
# CMD的情况下 -l 替换了 CMD ["ls","-a"] 命令, -l 不是一个命令所以报错
# CMD正确应该
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# docker run 380b0c95615c ls -al
total 56
drwxr-xr-x 1 root root 4096 May 26 03:04 .
drwxr-xr-x 1 root root 4096 May 26 03:04 ..
-rwxr-xr-x 1 root root 0 May 26 03:04 .dockerenv
lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin
drwxr-xr-x 5 root root 340 May 26 03:04 dev
drwxr-xr-x 1 root root 4096 May 26 03:04 etc
drwxr-xr-x 2 root root 4096 May 11 2019 home
lrwxrwxrwx 1 root root 7 May 11 2019 lib -> usr/lib
lrwxrwxrwx 1 root root 9 May 11 2019 lib64 -> usr/lib64
drwx------ 2 root root 4096 Jan 13 21:48 lost+found
drwxr-xr-x 2 root root 4096 May 11 2019 media
drwxr-xr-x 2 root root 4096 May 11 2019 mnt
drwxr-xr-x 2 root root 4096 May 11 2019 opt
dr-xr-xr-x 111 root root 0 May 26 03:04 proc
dr-xr-x--- 2 root root 4096 Jan 13 21:49 root
drwxr-xr-x 11 root root 4096 Jan 13 21:49 run
lrwxrwxrwx 1 root root 8 May 11 2019 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 May 11 2019 srv
dr-xr-xr-x 13 root root 0 May 9 09:17 sys
drwxrwxrwt 7 root root 4096 Jan 13 21:49 tmp
drwxr-xr-x 12 root root 4096 Jan 13 21:49 usr
drwxr-xr-x 20 root root 4096 Jan 13 21:49 var
测试ENTRYPOINT
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# vim dockerfile-entrypoint-test
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# cat dockerfile-entrypoint-test
FROM centos
ENTRYPOINT ["ls","-a"]
#构建
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# docker build -f dockerfile-entrypoint-test -t entrypoint-test .
Sending build context to Docker daemon 4.096kB
Step 1/2 : FROM centos
---> 470671670cac
Step 2/2 : ENTRYPOINT ["ls","-a"]
---> Running in bf9f861823b8
Removing intermediate container bf9f861823b8
---> cd452c66c026
Successfully built cd452c66c026
Successfully tagged entrypoint-test:latest
# 执行
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# docker run cd452c66c026
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
# 追加一个命令 -l
[root@iz2zeaet7s13lfkc8r3e2kz dockerfile]# docker run cd452c66c026 -l
total 56
drwxr-xr-x 1 root root 4096 May 26 03:12 .
drwxr-xr-x 1 root root 4096 May 26 03:12 ..
-rwxr-xr-x 1 root root 0 May 26 03:12 .dockerenv
lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin
drwxr-xr-x 5 root root 340 May 26 03:12 dev
drwxr-xr-x 1 root root 4096 May 26 03:12 etc
drwxr-xr-x 2 root root 4096 May 11 2019 home
lrwxrwxrwx 1 root root 7 May 11 2019 lib -> usr/lib
lrwxrwxrwx 1 root root 9 May 11 2019 lib64 -> usr/lib64
drwx------ 2 root root 4096 Jan 13 21:48 lost+found
drwxr-xr-x 2 root root 4096 May 11 2019 media
drwxr-xr-x 2 root root 4096 May 11 2019 mnt
drwxr-xr-x 2 root root 4096 May 11 2019 opt
dr-xr-xr-x 112 root root 0 May 26 03:12 proc
dr-xr-x--- 2 root root 4096 Jan 13 21:49 root
drwxr-xr-x 11 root root 4096 Jan 13 21:49 run
lrwxrwxrwx 1 root root 8 May 11 2019 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 May 11 2019 srv
dr-xr-xr-x 13 root root 0 May 9 09:17 sys
drwxrwxrwt 7 root root 4096 Jan 13 21:49 tmp
drwxr-xr-x 12 root root 4096 Jan 13 21:49 usr
drwxr-xr-x 20 root root 4096 Jan 13 21:49 var