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

浙公網安備 33010602011771號