基于一个centos创建一个tomcat镜像 1. 准备工作:
创建目录
下载jdk和tomcat: jdk和tomcat下载到Dockerfile所在的目录
1 2 3 4 [root@localhost tomcat]# pwd /usr/local/docker/docker_file/tomcat [root@localhost tomcat]# ls apache-tomcat-8.5.60.tar.gz Dockerfile jdk-8u271-linux-x64.tar.gz
2. 创建并编写Dockerfile文件 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 FROM centosMAINTAINER Tom<Tom@qq.com>ENV SOFTWARE_PATH /usr/local/softwareENV JDK_NAME jdk1.8.0 _271ENV TOMCAT_NAME tomcat-8.5 RUN mkdir ${SOFTWARE_PATH} ADD jdk-8u271-linux-x64.tar.gz /${SOFTWARE_PATH} / ADD apache-tomcat-8.5.60.tar.gz /${SOFTWARE_PATH} / RUN mv ${SOFTWARE_PATH} /apache-tomcat-8.5.60 ${SOFTWARE_PATH} /${TOMCAT_NAME} WORKDIR ${SOFTWARE_PATH} /${TOMCAT_NAME} ENV JAVA_HOME ${SOFTWARE_PATH}/${JDK_NAME}ENV CLASSPATH ${JAVA_HOME}/lib/dt.jar:${JDK_HOME}/lib/tools.jarENV CATALINA_HOME ${SOFTWARE_PATH}/${TOMCAT_NAME}ENV CATALINA_BASE ${SOFTWARE_PATH}/${TOMCAT_NAME}ENV PATH ${PATH}:${JAVA_HOME}/bin:${CATALINA_HOME}/lib:${CATALINA_HOME}/binRUN yum -y install vim EXPOSE 8080 CMD bin/startup.sh && tail -f logs/catalina.out
3. 构造镜像 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 130 131 132 133 134 135 [root@localhost tomcat]# docker build -t mytomcat02:1.0 . Sending build context to Docker daemon 153.6 MB Step 1/18 : FROM centos ---> 0d120b6ccaa8 Step 2/18 : MAINTAINER Tom<Tom@qq.com> ---> Using cache ---> e91b1474c962 Step 3/18 : ENV SOFTWARE_PATH /usr/local/software ---> Running in a83cb85001ea ---> 7c51deda1e35 Removing intermediate container a83cb85001ea Step 4/18 : ENV JDK_NAME jdk1.8.0_271 ---> Running in 3e444324244e ---> c931c9163dee Removing intermediate container 3e444324244e Step 5/18 : ENV TOMCAT_NAME tomcat-8.5 ---> Running in a1f79c6f0f10 ---> 5c0e9091b073 Removing intermediate container a1f79c6f0f10 Step 6/18 : RUN mkdir ${SOFTWARE_PATH} ---> Running in f73a01c7102b ---> 4a798458cf23 Removing intermediate container f73a01c7102b Step 7/18 : ADD jdk-8u271-linux-x64.tar.gz /${SOFTWARE_PATH}/ ---> 454d6815d6dd Removing intermediate container 2ec4f2574a96 Step 8/18 : ADD apache-tomcat-8.5.60.tar.gz /${SOFTWARE_PATH}/ ---> a261ee4d9ff9 Removing intermediate container 870a36b21bcb Step 9/18 : RUN mv ${SOFTWARE_PATH}/apache-tomcat-8.5.60 ${SOFTWARE_PATH}/${TOMCAT_NAME} ---> Running in aa65000baf26 ---> 181d3d8aa457 Removing intermediate container aa65000baf26 Step 10/18 : WORKDIR ${SOFTWARE_PATH}/${TOMCAT_NAME} ---> 5706c8374e51 Removing intermediate container fc0e0fad6771 Step 11/18 : ENV JAVA_HOME ${SOFTWARE_PATH}/${JDK_NAME} ---> Running in e531aa3daadb ---> 76e7217d0c9d Removing intermediate container e531aa3daadb Step 12/18 : ENV CLASSPATH ${JAVA_HOME}/lib/dt.jar:${JDK_HOME}/lib/tools.jar ---> Running in fbd5d397794b ---> 4e161e5b7663 Removing intermediate container fbd5d397794b Step 13/18 : ENV CATALINA_HOME ${SOFTWARE_PATH}/${TOMCAT_NAME} ---> Running in 0663853fed51 ---> 05fb696f3d96 Removing intermediate container 0663853fed51 Step 14/18 : ENV CATALINA_BASE ${SOFTWARE_PATH}/${TOMCAT_NAME} ---> Running in 100d9b73591a ---> 6db8719a58d5 Removing intermediate container 100d9b73591a Step 15/18 : ENV PATH ${PATH}:${JAVA_HOME}/bin:${CATALINA_HOME}/lib:${CATALINA_HOME}/bin ---> Running in d47d95bf5ece ---> 3d5daf705584 Removing intermediate container d47d95bf5ece Step 16/18 : RUN yum -y install vim ---> Running in eada565e8b2c CentOS-8 - AppStream 24 kB/s | 5.8 MB 04:08 CentOS-8 - Base 318 kB/s | 2.2 MB 00:07 CentOS-8 - Extras 505 B/s | 8.6 kB 00:17 Dependencies resolved. ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: vim-enhanced x86_64 2:8.0.1763-13.el8 AppStream 1.4 M Installing dependencies: gpm-libs x86_64 1.20.7-15.el8 AppStream 39 k vim-common x86_64 2:8.0.1763-13.el8 AppStream 6.3 M vim-filesystem noarch 2:8.0.1763-13.el8 AppStream 48 k which x86_64 2.21-12.el8 BaseOS 49 k Transaction Summary ================================================================================ Install 5 Packages Total download size: 7.8 M Installed size: 31 M Downloading Packages: (1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm 28 kB/s | 39 kB 00:01 (2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 25 kB/s | 48 kB 00:01 (3/5): which-2.21-12.el8.x86_64.rpm 23 kB/s | 49 kB 00:02 [MIRROR] vim-common-8.0.1763-13.el8.x86_64.rpm: Curl error (28): Timeout was reached for http://mirror.centos.org/centos/8/AppStream/x86_64/os/Packages/vim-common-8.0.1763-13.el8.x86_64.rpm [Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds] (4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 16 kB/s | 1.4 MB 01:26 (5/5): vim-common-8.0.1763-13.el8.x86_64.rpm 11 kB/s | 6.3 MB 10:02 -------------------------------------------------------------------------------- Total 13 kB/s | 7.8 MB 10:03 warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY CentOS-8 - AppStream 106 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 : which-2.21-12.el8.x86_64 1/5 Installing : vim-filesystem-2:8.0.1763-13.el8.noarch 2/5 Installing : vim-common-2:8.0.1763-13.el8.x86_64 3/5 Installing : gpm-libs-1.20.7-15.el8.x86_64 4/5 Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64 4/5 Installing : vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5 Running scriptlet: vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5 Running scriptlet: vim-common-2:8.0.1763-13.el8.x86_64 5/5 Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5 Verifying : vim-common-2:8.0.1763-13.el8.x86_64 2/5 Verifying : vim-enhanced-2:8.0.1763-13.el8.x86_64 3/5 Verifying : vim-filesystem-2:8.0.1763-13.el8.noarch 4/5 Verifying : which-2.21-12.el8.x86_64 5/5 Installed: gpm-libs-1.20.7-15.el8.x86_64 vim-common-2:8.0.1763-13.el8.x86_64 vim-enhanced-2:8.0.1763-13.el8.x86_64 vim-filesystem-2:8.0.1763-13.el8.noarch which-2.21-12.el8.x86_64 Complete! ---> 2ae53256fc5d Removing intermediate container eada565e8b2c Step 17/18 : EXPOSE 8080 ---> Running in cd441c5d4da4 ---> 4b508c17806e Removing intermediate container cd441c5d4da4 Step 18/18 : CMD bin/startup.sh && tail -f logs/catalina.out ---> Running in 3ae8384422d7 ---> 9715fb4d02ba Removing intermediate container 3ae8384422d7 Successfully built 9715fb4d02ba
1 2 3 [root@localhost tomcat]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mytomcat02 1.0 9715fb4d02ba 3 hours ago 657 MB
4. 运行镜像 1 2 [root@localhost tomcat]# docker run -d -p 8080:8080 mytomcat02:1.0 78a9391a3e10222a3cfcd8298974a840f60ef01f5bbfc26d07706544c7f96c0e
1 2 3 [root@localhost tomcat]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 78a9391a3e10 mytomcat02:1.0 "/bin/sh -c 'bin/s..." 9 seconds ago Up 8 seconds 0.0.0.0:8080->8080/tcp objective_goodall
5. 测试验证 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [root@localhost tomcat]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:1f:b2:aa brd ff:ff:ff:ff:ff:ff inet 192.168.168.130/24 brd 192.168.168.255 scope global noprefixroute dynamic ens33 valid_lft 1624sec preferred_lft 1624sec inet6 fe80::8c0b:4db3:b3f3:c20b/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:d4:c8:84:ba brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:d4ff:fec8:84ba/64 scope link valid_lft forever preferred_lft forever 35: veth25de6ca@if34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether 6e:ec:1f:4a:63:9d brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet6 fe80::6cec:1fff:fe4a:639d/64 scope link valid_lft forever preferred_lft forever
打开浏览器,访问http://192.168.168.130:8080
1 2 3 4 5 [root@localhost tomcat]# docker exec -it 78a9391a3e10 /bin/bash [root@78a9391a3e10 tomcat-8.5]# vim -version VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Nov 11 2019 19:08:24) Garbage after option argument: "-version" More info with: "vim -h"