Dockerfile
Dockerfile
Dockerfile
简介
Dockerfile 是用来构建 Docker 镜像的文件,可以理解为命令参数脚本。
Dockerfile 是面向开发的,想要打包项目,就要编写 Dockerfile 文件。
命令
以上面的 centos 官方镜像的 Dockerfile 为例。
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
命令运行。
语法
docker build [参数] 路径 | 网络地址 | -
参数
f
:指定要使用的Dockerfile路径。t
:镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。m
:设置内存最大值。
Docker 守护进程执行 Dockerfile 中的指令前,首先会对 Dockerfile 进行语法检查,有语法错误时会返回报错信息。
Error response from daemon: Unknown instruction: RUNCMD
查看构建记录
docker history
语法
docker history 镜像
CMD 与 ENTRYPOINT 区别
CMD 命令演示
编写 Dockerfile
[root@fantasy dockerfile]# vim Dockerfile-cmd-test
[root@fantasy dockerfile]# cat Dockerfile-cmd-test
FROM centos
CMD ["ls","-a"]
构建镜像
[root@fantasy dockerfile]# docker build -f Dockerfile-cmd-test -t zmtest .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM centos
---> 5d0da3dc9764
Step 2/2 : CMD ["ls","-a"]
---> Running in 0a743e929fff
Removing intermediate container 0a743e929fff
---> 1683c0790d49
Successfully built 1683c0790d49
Successfully tagged zmtest:latest
[root@fantasy dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zmtest latest 1683c0790d49 13 minutes ago 231MB
运行镜像
[root@fantasy dockerfile]# docker run zmtest
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
此时 Dockerfile 中编写的命令生效了。
追加 -l 命令
[root@fantasy dockerfile]# docker run zmtest -l
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
[root@fantasy dockerfile]# vim Dockerfile-ent-test
[root@fantasy dockerfile]# cat Dockerfile-ent-test
FROM centos
ENTRYPOINT ["ls","-a"]
构建镜像
[root@fantasy dockerfile]# docker build -f Dockerfile-ent-test -t ent-test .
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM centos
---> 5d0da3dc9764
Step 2/2 : ENTRYPOINT ["ls","-a"]
---> Running in a02d55ae0a00
Removing intermediate container a02d55ae0a00
---> 795973a0ed43
Successfully built 795973a0ed43
Successfully tagged ent-test:latest
[root@fantasy dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ent-test latest 795973a0ed43 22 seconds ago 231MB
运行镜像
[root@fantasy dockerfile]# docker run ent-test
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
此时 Dockerfile 中编写的命令也生效了。
追加 -l 命令
[root@fantasy dockerfile]# docker run ent-test -l
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 .dockerenv
lrwxrwxrwx 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
[root@fantasy dockerfile]# vim Dockerfile-centos-my
[root@fantasy dockerfile]# cat Dockerfile-centos-my
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"]
构建镜像
[root@fantasy dockerfile]# docker build -f Dockerfile-centos-my -t centos-my .
Sending build context to Docker daemon 5.632kB
Step 1/10 : FROM centos
---> 5d0da3dc9764
Step 2/10 : MAINTAINER fantasy<xxxxxx@163.com>
---> Running in 8b7340768878
Removing intermediate container 8b7340768878
---> 9616888f3b10
Step 3/10 : ENV MYPATH /usr/local
---> Running in 2c73446a56ff
Removing intermediate container 2c73446a56ff
---> 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 8483c65d: NOKEY
CentOS Linux 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-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
---> 9f54f48660ac
Step 6/10 : RUN yum -y install net-tools
---> Running in 6caa7361b001
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.20160912git.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.20160912git.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.20160912git.el8.x86_64 1/1
Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Verifying : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Installed:
net-tools-2.0-0.52.20160912git.el8.x86_64
Complete!
Removing intermediate container 6caa7361b001
---> 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
---> 5c5def0bbb85
Step 9/10 : CMD echo "---end---"
---> Running in a9d955b6b389
Removing intermediate container a9d955b6b389
---> ad95558eb658
Step 10/10 : CMD ["/bin/bash"]
---> Running in 190651202e7b
Removing intermediate container 190651202e7b
---> 7d202bdf002b
Successfully built 7d202bdf002b
Successfully tagged centos-my:latest
查看构建的镜像
[root@fantasy dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-my latest 7d202bdf002b About a minute ago 323MB
查看本地镜像的构建记录
[root@fantasy dockerfile]# docker history centos-my
IMAGE CREATED CREATED BY SIZE COMMENT
7d202bdf002b 5 minutes ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
ad95558eb658 5 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
5c5def0bbb85 5 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
b5bd21416741 5 minutes ago /bin/sh -c #(nop) EXPOSE 81 0B
a9431f90fd3f 5 minutes ago /bin/sh -c yum -y install net-tools 27.3MB
9f54f48660ac 5 minutes ago /bin/sh -c yum -y install vim 64.8MB
fb41ece5d944 5 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
be89377d4c2c 5 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
9616888f3b10 5 minutes ago /bin/sh -c #(nop) MAINTAINER fantasy<yifanfantasy… 0B
5d0da3dc9764 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0… 231MB
运行测试
[root@fantasy ~]# docker run -it centos-my
[root@530551bc2162 local]# pwd
/usr/local
[root@530551bc2162 local]# vim test.java
[root@530551bc2162 local]#
默认的工作目录正是 Dockerfile 中设置的 /usr/local
,且可以使用 vim
命令了。
自定义tomcat环境镜像
编写 Dockerfile
FROM centos
MAINTAINER fantasy<xxxxxx@163.com>
COPY readme.txt /usr/local/readme.txt
ENV 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 一般作为镜像说明文件,可以在里面编写镜像的信息。
构建镜像
[root@fantasy tomcat]# docker build -t tomcat-test .
Sending build context to Docker daemon 157.1MB
Step 1/15 : FROM centos
---> 5d0da3dc9764
Step 2/15 : MAINTAINER fantasy<xxxxxx@163.com>
---> Using cache
---> 9616888f3b10
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
---> 7b1e6970b4b3
Step 5/15 : WORKDIR $MYPATH
---> Running in 835dabd080dd
Removing intermediate container 835dabd080dd
---> 7be17b1556ee
Step 6/15 : ADD jdk-8u301-linux-x64.tar.gz $MYPATH
---> 480721043fda
Step 7/15 : ADD apache-tomcat-9.0.55.tar.gz $MYPATH
---> c7bfa13bfcd1
Step 8/15 : RUN yum -y install vim
---> Running in 85532523d784
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 8483c65d: NOKEY
CentOS Linux 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-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 85532523d784
---> e091ece0364d
Step 9/15 : ENV JAVA_HOME $MYPATH/jdk1.8.0_301-amd64
---> Running in 473066cf57f4
Removing intermediate container 473066cf57f4
---> 0a8963a2c1ab
Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/
---> Running in 78a2cb9b06cd
Removing intermediate container 78a2cb9b06cd
---> 3dd34a2857b4
Step 11/15 : ENV CATALINA_HOME $MYPATH/apache-tomcat-9.0.55
---> Running in 4ca540479e3d
Removing intermediate container 4ca540479e3d
---> fa38f4581510
Step 12/15 : ENV CATALINA_BASH $MYPATH/apache-tomcat-9.0.55
---> Running in 31dc5b38478c
Removing intermediate container 31dc5b38478c
---> 8ae919106bf6
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 1f1601f2dcc2
Removing intermediate container 1f1601f2dcc2
---> 9078648b7a2e
Step 15/15 : CMD $CATALINA_HOME/bin/startup.sh && tail -F $CATALINA_HOME/logs/catalina.out
---> Running in 6a3b2aefaf44
Removing intermediate container 6a3b2aefaf44
---> 23a538c107a0
Successfully built 23a538c107a0
Successfully tagged tomcat-test:latest
查看镜像
[root@fantasy tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat-test latest 23a538c107a0 25 minutes ago 673MB
启动镜像
[root@fantasy tomcat]# docker run -d -p 8080:8080 --name fantasy-tomcat -v /home/fantasy/tomcat/webinfos:/usr/local/apache-tomcat-9.0.55/webinfos -v /home/fantasy/tomcat/logs:/usr/local/apache-tomcat-9.0.55/logs tomcat-test
9d391e13efdc495206429dbdb0392180a7bd3a4750cbc1419c31c80cd69c6b7b
[root@fantasy tomcat]#
启动时将 tomcat 的 webinfos 和 logs 目录都挂载到了本机。
查看挂载目录
[root@fantasy tomcat]# ls /home/fantasy/tomcat
logs webinfos
这里找到了挂载到本机的两个目录,说明挂载成功了。
进入容器
[root@fantasy tomcat]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d391e13efdc tomcat-test "/bin/sh -c '$CATALI…" 24 minutes ago Up 24 minutes 0.0.0.0:8080->8080/tcp fantasy-tomcat
[root@fantasy tomcat]# docker exec -it 9d391e13efdc /bin/bash
[root@9d391e13efdc local]# ls
apache-tomcat-9.0.55 bin etc games include jdk1.8.0_301 lib lib64 libexec readme.txt sbin share src
[root@fantasy tomcat]# docker exec -it 9d391e13efdc /bin/bash
[root@9d391e13efdc local]# ls
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 文件夹查看日志内容。
[root@9d391e13efdc apache-tomcat-9.0.55]# cd logs
[root@9d391e13efdc logs]# ls
catalina.out
[root@9d391e13efdc logs]# cat catalina.out
/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 文件夹。
[root@9d391e13efdc logs]# exit
exit
[root@fantasy tomcat]# cd /home/fantasy/tomcat/logs
[root@fantasy logs]# ls
catalina.out
[root@fantasy logs]# cat catalina.out
/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 账号
[root@fantasy logs]# docker login -u fantasyke
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/#credentials-store
Login Succeeded
发布镜像
docker push
直接发布镜像
[root@fantasy logs]# docker push centos-myUsing default tag: latestThe push refers to repository [docker.io/library/centos-my]de70c523870b:
Preparing 909db45c4bc4:
Preparing 74ddd0ec08fa:
Preparing denied: requested access to the resource is denied
访问资源被拒绝了。拒绝的原因是我们没有带标签,默认的 latest 标签是不能被识别的。
指定镜像标签
docker tag
我们可以使用 docker tag
命令给镜像加一个标签。
必须以 账号名/镜像名:标签 的格式命令才能提交。
找到之前打的测试镜像 centos-my
IMAGE ID
再次发布镜像
[root@fantasy logs]# docker tag 7d202bdf002b fantasyke/centos:1.0
[root@fantasy logs]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
fantasyke/centos 1.0 7d202bdf002b 29 hours ago 323MB
centos-my latest 7d202bdf002b 29 hours ago 323MB
这样就能发布成功了。且可以发现,镜像的发布也是分层发布的。
[root@fantasy logs]# docker push fantasyke/centos:1.0
Using default tag: latest
The push refers to repository [docker.io/library/centos-my]
The push refers to repository [docker.io/fantasyke/centos]
74ddd0ec08fa: Mounted from library/centos
1.0: digest: sha256:58c60ae4f5f1e27d7027ebc641f60f6768a474b617a30b48916fe02de14e0892 size: 529
这样就发布成功了。
配置国内镜像站
由于对国外网络的限制,发布镜像到 DockerHub 是比较缓慢的。
这里可以使用配置 Docker 国内镜像站的方式实现加速。
运行以下命令即可:
[root@fantasy ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s XXXXXXX
docker version >= 1.12
{
"registry-mirrors": ["XXXXXXXXX"]
}
Success.
You need to restart docker to take effect: sudo systemctl restart docker
[root@fantasy ~]# systemctl restart docker
[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
的缩写。
且镜像的大小是小于我们本地镜像的,说明发布的过程中也会压缩镜像。
拉取我们发布的镜像
[root@fantasy logs]# docker pull fantasyke/centos:1.0
1.0: Pulling from fantasyke/centos
Digest: sha256:ecefaae6c5a2cab84693175ea3b18d0d0a7aa0160e33a0bf3eb4ab626b10f0f1
Status: Image is up to date for fantasyke/centos:1.0
docker.io/fantasyke/centos:1.0
无法拉取。原因很简单,因为我们本地存在了同名镜像。
我们先删除这个镜像再拉取
[root@fantasy logs]# docker rmi -f 7d202bdf002b
Untagged: fantasyke/centos:1.0
Untagged: fantasyke/centos@sha256:ecefaae6c5a2cab84693175ea3b18d0d0a7aa0160e33a0bf3eb4ab626b10f0f1
Untagged: centos-my:latest
Untagged: fantasy/centos:1.0
Deleted: sha256:7d202bdf002bbd95d8016fa5807a486d6c50e195879eddd88cb602172fc51ffe
Deleted: sha256:ad95558eb65801f5871215837558156c5e33ba351b3b52e0a50aac045abb46c1
Deleted: sha256:5c5def0bbb85d8779d02f115c3d072fe9adb1fd07556ee8c5a130823ecf6811d
Deleted: sha256:b5bd21416741daec348f417dbea1b73001e257f1e63a5d2abddabc8554fca611
Deleted: sha256:a9431f90fd3f23387c456ad5b925dbb9531beece3eab825848db99db29c6a1fa
Deleted: sha256:9f54f48660acb350921aefab74769e51fc7917a1e1e730d3df2edd1513517c42
Deleted: sha256:fb41ece5d944c667762945fdf7275a1d267acd92fe9dc56709fc3adaca6f087f
Deleted: sha256:be89377d4c2ccea11308d8196ba53f03985882db015e01ed8b54fc114f4ba058
Deleted: sha256:9616888f3b103230ed5f378af4afc11b7ce7ed3d96653e5bd918c49152bbdf8c
[root@fantasy logs]# docker pull fantasyke/centos:1.0
1.0: Pulling from fantasyke/centos
a1d0c7532777: Already exists
0594d57f8468: Already exists
9c13f720f33e: Already exists
Digest: sha256:ecefaae6c5a2cab84693175ea3b18d0d0a7aa0160e33a0bf3eb4ab626b10f0f1
Status: Downloaded newer image for fantasyke/centos:1.0
docker.io/fantasyke/centos:1.0
[root@fantasy logs]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
fantasyke/centos 1.0 7d202bdf002b 29 hours ago 323MB
拉取成功,且大小又恢复到了之前本地的镜像大小,说明拉取的过程中也会解压镜像。
启动拉取的镜像
[root@fantasy logs]# docker run -it fantasyke/centos:1.0 /bin/bash
[root@168c9e550886 local]# vim test.java
[root@168c9e550886 local]#
vim
命令也是可以使用的,镜像发布成功。
发布镜像到阿里云镜像仓库
登录阿里云,点击我的阿里云
创建实例
这里以创建个人版实例为例。
我这里已经创建好了,如果没有创建点击创建即可。
进入镜像仓库
创建好个人实例后,点击进入。
创建命名空间
一个账号只能创建 3 个命名空间,需要谨慎创建。
创建好后就是这样。
创建镜像仓库
点击下一步,创建本地仓库
至此,我们就创建好了阿里云的镜像仓库,具体的操作步骤上图也写得非常清楚。
退出登录的账号
如果之前登录了 Docker Hub 账号或者其他阿里云账号,先退出账号。
[root@fantasy logs]# docker logout
Removing login credentials for https://index.docker.io/v1/
登录阿里云账号
[root@fantasy logs]# docker login --username=******** registry.cn-hangzhou.aliyuncs.com
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/#credentials-store
Login Succeeded
设置镜像标签
[root@fantasy logs]# docker tag 7d202bdf002b registry.cn-hangzhou.aliyuncs.com/fantasyke/fantasy:1.0
[root@fantasy logs]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
fantasyke/centos 1.0 7d202bdf002b 32 hours ago 323MB
registry.cn-hangzhou.aliyuncs.com/fantasyke/fantasy 1.0 7d202bdf002b 32 hours ago 323MB
提交镜像
[root@fantasy logs]# docker push registry.cn-hangzhou.aliyuncs.com/fantasyke/fantasy:1.0
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/fantasyke/fantasy]
de70c523870b: Pushed
909db45c4bc4: Pushed
74ddd0ec08fa: Pushed
1.0: digest: sha256:ecefaae6c5a2cab84693175ea3b18d0d0a7aa0160e33a0bf3eb4ab626b10f0f1 size: 953
查看提交的镜像
提交的镜像可以在这里查看。
拉取镜像
先删除本地镜像, 再拉取测试。
[root@fantasy logs]# docker rmi -f 7d202bdf002b
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:7d202bdf002bbd95d8016fa5807a486d6c50e195879eddd88cb602172fc51ffe
[root@fantasy logs]# docker pull registry.cn-hangzhou.aliyuncs.com/fantasyke/fantasy:1.0
1.0: Pulling from fantasyke/fantasy
a1d0c7532777: Already exists
0594d57f8468: Already exists
9c13f720f33e: 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]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/fantasyke/fantasy 1.0 7d202bdf002b 32 hours ago 323MB
启动镜像
[root@fantasy logs]# docker run -it registry.cn-hangzhou.aliyuncs.com/fantasyke/fantasy:1.0