一、实验环境:
[root@docker1 tomcat9]# cat /etc/redhat-release CentOS Linux release 7.7.1908 (Core) [root@docker1 tomcat9]# uname -r 3.10.0-1062.el7.x86_64
二、实验准备:
没有资源的可以上我的网盘下载:pan.baidu.com/s/1r3Y2ziVV…提取码:eg0dapache-tomcat-8.0.9.tar.gz jdk-8u161-linux-x64.tar.gz
[root@docker1 tomcat9]# pwd /mydockerfile/tomcat9 [root@docker1 tomcat9]# ll total 194244 -rw-r--r--. 1 root root 9144234 May 21 03:08 apache-tomcat-8.0.9.tar.gz -rw-r--r--. 1 root root 0 May 21 02:56 c.txt -rw-r--r--. 1 root root 189756259 May 21 03:08 jdk-8u161-linux-x64.tar.gz [root@docker1 tomcat9]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 470671670cac 4 months ago 237MB
三、创建Dockerfile*(重点)
[root@docker1 tomcat9]# vim dockerfile FROM centos MAINTAINER caq(mildcaq@163.com) # 把宿主机当前目录下c.txt拷贝到容器/usr/local/路径下 COPY c.txt /usr/local/newname.txt # 把java与tomcat加入到容器 ADD jdk-8u161-linux-x64.tar.gz /usr/local/ ADD apache-tomcat-8.0.9.tar.gz /usr/local/ #安装vim编辑器 RUN yum install -y vim #设置工作访问路径 ENV MYPATH /usr/local WORKDIR $MYPATH #设置JAVA与tomcat环境变量 ENV JAVA_HOME /usr/local/jdk1.8.0.161 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.har ENV CATALINA_HOME /usr/local/apache-tomcat-8.0.9 ENV CATALINA_BASE /usr/local/apache-tomcat-8.0.9 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin # 容器运行时的监听端口 EXPOSE 8080 # 启动时运行tomcat CMD /usr/local/apache-tomcat-8.0.9/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.0.9/bin/logs/catalina.out [root@docker1 tomcat9]# ll total 194248 -rw-r--r--. 1 root root 9144234 May 21 03:08 apache-tomcat-8.0.9.tar.gz -rw-r--r--. 1 root root 0 May 21 02:56 c.txt -rw-r--r--. 1 root root 862 May 21 03:31 dockerfile -rw-r--r--. 1 root root 189756259 May 21 03:08 jdk-8u161-linux-x64.tar.gz
四、创建容器
[root@docker1 tomcat9]# docker build -t testtomcat . Sending build context to Docker daemon 198.9MB Step 1/15 : FROM centos ---> 470671670cac Step 2/15 : MAINTAINER caq(mildcaq@163.com) ---> Running in 616cc7ece2e4 Removing intermediate container 616cc7ece2e4 ---> 06cb640d9023 Step 3/15 : COPY c.txt /usr/local/newname.txt ---> 2df05ecd0715 Step 4/15 : ADD jdk-8u161-linux-x64.tar.gz /usr/local/ ---> a6cbd7a8edb5 Step 5/15 : ADD apache-tomcat-8.0.9.tar.gz /usr/local/ ---> 671a34f092e5 Step 6/15 : RUN yum install -y vim ---> Running in 9afb4449d710 CentOS-8 - AppStream 711 kB/s | 7.0 MB 00:10 CentOS-8 - Base 745 kB/s | 2.2 MB 00:03 CentOS-8 - Extras 7.0 kB/s | 5.9 kB 00:00 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-10.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 11 kB/s | 39 kB 00:03 (2/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 212 kB/s | 1.4 MB 00:06 (3/5): which-2.21-10.el8.x86_64.rpm 191 kB/s | 49 kB 00:00 (4/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 5.2 kB/s | 48 kB 00:09 (5/5): vim-common-8.0.1763-13.el8.x86_64.rpm 400 kB/s | 6.3 MB 00:16 -------------------------------------------------------------------------------- Total 457 kB/s | 7.8 MB 00:17 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 178 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-10.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-10.el8.x86_64 5/5 Installed: vim-enhanced-2:8.0.1763-13.el8.x86_64 gpm-libs-1.20.7-15.el8.x86_64 vim-common-2:8.0.1763-13.el8.x86_64 vim-filesystem-2:8.0.1763-13.el8.noarch which-2.21-10.el8.x86_64 Complete! Removing intermediate container 9afb4449d710 ---> fb0dbdbd60b1 Step 7/15 : ENV MYPATH /usr/local ---> Running in f6e5b13f666b Removing intermediate container f6e5b13f666b ---> 7b0699867a8c Step 8/15 : WORKDIR $MYPATH ---> Running in 6628bc543818 Removing intermediate container 6628bc543818 ---> 45566edb4b73 Step 9/15 : ENV JAVA_HOME /usr/local/jdk1.8.0.161 ---> Running in 2354405b3ea8 Removing intermediate container 2354405b3ea8 ---> 44e6df753038 Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.har ---> Running in 2c27723e5688 Removing intermediate container 2c27723e5688 ---> a4bedc629b93 Step 11/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-8.0.9 ---> Running in c3cc0aa29261 Removing intermediate container c3cc0aa29261 ---> 8d6d430728e6 Step 12/15 : ENV CATALINA_BASE /usr/local/apache-tomcat-8.0.9 ---> Running in 43dcef67e92b Removing intermediate container 43dcef67e92b ---> d6e3704b7d1e Step 13/15 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin ---> Running in 91b932191c70 Removing intermediate container 91b932191c70 ---> bda5d6d28085 Step 14/15 : EXPOSE 8080 ---> Running in 1d19f7899119 Removing intermediate container 1d19f7899119 ---> 2ca0b456ba49 Step 15/15 : CMD /usr/local/apache-tomcat-8.0.9/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.0.9/bin/logs/catalina.out ---> Running in 670d46a56409 Removing intermediate container 670d46a56409 ---> d57ca588cc1b Successfully built d57ca588cc1b Successfully tagged testtomcat:latest
五、运行容器
[root@docker1 tomcat9]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@docker1 tomcat9]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE testtomcat latest d57ca588cc1b 5 minutes ago 694MB [root@docker1 tomcat9]# docker run -d -p 80:8080 -v /mydockerfile/tomcat9/test:/usr/local/apache-tomcat-8.0.9/webapps/test -v /mydockerfile/tomcat9/test2/:/usr/local/apache-tomcat-8.0.9/logs --privileged=true testtomcat 43a5d886018d016984ea35941b92d08c72576c3c1ad44f9d9986393884a6dc06
六、进行测试
[root@docker1 tomcat9]# ll total 194248 -rw-r--r--. 1 root root 9144234 May 21 03:08 apache-tomcat-8.0.9.tar.gz -rw-r--r--. 1 root root 0 May 21 02:56 c.txt -rw-r--r--. 1 root root 862 May 21 03:31 dockerfile -rw-r--r--. 1 root root 189756259 May 21 03:08 jdk-8u161-linux-x64.tar.gz drwxr-xr-x. 2 root root 6 May 21 03:42 test drwxr-xr-x. 2 root root 26 May 21 03:42 test2 [root@docker1 tomcat9]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 43a5d886018d testtomcat "/bin/sh -c '/usr/lo…" 3 minutes ago Up 3 minutes 0.0.0.0:80->8080/tcp gallant_swartz [root@docker1 tomcat9]# docker exec 43a5d886018d ls -l total 0 drwxr-xr-x. 1 root root 21 May 21 07:32 apache-tomcat-8.0.9 drwxr-xr-x. 2 root root 6 May 11 2019 bin drwxr-xr-x. 2 root root 6 May 11 2019 etc drwxr-xr-x. 2 root root 6 May 11 2019 games drwxr-xr-x. 2 root root 6 May 11 2019 include drwxr-xr-x. 8 10 143 255 Dec 20 2017 jdk1.8.0_161 drwxr-xr-x. 2 root root 6 May 11 2019 lib drwxr-xr-x. 2 root root 6 May 11 2019 lib64 drwxr-xr-x. 2 root root 6 May 11 2019 libexec -rw-r--r--. 1 root root 0 May 21 06:56 newname.txt drwxr-xr-x. 2 root root 6 May 11 2019 sbin drwxr-xr-x. 5 root root 49 Jan 13 21:49 share drwxr-xr-x. 2 root root 6 May 11 2019 src