推荐使用dockerfile(本文直接拉取tomcat需要进入容器自行安装vim):docker利用Dockerfile来制作镜像
1.查找Docker Hub上的tomcat镜像
[root@localhost sysctl.d]# docker search tomcat INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/tomcat Apache Tomcat is an open source implementa... 2324 [OK] docker.io docker.io/tomee Apache TomEE is an all-Apache Java EE cert... 64 [OK] docker.io docker.io/dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba... 52 [OK] docker.io docker.io/davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 wi... 34 [OK] docker.io docker.io/bitnami/tomcat Bitnami Tomcat Docker Image 28 [OK] docker.io docker.io/cloudesire/tomcat Tomcat server, 6/7/8 14 [OK] docker.io docker.io/meirwa/spring-boot-tomcat-mysql-app a sample spring-boot app using tomcat and ... 12 [OK] docker.io docker.io/aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 11 [OK] docker.io docker.io/tutum/tomcat Base docker image to run a Tomcat applicat... 11 docker.io docker.io/jeanblanchard/tomcat Minimal Docker image with Apache Tomcat 8 docker.io docker.io/arm32v7/tomcat Apache Tomcat is an open source implementa... 6 docker.io docker.io/maluuba/tomcat7-java8 Tomcat7 with java8. 3 docker.io docker.io/rightctrl/tomcat CentOS , Oracle Java, tomcat application s... 3 [OK] docker.io docker.io/amd64/tomcat Apache Tomcat is an open source implementa... 2 docker.io docker.io/arm64v8/tomcat Apache Tomcat is an open source implementa... 2 docker.io docker.io/fabric8/tomcat-8 Fabric8 Tomcat 8 Image 2 [OK] docker.io docker.io/99taxis/tomcat7 Tomcat7 1 [OK] docker.io docker.io/camptocamp/tomcat-logback Docker image for tomcat with logback integ... 1 [OK] docker.io docker.io/1and1internet/debian-9-java-8-tomcat-8.5 Our tomcat 8.5 image 0 [OK] docker.io docker.io/cfje/tomcat-resource Tomcat Concourse Resource 0 docker.io docker.io/jelastic/tomcat An image of the Tomcat Java application se... 0 docker.io docker.io/oobsri/tomcat8 Testing CI Jobs with different names. 0 docker.io docker.io/picoded/tomcat7 tomcat7 with jre8 and MANAGER_USER / MANAG... 0 [OK] docker.io docker.io/s390x/tomcat Apache Tomcat is an open source implementa... 0 docker.io docker.io/swisstopo/service-print-tomcat backend tomcat for service-print "the true... 0
2.查看本地已有镜像
docker images
[root@localhost sysctl.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
3.拉取镜像
docker pull tomcat:8 ##version 如果很慢请参照:docker pull centos慢问题的解决方案
[root@localhost sysctl.d]# docker pull tomcat:8 Trying to pull repository docker.io/library/tomcat ... 8: Pulling from docker.io/library/tomcat 22dbe790f715: Pull complete 0250231711a0: Pull complete 6fba9447437b: Pull complete 4afad9c4aba6: Pull complete 12fce9923c9d: Pull complete 0dd81083d77e: Pull complete 4c2afc6ff72d: Pull complete a62332a260f5: Pull complete 96dfb5ad080f: Pull complete 782f63228ce1: Pull complete d610fffe30ad: Pull complete Digest: sha256:aacce4e5ca37a3b8241c544deffd65f4cafbfb1a2fb2066f97621253c34f7dc4 Status: Downloaded newer image for docker.io/tomcat:8 [root@localhost sysctl.d]#
验证
docker images
[root@localhost sysctl.d]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/tomcat 8 dd6ff929584a 2 weeks ago 463 MB [root@localhost sysctl.d]#
4.启动tomcat
docker run -d -p 8080:8080 tomcat ####前者是外部访问端口,后者是容器内映射端口
如下命令可后台启动tomcat
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
[root@localhost sysctl.d]# docker run -d -p 8080:8080 tomcat Unable to find image 'tomcat:latest' locally Trying to pull repository docker.io/library/tomcat ... latest: Pulling from docker.io/library/tomcat Digest: sha256:aacce4e5ca37a3b8241c544deffd65f4cafbfb1a2fb2066f97621253c34f7dc4 Status: Downloaded newer image for docker.io/tomcat:latest 6a3c7c96df932566cc24cf7811ba774eb4c3f59f498ffc79d9f576ba2c589d04
5.查看运行中的容器
[root@localhost sysctl.d]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a3c7c96df93 tomcat "catalina.sh run" 22 seconds ago Up 20 seconds 0.0.0.0:8080->8080/tcp youthful_carson [root@localhost sysctl.d]# cd
6.进入已经运行的容器
docker exec -it containerID /bin/bash 这个命令exit退出,ok容器还在运行
docker attach id 刚方法进入之后,exit退出就会导致容器stop
[root@localhost sysctl.d]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a3c7c96df93 tomcat "catalina.sh run" 22 seconds ago Up 20 seconds 0.0.0.0:8080->8080/tcp youthful_carson 27c726b541d5 mytomcat:1.0 "/root/run.sh" 14 hours ago Up 14 hours 0.0.0.0:58080->8080/tcp mytomcat_1 6afe7d52b011 9f3 "/bin/bash" 15 hours ago Exited (0) 15 hours ago jovial_mayer 8e9e23f4cd3b 9f3 "/bin/bash" 16 hours ago Exited (127) 15 hours ago admiring_rosalind [root@localhost sysctl.d]# cd [root@localhost ~]# [root@localhost ~]# docker exec -it 6a3c /bin/bash root@6a3c7c96df93:/usr/local/tomcat#
添加新文件
root@6a3c7c96df93:/usr/local/tomcat/webapps# cd ROOT/ root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT# ls RELEASE-NOTES.txt asf-logo-wide.svg bg-middle.png bg-nav.png favicon.ico tomcat-power.gif tomcat.gif tomcat.svg WEB-INF bg-button.png bg-nav-item.png bg-upper.png index.jsp tomcat.css tomcat.png root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT# mkdir test root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT# cd test root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT/test# vi showpage.jsp bash: vi: command not found root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT/test# tounch showpage.jsp bash: tounch: command not found root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT/test# touch showpage.jsp root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT/test# vi showpage.jsp bash: vi: command not found
无法使用vi vim
apt-get update,这个命令的作用是:同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。
等更新完毕以后再敲命令:apt-get install vi命令即可
root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT/test# apt-get update Ign:1 http://deb.debian.org/debian stretch InRelease Get:2 http://deb.debian.org/debian stretch-updates InRelease [91.0 kB] Get:3 http://deb.debian.org/debian stretch Release [118 kB] Get:4 http://deb.debian.org/debian stretch-updates/main amd64 Packages [11.1 kB] Get:5 http://deb.debian.org/debian stretch Release.gpg [2434 B] Get:6 http://deb.debian.org/debian stretch/main amd64 Packages [7084 kB] Err:7 http://security.debian.org/debian-security stretch/updates InRelease Cannot initiate the connection to prod.debian.map.fastly.net:80 (2a04:4e42:a::204). - connect (101: Network is unreachable) Cannot initiate the connection to security.debian.org:80 (2401:2500:103:11a::74). - connect (101: Network is unreachable) [IP: 2401:2500:103:11a::74 80] Fetched 7306 kB in 43s (168 kB/s) Reading package lists... Done W: Failed to fetch http://security.debian.org/debian-security/dists/stretch/updates/InRelease Cannot initiate the connection to prod.debian.map.fastly.net:80 (2a04:4e42:a::204). - connect (101: Network is unreachable) Cannot initiate the connection to security.debian.org:80 (2401:2500:103:11a::74). - connect (101: Network is unreachable) [IP: 2401:2500:103:11a::74 80] W: Some index files failed to download. They have been ignored, or old ones used instead. root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT/test#
仍然不可用并提示E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT/test# apt-get install vim --fix-missing Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libgpm2 vim-common vim-runtime xxd Suggested packages: gpm ctags vim-doc vim-scripts The following NEW packages will be installed: libgpm2 vim vim-common vim-runtime xxd 0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded. Need to get 6766 kB of archives. After this operation, 31.2 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 xxd amd64 2:8.0.0197-4+deb9u1 [132 kB] Get:2 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 vim-common all 2:8.0.0197-4+deb9u1 [159 kB] Get:3 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libgpm2 amd64 1.20.4-6.2+b1 [34.2 kB] Get:4 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 vim-runtime all 2:8.0.0197-4+deb9u1 [5407 kB] Get:5 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 vim amd64 2:8.0.0197-4+deb9u1 [1034 kB] Fetched 6766 kB in 8min 44s (12.9 kB/s) debconf: delaying package configuration, since apt-utils is not installed Selecting previously unselected package xxd. (Reading database ... 16623 files and directories currently installed.) Preparing to unpack .../xxd_2%3a8.0.0197-4+deb9u1_amd64.deb ... Unpacking xxd (2:8.0.0197-4+deb9u1) ... Selecting previously unselected package vim-common. Preparing to unpack .../vim-common_2%3a8.0.0197-4+deb9u1_all.deb ... Unpacking vim-common (2:8.0.0197-4+deb9u1) ... Selecting previously unselected package libgpm2:amd64. Preparing to unpack .../libgpm2_1.20.4-6.2+b1_amd64.deb ... Unpacking libgpm2:amd64 (1.20.4-6.2+b1) ... Selecting previously unselected package vim-runtime. ;?eparing to unpack .../vim-runtime_2%3a8.0.0197-4+deb9u1_all.deb ... ding 'diversion of /usr/share/vim/vim80/doc/help.txt to /usr/share/vim/vim80/doc/help.txt.vim-tiny by vim-runtime' Adding 'diversion of /usr/share/vim/vim80/doc/tags to /usr/share/vim/vim80/doc/tags.vim-tiny by vim-runtime' Unpacking vim-runtime (2:8.0.0197-4+deb9u1) ... Selecting previously unselected package vim. Preparing to unpack .../vim_2%3a8.0.0197-4+deb9u1_amd64.deb ... Unpacking vim (2:8.0.0197-4+deb9u1) ... Setting up xxd (2:8.0.0197-4+deb9u1) ... Setting up libgpm2:amd64 (1.20.4-6.2+b1) ... Processing triggers for libc-bin (2.24-11+deb9u4) ... Setting up vim-common (2:8.0.0197-4+deb9u1) ... Setting up vim-runtime (2:8.0.0197-4+deb9u1) ... Processing triggers for hicolor-icon-theme (0.15-1) ... Setting up vim (2:8.0.0197-4+deb9u1) ... update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
7.添加hello wold页面
root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT/test# pwd /usr/local/tomcat/webapps/ROOT/test
root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT/test# vi showpage.jsp
root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT/test# cat showpage.jsp <html> <body> <h2>Hello World!</h2> </body> </html> root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT/test#
8.基于此制作新镜像
退出6a3c 容器,并停止容器
root@6a3c7c96df93:/usr/local/tomcat/webapps/ROOT/test# exit exit [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a3c7c96df93 tomcat "catalina.sh run" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp youthful_carson [root@localhost ~]# docker stop 6a3c 6a3c [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a3c7c96df93 tomcat "catalina.sh run" 2 hours ago Exited (143) 4 seconds ago youthful_carson [root@localhost ~]#
使用以下命令,根据某个“容器 ID”来创建一个新的“镜像”:
docker commit 6a3c my_tomcat:1.0
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos latest 9f38484d220f 11 days ago 202 MB docker.io/tomcat 8 dd6ff929584a 2 weeks ago 463 MB [root@localhost ~]#
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a3c7c96df93 tomcat "catalina.sh run" 2 hours ago Exited (143) 2 minutes ago youthful_carson [root@localhost ~]# docker commit 6a3c my_tomcat:1.0 sha256:20e7c603432c2d9b39598809a028cf9f4e30eb43bf3b5db287fb278d7cb43e11 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE my_tomcat 1.0 20e7c603432c 14 seconds ago 510 MB docker.io/centos latest 9f38484d220f 11 days ago 202 MB docker.io/tomcat 8 dd6ff929584a 2 weeks ago 463 MB [root@localhost ~]#
9.验证新镜像
docker run -d -p 8080:8080 my_tomcat
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a3c7c96df93 tomcat "catalina.sh run" 2 hours ago Exited (143) 11 minutes ago youthful_carson [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE my_tomcat 1.0 20e7c603432c 7 minutes ago 510 MB docker.io/centos latest 9f38484d220f 11 days ago 202 MB docker.io/tomcat 8 dd6ff929584a 2 weeks ago 463 MB [root@localhost ~]# docker run -d -p 8080:8080 --name my_tomcat_00 my_tomcat Unable to find image 'cmiot_tomcat:latest' locally Trying to pull repository docker.io/library/cmiot_tomcat ... /usr/bin/docker-current: repository docker.io/cmiot_tomcat not found: does not exist or no pull access. See '/usr/bin/docker-current run --help'. [root@localhost ~]# docker run -d -p 8080:8080 --name my_tomcat_00 my_tomcat:1.0 0a299b64105fac63371978168a67f9015b361f9816678c0e4bb2ee174aa6ef55 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a299b64105f my_tomcat:1.0 "catalina.sh run" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp my_tomcat_00 6a3c7c96df93 tomcat "catalina.sh run" 2 hours ago Exited (143) 14 minutes ago youthful_carson [root@localhost ~]#