点击查看代码
# dockerfile文件
[11-14 15:39:27 huawei root ~/dockerfile] # ls -l
total 8
-rw-r--r-- 1 root root 579 Nov 14 15:25 dockerfile
-rw-r--r-- 1 root root 20 Nov 14 15:26 test_copy.file
[11-14 15:39:37 huawei root ~/dockerfile] # cat dockerfile -n
1 FROM centos:latest
2 MAINTAINER huan
3 EXPOSE 8888
4 VOLUME /test_dockerfile_datavolume
5 RUN touch test_run_command
6 WORKDIR /home
7 RUN touch test_run_command
8 CMD touch test_cmd_command
9 COPY test_copy.file /var/log/test_copy.file
10 ADD https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo /etc/yum.repos.d/test.repo
11 ARG author=huanhuan
12 LABEL mathor="this is a image build from centos " author=$author
13 ENV testuser="test"
14 CMD useradd test
15 USER test
16 CMD touch test_file_user
17 USER root
18 ONBUILD CMD touch test_onbuild_command
19 ENTRYPOINT ["/bin/bash","-c"]
20 CMD tail -F /var/log/aaaa
#构建镜像
[11-14 15:27:35 huawei root ~/dockerfile] # docker build -t "test:dockerfile" .
Sending build context to Docker daemon 3.584 kB
Step 1/20 : FROM centos:latest
---> 5d0da3dc9764
Step 2/20 : MAINTAINER huan
---> Running in 63e438f5166a
---> 32c4be9a9fd8
Removing intermediate container 63e438f5166a
Step 3/20 : EXPOSE 8888
---> Running in dc87e3719a16
---> 5c7730fd7cfd
Removing intermediate container dc87e3719a16
Step 4/20 : VOLUME /test_dockerfile_datavolume
---> Running in 9e17ec2197b7
---> f2ebf930107b
Removing intermediate container 9e17ec2197b7
Step 5/20 : RUN touch test_run_command
---> Running in eefedf0ed2e2
---> c2ffd2228bab
Removing intermediate container eefedf0ed2e2
Step 6/20 : WORKDIR /home
---> 749b0d41cbcc
Removing intermediate container 7758e784a08a
Step 7/20 : RUN touch test_run_command
---> Running in 51be6bbd145b
---> 3a1fbbde10e9
Removing intermediate container 51be6bbd145b
Step 8/20 : CMD touch test_cmd_command
---> Running in cbc4fc33ebb3
---> 3558407cd1d7
Removing intermediate container cbc4fc33ebb3
Step 9/20 : COPY test_copy.file /var/log/test_copy.file
---> 3228664373fb
Removing intermediate container eb4ff5609d6e
Step 10/20 : ADD https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo /etc/yum.repos.d/test.repo
Downloading [==================================================>] 2.495 kB/2.495 kB
---> d9febacae0b5
Removing intermediate container eae94ab84663
Step 11/20 : ARG author=huanhuan
---> Running in a9c1daca30a3
---> 0d29df131ab3
Removing intermediate container a9c1daca30a3
Step 12/20 : LABEL mathor "this is a image build from centos " author $author
---> Running in 50bc1aedf70e
---> 810f42a8b862
Removing intermediate container 50bc1aedf70e
Step 13/20 : ENV testuser "test"
---> Running in 3bd4d21aa3e6
---> d19d5520022c
Removing intermediate container 3bd4d21aa3e6
Step 14/20 : CMD useradd $testuser
---> Running in 375b79119b9a
---> 733c1deb294e
Removing intermediate container 375b79119b9a
Step 15/20 : USER $testuser
---> Running in 2d81f0273840
---> 769b1ba02426
Removing intermediate container 2d81f0273840
Step 16/20 : CMD touch test_file_user
---> Running in f8ba03aa4f9d
---> f173e21b2a82
Removing intermediate container f8ba03aa4f9d
Step 17/20 : USER root
---> Running in 851381e61a53
---> 579755bf00c2
Removing intermediate container 851381e61a53
Step 18/20 : ONBUILD CMD touch
---> Running in 828d41facfb7
---> 85d8171fc6be
Removing intermediate container 828d41facfb7
Step 19/20 : ENTRYPOINT /bin/bash -c
---> Running in 6618388998d5
---> ff0e3337b2bb
Removing intermediate container 6618388998d5
Step 20/20 : CMD tail -F /var/log/aaaa
---> Running in 0434c3fbf757
---> c5d4667d2e8a
Removing intermediate container 0434c3fbf757
Successfully built c5d4667d2e8a
You have mail in /var/spool/mail/root
# 查看构建的镜像
[11-14 16:17:22 huawei root ~/dockerfile] # docker images | grep dockerfile
test dockerfile c5d4667d2e8a 49 minutes ago 231 MB
[11-14 16:18:55 huawei root ~/dockerfile] # docker image inspect test:dockerfile
[
{
"Id": "sha256:c5d4667d2e8ae110d4a3d2376af548e0ec6373cbd0b67fbc975df31c1929fe2b",
"RepoTags": [
"test:dockerfile"
],
"RepoDigests": [],
"Parent": "sha256:ff0e3337b2bb0c4598699b124429da857b264a5cba5b6c5fddde0dafe6a2b0d7",
"Comment": "",
"Created": "2022-11-14T07:27:49.566318799Z",
"Container": "0434c3fbf7576343367c0596a27df517441f542d07aba338ba2b8eb530c2473a",
"ContainerConfig": {
"Hostname": "63e438f5166a",
"Domainname": "",
"User": "root",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"8888/tcp": {} # expose 端口
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"testuser=test" # env变量
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"/bin/sh\" \"-c\" \"tail -F /var/log/aaaa\"]” # CMD
],
"ArgsEscaped": true,
"Image": "sha256:ff0e3337b2bb0c4598699b124429da857b264a5cba5b6c5fddde0dafe6a2b0d7",
"Volumes": {
"/test_dockerfile_datavolume": {} # volume
},
"WorkingDir": "/home", # workdir
"Entrypoint": [
"/bin/bash", # unbuild
"-c"
],
"OnBuild": [
“CMD touch test_onbuild_command" # onbiuld
],
"Labels": {
"author": "huanhuan”, # label 调用arg
"mathor": "this is a image build from centos “, # label
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"DockerVersion": "1.13.1",
"Author": "huan",
"Config": {
"Hostname": "63e438f5166a",
"Domainname": "",
"User": "root",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"8888/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"testuser=test"
],
"Cmd": [
"/bin/sh",
"-c",
"tail -F /var/log/aaaa"
],
"ArgsEscaped": true,
"Image": "sha256:ff0e3337b2bb0c4598699b124429da857b264a5cba5b6c5fddde0dafe6a2b0d7",
"Volumes": {
"/test_dockerfile_datavolume": {}
},
"WorkingDir": "/home",
"Entrypoint": [
"/bin/bash",
"-c"
],
"OnBuild": [
“CMD touch test_onbuild_command"
],
"Labels": {
"author": "huanhuan”, # 调用arg变量;
"mathor": "this is a image build from centos “, # 这里与dockerfile中的label对应
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 231271371,
"VirtualSize": 231271371,
"GraphDriver": {
"Name": "overlay2",
"Data": {
"LowerDir": "/var/lib/docker/overlay2/843dbc77ffc47a10df520298667faa2eab210100602c0cd7f46d9b673c99855b/diff:/var/lib/docker/overlay2/fc11cc620c56294dde9f0bfee0cfcff75230ccdb6c365ede8ee7ef59c0fa1adb/diff:/var/lib/docker/overlay2/42663774aaa795fec9cf587674ac69ec656581a0793266619e27e9c046ab7f26/diff:/var/lib/docker/overlay2/e0631b205c685cae1fb63c2846c402151ed2180e1a892b4d3e7a132b113f5a96/diff",
"MergedDir": "/var/lib/docker/overlay2/455ee60d8346df080a5e27938f7dac2c4852c54cac1ebf67ca94de3f3a422569/merged",
"UpperDir": "/var/lib/docker/overlay2/455ee60d8346df080a5e27938f7dac2c4852c54cac1ebf67ca94de3f3a422569/diff",
"WorkDir": "/var/lib/docker/overlay2/455ee60d8346df080a5e27938f7dac2c4852c54cac1ebf67ca94de3f3a422569/work"
}
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59",
"sha256:b5b47092ccb398a894e968cd669a810a5a2de62a12fb87f34432a91764729b47",
"sha256:9c94c178db9516b69960d4d0c6fdc33a3a38c9f2652f5f9b16909944d2000d69",
"sha256:8be7ceabf607fc7aca16766ea314167e20f8753eb2cbf959992572215b6b71e8",
"sha256:092fc5bdc8fa52c38a1e1957a3169a2d1e8b1fe47c409d851f1eb11f762622b1"
]
}
}
]
[11-14 16:19:10 huawei root ~/dockerfile] #
# 以这个镜像运行一个容器
[11-14 16:53:14 huawei root ~/dockerfile] # docker run --name testdockerfile_2 -it -p 8888 test:dockerfile /bin/bash
[root@53f420731914 home]#
# 第4步,新增的挂载点
[root@53f420731914 ~]# ls -l /test_dockerfile_datavolume/
total 0
[root@53f420731914 ~]# df -Th | grep test
/dev/vda1 ext4 40G 14G 24G 37% /test_dockerfile_datavolume
# 第5步,新增的文件,默认的工作目录是/
[root@53f420731914 ~]# ls -l /test_run_command
-rw-r--r-- 1 root root 0 Nov 14 07:27 /test_run_command
# 第6步,进行workdir切换,登录以后默认是root用户,工作目录与workdir对应为/home
[root@53f420731914 home]# whoami
root
[root@53f420731914 home]# pwd
/home
# 第7步,在workdir创建的文件
[root@53f420731914 home]#
[root@53f420731914 home]# ls -l
total 0
-rw-r--r-- 1 root root 0 Nov 14 07:27 test_run_command
# 第8步,因为CMD要被覆盖,所以并没有创建文件
[root@53f420731914 ~]# ls -l /home/
total 0
-rw-r--r-- 1 root root 0 Nov 14 07:27 test_run_command
# 第9步,中将dockerfile同目录下的文件拷问到/var/log
[root@53f420731914 ~]# ls -l /var/log/test_copy.file
-rw-r--r-- 1 root root 20 Nov 14 07:26 /var/log/test_copy.file
# 第10步,ADD可以从远端拷贝文件到镜像中
[root@53f420731914 ~]# ls -l /etc/yum.repos.d/test.repo
-rw------- 1 root root 2495 Aug 4 07:04 /etc/yum.repos.d/test.repo
[root@53f420731914 ~]# cat /etc/yum.repos.d/test.repo | wc -
56 149 2495 -
# 第11步,ARG仅对build生效,run的时候没有这个变量
[root@53f420731914 ~]# env | grep autho
# 第12步,label的信息对镜像起作用,可以通过docker image inspect查看
[11-14 17:12:43 huawei root ~/dockerfile] # docker inspect test:dockerfile | grep -A 4 Labels
"Labels": {
"author": "huanhuan",
"mathor": "this is a image build from centos ",
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
# 第13步,配置ENV变量
[root@53f420731914 home]# env | grep user
testuser=test
# 第14步,查看新建的用户,因为CMD命令会被覆盖,所以14\15\16\17都会执行异常
# 对14-17进行了修改,使用RUN 替代 CMD
14 RUN useradd $testuser
15 USER $testuser
16 WORKDIR /home/$testuser # 解决问题,test没法在/home创建文件
17 RUN touch test_file_user
18 USER root
# 这样就会多出test用户
[root@53f420731914 home]# id test
uid=1000(test) gid=1000(test) groups=1000(test)
# 用户的切换和文件创建都可以正常下发
[root@b3bd981c2b8f test]# ls -l
total 0
-rw-r--r-- 1 test test 0 Nov 14 16:15 test_file_user
-rw-r--r-- 1 root root 0 Nov 14 16:15 test_file_user_2
# 第18步,以test:dockerfile_2这个镜像再次作为FROM创建镜像时,onbuild命令将执行
# 这个地方我反复测试了很多次,真正体会到了CMD的特点,他真的会被覆盖,哪怕是在ONBUILD后面也要被覆盖,所以后面我修改了onbuild的命令为:
ONBUILD ADD https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo ./test_onbuild_command
# 然后再次封装以后,测试就正常了。
[11-14 23:41:46 huawei root ~/dockerfile] # cat Dockerfile
FROM test:dockerfile_2
MAINTAINER huan
ENTRYPOINT tail -F /var/log/sssss
[11-14 23:54:37 huawei root ~/dockerfile] # docker build -t "test:onbuild" .
Sending build context to Docker daemon 6.144 kB
Step 1/3 : FROM test:dockerfile_2
# Executing 1 build trigger...
Step 1/1 : ADD https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo ./test_onbuild_command
Downloading [==================================================>] 2.495 kB/2.495 kB
---> 0e853ee58e88
Removing intermediate container acdb4b01ff99
Step 2/3 : MAINTAINER huan
---> Running in 239002c5c267
---> a5890608d479
Removing intermediate container 239002c5c267
Step 3/3 : ENTRYPOINT tail -F /var/log/sssss
---> Running in 4f8e71febf5d
---> d27c66d32037
Removing intermediate container 4f8e71febf5d
Successfully built d27c66d32037
[11-14 23:54:46 huawei root ~/dockerfile] # docker run -itd test:onbuild bash
92cf5fb9bf7fab200beaaf5ec7d70a54320d738c0c32a542db261dcbe9658f51
[11-14 23:54:55 huawei root ~/dockerfile] # docker exec -it 92cf5fb9bf7fab200beaaf5ec7d70a54320d738c0c32a542db261dcbe9658f51 bash
[root@92cf5fb9bf7f home]# ls
test_onbuild_command test_run_command_2