1. 程式人生 > 實用技巧 >使用Dockerfile構建自己的tomcat映象

使用Dockerfile構建自己的tomcat映象

dockerfile 是用來構建docker映象的檔案!命令引數指令碼!

構建步驟:

  1. 編寫一個dockerfile 檔案
  2. docker build 構建成一個映象
  3. docker run 執行映象
  4. docker push 釋出映象(DockerHub、阿里雲映象倉庫)
# 以contes為例 檢視 dockerhub 上 contes 的 dockerfile

FROM scratch
ADD centos-7-x86_64-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="20200809" \
    org.opencontainers.image.title="CentOS Base Image" \
    org.opencontainers.image.vendor="CentOS" \
    org.opencontainers.image.licenses="GPL-2.0-only" \
    org.opencontainers.image.created="2020-08-09 00:00:00+01:00"

CMD ["/bin/bash"]

DockerFile構建過程

基礎知識:

  1. 每個保留關鍵字(指令)都必須是大寫字母
  2. 執行順序從上到下順序執行
  3. 表示註釋
  4. 每個指令都會建立提交一個新的映象層,並提交

dockerfile是面向開發的,我們以後要釋出專案,做映象,就需要編寫dockerfile檔案,這個檔案十分簡單!

Docker映象逐漸成為企業交付的標準!

DockerFile:構建映象,定義了一切的步驟,原始碼;

DockerImages:通過DockerFile構建生成的一個映象,這個映象就是我們最終釋出和執行的產品!

Docker容器:容器就是映象執行起來提供服務!

DockerFile的指令

FROM			# 基礎映象
MAINTAINER		# 映象是誰寫的	姓名 + 郵箱
RUN				# 映象構建的時候需要執行的命令
ADD				# 步驟	eg:tomcat映象---》放入tomcat的壓縮包!新增內容
WORKDIR			# 映象的工作目錄
VOLUME			# 掛載的目錄位置
EXPOST			# 暴露埠配置
CMD				# 指定這個容器啟動的時候要執行的命令,只有最後一個會生效,可被替代
ENTRYPOINT		# 指定這個容器啟動的時候要執行的命令,可以追加命令
ONBUILD			# 當構建一個被繼承的 DockerFile ,這個時候就會執行 ONBUILD 的指令,觸發指令
COPY			# 類似ADD,將我們的檔案拷貝到映象中
ENV 			# 構建的時候設定環境變數

實戰測試

Docker Hub 中 99% 的映象都是從 FROM scratch 這個基礎映象過來的 ,然後配置我們需要的軟體和配置來進行構建

建立一個自己的centos

# 1、編寫DockerFile的檔案
FROM centos
# 作者資訊
MAINTAINER yinxiaodong<[email protected]>
# 配置環境變數 
ENV MYPATH /user/local
# 指定映象的工作目錄
WORKDIR $MYPATH 
 
RUN yum install -y vim
RUN yum install -y net-tools
# 暴露80埠 
EXPOSE 80
 
CMD echo $MYPATH
CMD echo "---------end--------"
CMD /bin/bash


# 2、通過檔案構建映象
# 命令 docker build -f dockerfile檔案路徑	-t 映象名:版本號 .
[root@localhost dockerfile]# docker build -f mydockerfile-centos -t mycentos:0.1 .
Sending build context to Docker daemon  2.048kB
Step 1/9 : FROM centos
 ---> 0d120b6ccaa8
Step 2/9 : MAINTAINER yinxiaodong<[email protected]>
 ---> Running in f79b12ffb083
Removing intermediate container f79b12ffb083
 ---> 8ee53d3f7a65
Step 3/9 : ENV MYPATH /user/local
 ---> Running in 659a2c96d5f7
Removing intermediate container 659a2c96d5f7
 ---> 59ab131ef44c
Step 4/9 : WORKDIR $MYPATH
 ---> Running in 0b4b8f9c65bb
Removing intermediate container 0b4b8f9c65bb
 ---> 408b06671488
Step 5/9 : RUN yum install -y net-tools
 ---> Running in 151f81148a87
CentOS-8 - AppStream                            118 kB/s | 5.8 MB     00:50    
CentOS-8 - Base                                 218 kB/s | 2.2 MB     00:10    
CentOS-8 - Extras                               1.8 kB/s | 7.3 kB     00:04    
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   219 kB/s | 323 kB     00:01    
--------------------------------------------------------------------------------
Total                                            66 kB/s | 323 kB     00:04     
warning: /var/cache/dnf/BaseOS-f6a80ba95cf937f2/packages/net-tools-2.0-0.51.20160912git.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - Base                                 162 kB/s | 1.6 kB     00:00    
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <[email protected]>"
 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       : 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 151f81148a87
 ---> c0a0546c9b2a
Step 6/9 : EXPOSE 80
 ---> Running in 42000936515d
Removing intermediate container 42000936515d
 ---> fe68114ecf3f
Step 7/9 : CMD echo $MYPATH
 ---> Running in c393fc53a354
Removing intermediate container c393fc53a354
 ---> a6924276bf90
Step 8/9 : CMD echo "---------end--------"
 ---> Running in 5994de56f0a1
Removing intermediate container 5994de56f0a1
 ---> a8ba0ebb3770
Step 9/9 : CMD /bin/bash
 ---> Running in d1fa2d436363
Removing intermediate container d1fa2d436363
 ---> 41bb76be4884
Successfully built 41bb76be4884
Successfully tagged mycentos:0.1
[root@localhost dockerfile]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
mycentos              0.1                 41bb76be4884        7 seconds ago       246MB
centos                1.0                 d840628c30a9        6 hours ago         215MB
centos                latest              0d120b6ccaa8        2 days ago          215MB

# 啟動 mycentos 使用 ifconfig 命令檢視ip資訊
[root@localhost dockerfile]# docker run -it mycentos:0.1
[root@494c2bc72263 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 6  bytes 516 (516.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
# 檢視所在目錄        /user/local 正是我們在dockerfile中指定的目錄 
[root@494c2bc72263 local]# pwd
/user/local


檢視映象的構建歷史記錄

# 命令 docker history 映象id

[root@localhost ~]# docker history 41bb76be4884
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
41bb76be4884        16 minutes ago      /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B                  
a8ba0ebb3770        16 minutes ago      /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B                  
a6924276bf90        16 minutes ago      /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B                  
fe68114ecf3f        16 minutes ago      /bin/sh -c #(nop)  EXPOSE 80                    0B                  
c0a0546c9b2a        16 minutes ago      /bin/sh -c yum install -y net-tools             31.3MB              
408b06671488        17 minutes ago      /bin/sh -c #(nop) WORKDIR /user/local           0B                  
59ab131ef44c        17 minutes ago      /bin/sh -c #(nop)  ENV MYPATH=/user/local       0B                  
8ee53d3f7a65        17 minutes ago      /bin/sh -c #(nop)  MAINTAINER yinxiaodong<m1…   0B                  
0d120b6ccaa8        2 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           2 days ago          /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
<missing>           2 days ago          /bin/sh -c #(nop) ADD file:538afc0c5c964ce0d…   215MB   

我們平時拿到一個映象,可以研究一下它是怎麼做的---->docker history 映象id

CMD 和 ENTRYPOINT 的區別

CMD 和 ENTRYPOINT 的區別

CMD				# 指定這個容器啟動的時候要執行的命令,只有最後一個會生效,可被替代
ENTRYPOINT		# 指定這個容器啟動的時候要執行的命令,可以追加命令

# 測試 CMD
# 編寫DockerFile的檔案
[root@localhost dockerfile]# cat dockerfile-cmd-test 
FROM centos
CMD ["ls","-a"]
# 根據 DockerFile 構建映象
[root@localhost dockerfile]# docker build -f dockerfile-cmd-test -t testcmd:0.1 .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM centos
 ---> 0d120b6ccaa8
Step 2/2 : CMD ["ls","-a"]
 ---> Running in b3f8ba72222b
Removing intermediate container b3f8ba72222b
 ---> 561e47f88730
Successfully built 561e47f88730
Successfully tagged testcmd:0.1	# 構建成功
# 檢視映象
[root@localhost dockerfile]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
testcmd               0.1                 561e47f88730        6 seconds ago       215MB
centos                latest              0d120b6ccaa8        2 days ago          215MB
# 啟動映象 發現ls -a命令生效
[root@localhost dockerfile]# docker run -it testcmd:0.1
.   .dockerenv	dev  home  lib64       media  opt   root  sbin	sys  usr
..  bin		etc  lib   lost+found  mnt    proc  run   srv	tmp  var

# 啟動命令中 追加一個 -l, 我們期望的是 ls -a -l,但是 報錯,這裡將 ls -a 替換成了 -l,最終的命令是 -l 所以報錯。
[root@localhost dockerfile]# docker run -it 561e47f88730 -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.
# 將啟動命令替換成 docker run -it testcmd:0.1 ls -al, 成功列印詳細資訊;
[root@localhost dockerfile]# docker run -it testcmd:0.1 ls -al
total 0
drwxr-xr-x.   1 root root   6 Aug 13 08:20 .
drwxr-xr-x.   1 root root   6 Aug 13 08:20 ..
-rwxr-xr-x.   1 root root   0 Aug 13 08:20 .dockerenv
lrwxrwxrwx.   1 root root   7 May 11  2019 bin -> usr/bin
drwxr-xr-x.   5 root root 360 Aug 13 08:20 dev
drwxr-xr-x.   1 root root  66 Aug 13 08:20 etc
drwxr-xr-x.   2 root root   6 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   6 Aug  9 21:40 lost+found
drwxr-xr-x.   2 root root   6 May 11  2019 media
drwxr-xr-x.   2 root root   6 May 11  2019 mnt
drwxr-xr-x.   2 root root   6 May 11  2019 opt
dr-xr-xr-x. 123 root root   0 Aug 13 08:20 proc
dr-xr-x---.   2 root root 162 Aug  9 21:40 root
drwxr-xr-x.  11 root root 163 Aug  9 21:40 run
lrwxrwxrwx.   1 root root   8 May 11  2019 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 May 11  2019 srv
dr-xr-xr-x.  13 root root   0 Aug 11 09:58 sys
drwxrwxrwt.   7 root root 145 Aug  9 21:40 tmp
drwxr-xr-x.  12 root root 144 Aug  9 21:40 usr
drwxr-xr-x.  20 root root 262 Aug  9 21:40 var



# 測試二:現在我們把 DockerFile 中 CMD 替換成  ENTRYPOINT 後重新構建,執行 看看結果

[root@localhost dockerfile]# cat dockerfile-cmd-test 
FROM centos
ENTRYPOINT ["ls","-a"]
# 重新構建映象
[root@localhost dockerfile]# docker build -f dockerfile-cmd-test  -t testcmd:0.2 .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM centos
 ---> 0d120b6ccaa8
Step 2/2 : ENTRYPOINT ["ls","-a"]
 ---> Running in c634ca09fabe
Removing intermediate container c634ca09fabe
 ---> 52d295395f08
Successfully built 52d295395f08
Successfully tagged testcmd:0.2
# 檢視映象
[root@localhost dockerfile]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
testcmd               0.2                 52d295395f08        7 seconds ago       215MB
testcmd               0.1                 561e47f88730        12 minutes ago      215MB
centos                latest              0d120b6ccaa8        2 days ago          215MB
# 執行testcmd:0.2映象 並追加 -l ,發現 打印出了詳細資訊	
[root@localhost dockerfile]# docker run -it testcmd:0.2 -l
total 0
drwxr-xr-x.   1 root root   6 Aug 13 08:17 .
drwxr-xr-x.   1 root root   6 Aug 13 08:17 ..
-rwxr-xr-x.   1 root root   0 Aug 13 08:17 .dockerenv
lrwxrwxrwx.   1 root root   7 May 11  2019 bin -> usr/bin
drwxr-xr-x.   5 root root 360 Aug 13 08:17 dev
drwxr-xr-x.   1 root root  66 Aug 13 08:17 etc
drwxr-xr-x.   2 root root   6 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   6 Aug  9 21:40 lost+found
drwxr-xr-x.   2 root root   6 May 11  2019 media
drwxr-xr-x.   2 root root   6 May 11  2019 mnt
drwxr-xr-x.   2 root root   6 May 11  2019 opt
dr-xr-xr-x. 121 root root   0 Aug 13 08:17 proc
dr-xr-x---.   2 root root 162 Aug  9 21:40 root
drwxr-xr-x.  11 root root 163 Aug  9 21:40 run
lrwxrwxrwx.   1 root root   8 May 11  2019 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 May 11  2019 srv
dr-xr-xr-x.  13 root root   0 Aug 11 09:58 sys
drwxrwxrwt.   7 root root 145 Aug  9 21:40 tmp
drwxr-xr-x.  12 root root 144 Aug  9 21:40 usr
drwxr-xr-x.  20 root root 262 Aug  9 21:40 var

DockerFile中很多命令都十分相似,我們需要了解他的區別,最好的學習就是對比進行測試檢視效果!

實戰:Tomcat映象

1、準備映象檔案 tomcat壓縮包 jdk壓縮包

[root@localhost tomcat]# ls
apache-tomcat-8.5.43.tar.gz  Dockerfile  jdk-8u211-linux-x64.tar.gz  read.txt

2、編寫dockerfile檔案, 官方命名Dockerfile,build會自動尋找這個檔案,不需要 -f 指定!

# 引入基礎
FROM centos
# 作者資訊
MAINTAINER yinxiaodoong<[email protected]>

# copy檔案read.txt到容器的 /usr/local/read.txt 目錄下
COPY read.txt /usr/local/read.txt

# 新增jdk tomcat ---> 自動解壓
ADD jdk-8u211-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.5.43.tar.gz /usr/local/

# 安裝 vim 命令
RUN yum install -y vim


# 配置 MYPATH
ENV MYPATH /usr/local/
# 指定 進入容器預設路徑 /user/local/ 
WORKDIR $MYPATH


# 配置jdk環境變數
ENV JAVA_HOME /usr/local/jdk1.8.0_211
ENV CLASSPATH $JAVA_HOME/lib/dt.jat:$JAVA_HOME/lib/tools.jar

# 配置tomcat環境變數
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.43
ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.43

ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin


# 暴露8080埠
EXPOSE 8080


# /usr/local/tomcat/webapps# 
# 啟動容器時 啟動tomcat
CMD /usr/local/apache-tomcat-8.5.43/bin/startup.sh && tail -f /usr/local/apache-tomcat-8.5.43/logs/catalina.out 

# 構建tomcat映象
[root@localhost tomcat]# docker build -t mytomcat 
[root@localhost tomcat]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mytomcat            latest              a9707559afa1        8 seconds ago       693MB
centos              latest              0d120b6ccaa8        4 days ago          215MB


[root@localhost tomcat]# docker run -d -p 8081:8080 --name mytomcat--01 -v /home/yinxiaodong/build/tomcat/test:/usr/local/apache-tomcat-8.5.43/webapps/test -v /home/yinxiaodong/build/tomcat/logs:/usr/local/apache-tomcat-8.5.43/logs mytomcat

釋出自己的映象

釋出到 dockerHub

  1. 地址 https://hub.docker.com/ 註冊自己的賬號
  2. 確定自己的賬號可以登入
  3. 在我們的伺服器上提交
[root@localhost logs]# docker login --help

Usage:	docker login [OPTIONS] [SERVER]

Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.

Options:
  -p, --password string   Password
      --password-stdin    Take the password from stdin
  -u, --username string   Username
[root@localhost logs]# 



[root@localhost logs]# docker login -u xxx -p xxx
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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

  1. 登入完畢後就可以提交映象了
[root@localhost logs]# docker push xxx/mytomcat:1.0