您好,登錄后才能下訂單哦!
如何使用Dockerfile構建一個tomcat鏡像?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
dockerfile 是用來構建docker鏡像的文件!命令參數腳本!
構建步驟:
# 以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構建過程
基礎知識:
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<m15010969094@163.com> # 配置環境變量 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<m15010969094@163.com> ---> 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) <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 : 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 # 編寫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<m15010969094@163.com> # 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
地址 https://hub.docker.com/ 注冊自己的賬號
確定自己的賬號可以登錄
在我們的服務器上提交
[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
登錄完畢后就可以提交鏡像了
[root@localhost logs]# docker push xxx/mytomcat:1.0
關于如何使用Dockerfile構建一個tomcat鏡像問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。