Dockerfile
Dockerfile
简介
Dockerfile 是用来构建 Docker 镜像的文件,可以理解为命令参数脚本 。
Dockerfile 是面向开发的,想要打包项目,就要编写 Dockerfile 文件。
命令
以上面的 centos 官方镜像的 Dockerfile 为例。
1 2 3 4 5 6 7 8 FROM scratch ADD centos-7-docker .tar.xz / LABEL org.label-schema .schema-version ="1.0" \ org.label-schema .name="CentOS Base Image" \ org.label-schema .vendor="CentOS" \ org.label-schema .license="GPLv2" \ org.label-schema .build-date ="20181204" CMD ["/bin/bash" ]
Docker Hub 中 99% 的镜像都是从FROM scratch
开始的。
规则
每个指令都必须是大写字母。
按照从上到下顺序执行。
# 表示注释。
每一条指令都会创建一个新的镜像层。
解释
FROM
:基础镜像,比如 centos。
MAINTAINER
:镜像是谁写的。建议以此格式:姓名<邮箱>
。
RUN
:镜像构建时需要运行的命令。
ADD
:添加,比如添加一个 tomcat 压缩包。
WORKDIR
:镜像的工作目录。
VOLUME
:挂载的目录。
EXPOSE
:指定暴露端口,跟 -p 一个道理。
RUN
:最终要运行的。
CMD
:指定这个容器启动的时候要运行的命令,只有最后一个会生效,而且可被替代。
ENTRYPOINT
:指定这个容器启动的时候要运行的命令,可以追加命令。
ONBUILD
:当构建一个被继承Dockerfile 这个时候运行ONBUILD指定,触发指令。
COPY
:将文件拷贝到镜像中。
ENV
:构建的时候设置环境变量。
构建镜像
docker build
Dockerfile 编写好后,需要使用 docker build
命令运行。
语法
1 docker build [参数] 路径 | 网络地址 | -
参数
-f
:指定要使用的Dockerfile路径。
-t
:镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
-m
:设置内存最大值。
Docker 守护进程执行 Dockerfile 中的指令前,首先会对 Dockerfile 进行语法检查,有语法错误时会返回报错信息。
查看构建记录
docker history
语法
CMD 与 ENTRYPOINT 区别
CMD 命令演示
编写 Dockerfile
1 2 3 4 [root @fantasy dockerfile ] [root @fantasy dockerfile ] FROM centos CMD ["ls" ,"-a" ]
构建镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root @fantasy dockerfile ] Sending build context to Docker daemon 2.048 kB Step 1 /2 : FROM centos --- > 5 d0da3dc9764 Step 2 /2 : CMD ["ls" ,"-a" ] --- > Running in 0 a743e929fff Removing intermediate container 0 a743e929fff --- > 1683 c0790d49 Successfully built 1683 c0790d49 Successfully tagged zmtest:latest [root @fantasy dockerfile ] REPOSITORY TAG IMAGE ID CREATED SIZE zmtest latest 1683 c0790d49 13 minutes ago 231 MB
运行镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [root @fantasy dockerfile ] . .. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
此时 Dockerfile 中编写的命令生效了。
追加 -l 命令
1 2 3 [root @fantasy dockerfile ] docker: Error response from daemon: OCI runtime create failed: container_linux.go:380 : starting container process caused: exec: "-l" : executable file not found in $PATH: unknown. ERRO[0000 ] error waiting for container: context canceled
没有达到预期的 ls -al
命令。
CMD 是替换的方式, -l 不是命令,所以报错。
ENTRYPOINT 命令演示
编写 Dockerfile
1 2 3 4 [root @fantasy dockerfile ] [root @fantasy dockerfile ] FROM centos ENTRYPOINT ["ls" ,"-a" ]
构建镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root @fantasy dockerfile ] Sending build context to Docker daemon 3.072 kB Step 1 /2 : FROM centos --- > 5 d0da3dc9764 Step 2 /2 : ENTRYPOINT ["ls" ,"-a" ] --- > Running in a02d55ae0a00 Removing intermediate container a02d55ae0a00 --- > 795973 a0ed43 Successfully built 795973 a0ed43 Successfully tagged ent-test :latest [root @fantasy dockerfile ] REPOSITORY TAG IMAGE ID CREATED SIZE ent-test latest 795973 a0ed43 22 seconds ago 231 MB
运行镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [root @fantasy dockerfile ] . .. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
此时 Dockerfile 中编写的命令也生效了。
追加 -l 命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [root @fantasy dockerfile ] total 56 drwxr-xr-x 1 root root 4096 Mar 27 10 :26 . drwxr-xr-x 1 root root 4096 Mar 27 10 :26 .. -rwxr-xr-x 1 root root 0 Mar 27 10 :26 .dockerenvlrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x 5 root root 340 Mar 27 10 :26 dev drwxr-xr-x 1 root root 4096 Mar 27 10 :26 etc drwxr-xr-x 2 root root 4096 Nov 3 2020 home lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64 drwx------ 2 root root 4096 Sep 15 2021 lost+found drwxr-xr-x 2 root root 4096 Nov 3 2020 media drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt drwxr-xr-x 2 root root 4096 Nov 3 2020 opt dr-xr-xr-x 352 root root 0 Mar 27 10 :26 proc dr-xr-x--- 2 root root 4096 Sep 15 2021 root drwxr-xr-x 11 root root 4096 Sep 15 2021 run lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 Nov 3 2020 srv dr-xr-xr-x 11 root root 0 Mar 27 10 :26 sys drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp drwxr-xr-x 12 root root 4096 Sep 15 2021 usr drwxr-xr-x 20 root root 4096 Sep 15 2021 var
运行了预期的 ls -al
命令。
ENTRYPOINT 是追加的方式。
Docker 中许多命令都十分相似,我们需要了解他们的区别,最好的方式就是这样对比测试。
实战
创建包含vim命令的centos镜像
编写 Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 [root @fantasy dockerfile ] [root @fantasy dockerfile ] FROM centos MAINTAINER fantasy<xxxxx@163 .com> ENV MYPATH /usr/local WORKDIR $MYPATH RUN yum -y install vim RUN yum -y install net-tools EXPOSE 81 CMD echo $MYPATH CMD echo "---end---" CMD ["/bin/bash" ]
构建镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 [root @fantasy dockerfile ] Sending build context to Docker daemon 5.632 kB Step 1 /10 : FROM centos --- > 5 d0da3dc9764 Step 2 /10 : MAINTAINER fantasy<xxxxxx@163 .com> --- > Running in 8 b7340768878 Removing intermediate container 8 b7340768878 --- > 9616888 f3b10 Step 3 /10 : ENV MYPATH /usr/local --- > Running in 2 c73446a56ff Removing intermediate container 2 c73446a56ff --- > be89377d4c2c Step 4 /10 : WORKDIR $MYPATH --- > Running in db113c4f7cb2 Removing intermediate container db113c4f7cb2 --- > fb41ece5d944 Step 5 /10 : RUN yum -y install vim --- > Running in eccee60c0389 CentOS Linux 8 - AppStream 12 MB/s | 8.4 MB 00 :00 CentOS Linux 8 - BaseOS 1.7 MB/s | 3.6 MB 00 :02 CentOS Linux 8 - Extras 17 kB/s | 10 kB 00 :00 Last metadata expiration check: 0 :00 :01 ago on Sat Dec 25 05 :09 :40 2021 . 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 582 kB/s | 39 kB 00 :00 (2 /5 ): vim-filesystem-8 .0.1763 -16 .el8.noarch.rp 1.2 MB/s | 49 kB 00 :00 (3 /5 ): vim-common-8 .0.1763 -16 .el8.x86_64.rpm 40 MB/s | 6.3 MB 00 :00 (4 /5 ): vim-enhanced-8 .0.1763 -16 .el8.x86_64.rpm 7.1 MB/s | 1.4 MB 00 :00 (5 /5 ): which-2 .21 -16 .el8.x86_64.rpm 252 kB/s | 49 kB 00 :00 -------------------------------------------------------------------------------- Total 6.4 MB/s | 7.8 MB 00 :01 warning: /var/cache/dnf/appstream-02e86d1c976ab532 /packages/gpm-libs-1 .20.7 -17 .el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483 c65d: NOKEY CentOS Linux 8 - AppStream 1.6 MB/s | 1.6 kB 00 :00 Importing GPG key 0 x8483C65D: Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>" Fingerprint: 99 DB 70 FA E1D7 CE22 7 FB6 4882 05 B5 55 B3 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 eccee60c0389 --- > 9 f54f48660ac Step 6 /10 : RUN yum -y install net-tools --- > Running in 6 caa7361b001 Last metadata expiration check: 0 :00 :08 ago on Sat Dec 25 05 :09 :40 2021 . Dependencies resolved. ================================================================================ Package Architecture Version Repository Size ================================================================================ Installing: net-tools x86_64 2.0 -0 .52.20160912 git.el8 baseos 322 k Transaction Summary ================================================================================ Install 1 Package Total download size: 322 k Installed size: 942 k Downloading Packages: net-tools-2 .0 -0 .52.20160912 git.el8.x86_64.rpm 1.0 MB/s | 322 kB 00 :00 -------------------------------------------------------------------------------- Total 449 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.20160912 git.el8.x86_64 1 /1 Running scriptlet: net-tools-2 .0 -0 .52.20160912 git.el8.x86_64 1 /1 Verifying : net-tools-2 .0 -0 .52.20160912 git.el8.x86_64 1 /1 Installed: net-tools-2 .0 -0 .52.20160912 git.el8.x86_64 Complete! Removing intermediate container 6 caa7361b001 --- > a9431f90fd3f Step 7 /10 : EXPOSE 81 --- > Running in ad67fa23940a Removing intermediate container ad67fa23940a --- > b5bd21416741 Step 8 /10 : CMD echo $MYPATH --- > Running in fb1d08538689 Removing intermediate container fb1d08538689 --- > 5 c5def0bbb85 Step 9 /10 : CMD echo "---end---" --- > Running in a9d955b6b389 Removing intermediate container a9d955b6b389 --- > ad95558eb658 Step 10 /10 : CMD ["/bin/bash" ] --- > Running in 190651202 e7b Removing intermediate container 190651202 e7b --- > 7 d202bdf002b Successfully built 7 d202bdf002b Successfully tagged centos-my :latest
查看构建的镜像
1 2 3 [root @fantasy dockerfile ] REPOSITORY TAG IMAGE ID CREATED SIZE centos-my latest 7 d202bdf002b About a minute ago 323 MB
查看本地镜像的构建记录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root @fantasy dockerfile ] IMAGE CREATED CREATED BY SIZE COMMENT 7 d202bdf002b 5 minutes ago /bin/sh -c ad95558eb658 5 minutes ago /bin/sh -c 5 c5def0bbb85 5 minutes ago /bin/sh -c b5bd21416741 5 minutes ago /bin/sh -c a9431f90fd3f 5 minutes ago /bin/sh -c yum -y install net-tools 27.3 MB 9 f54f48660ac 5 minutes ago /bin/sh -c yum -y install vim 64.8 MB fb41ece5d944 5 minutes ago /bin/sh -c be89377d4c2c 5 minutes ago /bin/sh -c 9616888 f3b10 5 minutes ago /bin/sh -c 5 d0da3dc9764 3 months ago /bin/sh -c <missing> 3 months ago /bin/sh -c <missing> 3 months ago /bin/sh -c
运行测试
1 2 3 4 5 [root @fantasy ~] [root @530551 bc2162 local ] /usr/local [root @530551 bc2162 local ] [root @530551 bc2162 local ]
默认的工作目录正是 Dockerfile 中设置的 /usr/local
,且可以使用 vim
命令了。
自定义tomcat环境镜像
编写 Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 FROM centos MAINTAINER fantasy<xxxxxx@163 .com> COPY readme.txt /usr/local/readme.txtENV MYPATH /usr/local/ WORKDIR $MYPATH ADD jdk-8u301-linux-x64 .tar.gz $MYPATH ADD apache-tomcat-9 .0.55 .tar.gz $MYPATH RUN yum -y install vim ENV JAVA_HOME $MYPATH /jdk1.8.0 _301-amd64 ENV CLASSPATH $JAVA_HOME /lib/ ENV CATALINA_HOME $MYPATH /apache-tomcat-9 .0.55 ENV CATALINA_BASH $MYPATH /apache-tomcat-9 .0.55 ENV PATH $PATH: $JAVA_HOME /bin:$CATALINA_HOME /bin:$CATALINA_HOME /lib EXPOSE 8080 CMD $CATALINA_HOME /bin/startup.sh && tail -F $CATALINA_HOME /logs/catalina.out
其中的 readme.txt 一般作为镜像说明文件,可以在里面编写镜像的信息。
构建镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 [root @fantasy tomcat ] Sending build context to Docker daemon 157.1 MB Step 1 /15 : FROM centos --- > 5 d0da3dc9764 Step 2 /15 : MAINTAINER fantasy<xxxxxx@163 .com> --- > Using cache --- > 9616888 f3b10 Step 3 /15 : COPY readme.txt /usr/local/readme.txt --- > da792df641f8 Step 4 /15 : ENV MYPATH /usr/local/ --- > Running in e4a5b13decd7 Removing intermediate container e4a5b13decd7 --- > 7 b1e6970b4b3 Step 5 /15 : WORKDIR $MYPATH --- > Running in 835 dabd080dd Removing intermediate container 835 dabd080dd --- > 7 be17b1556ee Step 6 /15 : ADD jdk-8u301-linux-x64 .tar.gz $MYPATH --- > 480721043 fda Step 7 /15 : ADD apache-tomcat-9 .0.55 .tar.gz $MYPATH --- > c7bfa13bfcd1 Step 8 /15 : RUN yum -y install vim --- > Running in 85532523 d784 CentOS Linux 8 - AppStream 9.0 MB/s | 8.4 MB 00 :00 CentOS Linux 8 - BaseOS 5.6 MB/s | 3.6 MB 00 :00 CentOS Linux 8 - Extras 20 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 973 kB/s | 39 kB 00 :00 (2 /5 ): vim-filesystem-8 .0.1763 -16 .el8.noarch.rp 726 kB/s | 49 kB 00 :00 (3 /5 ): vim-enhanced-8 .0.1763 -16 .el8.x86_64.rpm 10 MB/s | 1.4 MB 00 :00 (4 /5 ): which-2 .21 -16 .el8.x86_64.rpm 901 kB/s | 49 kB 00 :00 (5 /5 ): vim-common-8 .0.1763 -16 .el8.x86_64.rpm 27 MB/s | 6.3 MB 00 :00 -------------------------------------------------------------------------------- Total 6.6 MB/s | 7.8 MB 00 :01 warning: /var/cache/dnf/appstream-02e86d1c976ab532 /packages/gpm-libs-1 .20.7 -17 .el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483 c65d: NOKEY CentOS Linux 8 - AppStream 1.6 MB/s | 1.6 kB 00 :00 Importing GPG key 0 x8483C65D: Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>" Fingerprint: 99 DB 70 FA E1D7 CE22 7 FB6 4882 05 B5 55 B3 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 85532523 d784 --- > e091ece0364d Step 9 /15 : ENV JAVA_HOME $MYPATH /jdk1.8.0 _301-amd64 --- > Running in 473066 cf57f4 Removing intermediate container 473066 cf57f4 --- > 0 a8963a2c1ab Step 10 /15 : ENV CLASSPATH $JAVA_HOME /lib/ --- > Running in 78 a2cb9b06cd Removing intermediate container 78 a2cb9b06cd --- > 3 dd34a2857b4 Step 11 /15 : ENV CATALINA_HOME $MYPATH /apache-tomcat-9 .0.55 --- > Running in 4 ca540479e3d Removing intermediate container 4 ca540479e3d --- > fa38f4581510 Step 12 /15 : ENV CATALINA_BASH $MYPATH /apache-tomcat-9 .0.55 --- > Running in 31 dc5b38478c Removing intermediate container 31 dc5b38478c --- > 8 ae919106bf6 Step 13 /15 : ENV PATH $PATH: $JAVA_HOME /bin:$CATALINA_HOME /bin:$CATALINA_HOME /lib --- > Running in d3fe1f81fab7 Removing intermediate container d3fe1f81fab7 --- > dd8b07b2adfd Step 14 /15 : EXPOSE 8080 --- > Running in 1 f1601f2dcc2 Removing intermediate container 1 f1601f2dcc2 --- > 9078648 b7a2e Step 15 /15 : CMD $CATALINA_HOME /bin/startup.sh && tail -F $CATALINA_HOME /logs/catalina.out --- > Running in 6 a3b2aefaf44 Removing intermediate container 6 a3b2aefaf44 --- > 23 a538c107a0 Successfully built 23 a538c107a0 Successfully tagged tomcat-test :latest
查看镜像
1 2 3 [root @fantasy tomcat ] REPOSITORY TAG IMAGE ID CREATED SIZE tomcat-test latest 23 a538c107a0 25 minutes ago 673 MB
启动镜像
1 2 3 [root @fantasy tomcat ] 9 d391e13efdc495206429dbdb0392180a7bd3a4750cbc1419c31c80cd69c6b7b[root @fantasy tomcat ]
启动时将 tomcat 的 webinfos 和 logs 目录都挂载到了本机。
查看挂载目录
1 2 [root @fantasy tomcat ] logs webinfos
这里找到了挂载到本机的两个目录,说明挂载成功了。
进入容器
1 2 3 4 5 6 7 8 9 10 11 [root @fantasy tomcat ] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9 d391e13efdc tomcat-test "/bin/sh -c '$CATALI …" 24 minutes ago Up 24 minutes 0.0 .0.0 :8080 ->8080 /tcp fantasy-tomcat [root @fantasy tomcat ] [root @9 d391e13efdc local ] apache-tomcat-9 .0.55 bin etc games include jdk1.8.0 _301 lib lib64 libexec readme.txt sbin share src [root @fantasy tomcat ] [root @9 d391e13efdc local ] apache-tomcat-9 .0.55 bin etc games include jdk1.8.0 _301 lib lib64 libexec readme.txt sbin share src
jdk 和 readme.txt 都是具备了的,且 tomcat 目录下的文件也是完整的。
查看挂载文件
这里以 logs 为例,我们先进入 tomcat 容器中的 logs 文件夹查看日志内容。
1 2 3 4 5 [root @9 d391e13efdc apache -tomcat -9.0 .55 ] [root @9 d391e13efdc logs ] catalina.out [root @9 d391e13efdc logs ] /usr/local//apache-tomcat-9 .0.55 /bin/catalina.sh: line 504 : /usr/local//jdk1.8.0 _301-amd64 /bin/java: No such file or directory
然后再退出查看主机上挂载的 logs 文件夹。
1 2 3 4 5 6 7 [root @9 d391e13efdc logs ] exit [root @fantasy tomcat ] [root @fantasy logs ] catalina.out [root @fantasy logs ] /usr/local//apache-tomcat-9 .0.55 /bin/catalina.sh: line 504 : /usr/local//jdk1.8.0 _301-amd64 /bin/java: No such file or directory
两个地方 logs 下的文件内容一致,说明挂载成功。
发布镜像到 Docker Hub
注册账号
如果没有 Docker Hub 账号,先注册账号:https://hub.docker.com/
登录 Docker Hub 账号
1 2 3 4 5 6 [root @fantasy logs ] Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/ Login Succeeded
发布镜像
docker push
直接发布镜像
1 2 3 4 [root @fantasy logs ] Preparing 909 db45c4bc4: Preparing 74 ddd0ec08fa: Preparing denied: requested access to the resource is denied
访问资源被拒绝了。拒绝的原因是我们没有带标签,默认的 latest 标签是不能被识别的。
指定镜像标签
docker tag
我们可以使用 docker tag
命令给镜像加一个标签。
必须以 账号名/镜像名:标签 的格式命令才能提交。
找到之前打的测试镜像 centos-my
IMAGE ID
再次发布镜像
1 2 3 4 5 [root @fantasy logs ] [root @fantasy logs ] REPOSITORY TAG IMAGE ID CREATED SIZE fantasyke/centos 1.0 7 d202bdf002b 29 hours ago 323 MB centos-my latest 7 d202bdf002b 29 hours ago 323 MB
这样就能发布成功了。且可以发现,镜像的发布也是分层发布的 。
1 2 3 4 5 6 [root @fantasy logs ] Using default tag: latestThe push refers to repository [docker.io /library /centos -my ] The push refers to repository [docker.io /fantasyke /centos ] 74 ddd0ec08fa: Mounted from library/centos1.0 : digest: sha256:58 c60ae4f5f1e27d7027ebc641f60f6768a474b617a30b48916fe02de14e0892 size: 529
这样就发布成功了。
配置国内镜像站
由于对国外网络的限制,发布镜像到 DockerHub 是比较缓慢的。
这里可以使用配置 Docker 国内镜像站 的方式实现加速。
运行以下命令即可:
1 2 3 4 5 6 7 8 9 10 [root @fantasy ~] docker version >= 1.12 { "registry-mirrors" : ["XXXXXXXXX" ] } Success. You need to restart docker to take effect: sudo systemctl restart docker [root @fantasy ~] [root @fantasy ~]
该脚本可以将 --registry-mirror
加入到 Docker 配置文件 /etc/docker/daemon.json
中。
适用于 Ubuntu14.04 、Debian 、CentOS6 、CentOS7 、Fedora 、Arch Linux 、openSUSE Leap 42.1 ,其他版本可能有细微不同。
去 Docker Hub 上以 账号名/镜像名 搜索我们刚发布的镜像,发现是可以搜索到的。
查看详情也可以镜像的具体信息。
DIGEST 的值正是刚才发布后返回值 ecefaae6c5a2cab84693175ea3b18d0d0a7aa0160e33a0bf3eb4ab626b10f0f1
的缩写。
且镜像的大小是小于我们本地镜像的,说明发布的过程中也会压缩镜像 。
拉取我们发布的镜像
1 2 3 4 5 [root @fantasy logs ] 1.0 : Pulling from fantasyke/centosDigest: sha256:ecefaae6c5a2cab84693175ea3b18d0d0a7aa0160e33a0bf3eb4ab626b10f0f1 Status: Image is up to date for fantasyke/centos:1.0 docker.io/fantasyke/centos:1.0
无法拉取。原因很简单,因为我们本地存在了同名镜像。
我们先删除这个镜像再拉取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [root @fantasy logs ] Untagged: fantasyke/centos:1.0 Untagged: fantasyke/centos@sha256:ecefaae6c5a2cab84693175ea3b18d0d0a7aa0160e33a0bf3eb4ab626b10f0f1 Untagged: centos-my :latest Untagged: fantasy/centos:1.0 Deleted: sha256:7 d202bdf002bbd95d8016fa5807a486d6c50e195879eddd88cb602172fc51ffe Deleted: sha256:ad95558eb65801f5871215837558156c5e33ba351b3b52e0a50aac045abb46c1 Deleted: sha256:5 c5def0bbb85d8779d02f115c3d072fe9adb1fd07556ee8c5a130823ecf6811d Deleted: sha256:b5bd21416741daec348f417dbea1b73001e257f1e63a5d2abddabc8554fca611 Deleted: sha256:a9431f90fd3f23387c456ad5b925dbb9531beece3eab825848db99db29c6a1fa Deleted: sha256:9 f54f48660acb350921aefab74769e51fc7917a1e1e730d3df2edd1513517c42 Deleted: sha256:fb41ece5d944c667762945fdf7275a1d267acd92fe9dc56709fc3adaca6f087f Deleted: sha256:be89377d4c2ccea11308d8196ba53f03985882db015e01ed8b54fc114f4ba058 Deleted: sha256:9616888 f3b103230ed5f378af4afc11b7ce7ed3d96653e5bd918c49152bbdf8c [root @fantasy logs ] 1.0 : Pulling from fantasyke/centosa1d0c7532777: Already exists 0594 d57f8468: Already exists 9 c13f720f33e: Already exists Digest: sha256:ecefaae6c5a2cab84693175ea3b18d0d0a7aa0160e33a0bf3eb4ab626b10f0f1 Status: Downloaded newer image for fantasyke/centos:1.0 docker.io/fantasyke/centos:1.0 [root @fantasy logs ] REPOSITORY TAG IMAGE ID CREATED SIZE fantasyke/centos 1.0 7 d202bdf002b 29 hours ago 323 MB
拉取成功,且大小又恢复到了之前本地的镜像大小,说明拉取的过程中也会解压镜像 。
启动拉取的镜像
1 2 3 [root @fantasy logs ] [root @168 c9e550886 local ] [root @168 c9e550886 local ]
vim
命令也是可以使用的,镜像发布成功。
发布镜像到阿里云镜像仓库
登录阿里云,点击我的阿里云
创建实例
这里以创建个人版实例为例。
我这里已经创建好了,如果没有创建点击创建即可。
进入镜像仓库
创建好个人实例后,点击进入。
创建命名空间
一个账号只能创建 3 个命名空间,需要谨慎创建。
创建好后就是这样。
创建镜像仓库
点击下一步,创建本地仓库
至此,我们就创建好了阿里云的镜像仓库,具体的操作步骤上图也写得非常清楚。
退出登录的账号
如果之前登录了 Docker Hub 账号或者其他阿里云账号,先退出账号。
1 2 [root @fantasy logs ] Removing login credentials for https://index.docker.io/v1/
登录阿里云账号
1 2 3 4 5 6 [root @fantasy logs ] Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/ Login Succeeded
设置镜像标签
1 2 3 4 5 [root @fantasy logs ] [root @fantasy logs ] REPOSITORY TAG IMAGE ID CREATED SIZE fantasyke/centos 1.0 7 d202bdf002b 32 hours ago 323 MB registry.cn-hangzhou .aliyuncs.com/fantasyke/fantasy 1.0 7 d202bdf002b 32 hours ago 323 MB
提交镜像
1 2 3 4 5 6 [root @fantasy logs ] The push refers to repository [registry.cn -hangzhou.aliyuncs.com /fantasyke /fantasy ] de70c523870b: Pushed 909 db45c4bc4: Pushed 74 ddd0ec08fa: Pushed 1.0 : digest: sha256:ecefaae6c5a2cab84693175ea3b18d0d0a7aa0160e33a0bf3eb4ab626b10f0f1 size: 953
查看提交的镜像
提交的镜像可以在这里查看。
拉取镜像
先删除本地镜像, 再拉取测试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root @fantasy logs ] Untagged: fantasyke/centos:1.0 Untagged: fantasyke/centos@sha256:ecefaae6c5a2cab84693175ea3b18d0d0a7aa0160e33a0bf3eb4ab626b10f0f1 Untagged: registry.cn-hangzhou .aliyuncs.com/fantasyke/fantasy/centos:1.0 Untagged: registry.cn-hangzhou .aliyuncs.com/fantasyke/fantasy:1.0 Untagged: registry.cn-hangzhou .aliyuncs.com/fantasyke/fantasy@sha256:ecefaae6c5a2cab84693175ea3b18d0d0a7aa0160e33a0bf3eb4ab626b10f0f1 Deleted: sha256:7 d202bdf002bbd95d8016fa5807a486d6c50e195879eddd88cb602172fc51ffe [root @fantasy logs ] 1.0 : Pulling from fantasyke/fantasya1d0c7532777: Already exists 0594 d57f8468: Already exists 9 c13f720f33e: Already exists Digest: sha256:ecefaae6c5a2cab84693175ea3b18d0d0a7aa0160e33a0bf3eb4ab626b10f0f1 Status: Downloaded newer image for registry.cn-hangzhou .aliyuncs.com/fantasyke/fantasy:1.0 registry.cn-hangzhou .aliyuncs.com/fantasyke/fantasy:1.0 [root @fantasy logs ] REPOSITORY TAG IMAGE ID CREATED SIZE registry.cn-hangzhou .aliyuncs.com/fantasyke/fantasy 1.0 7 d202bdf002b 32 hours ago 323 MB
启动镜像