访问docker hub仓库找出你想安装的版本:docker hub
网址:https://registry.hub.docker.com/
直接在搜索框里搜tomcat就可以,如果想把自己镜像放到上边就自己注册一个账号
选择tomcat
这里先看下啥镜像都没有,容器也没有。
[root@wangazure ~]# docker images #查看所有镜像 REPOSITORY TAG IMAGE ID CREATED SIZE [root@wangazure ~]# docker ps #查看所有容器 ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@wangazure ~]#
把复制官网上的命令贴在这等着就好了
[root@wangazure ~]# docker pull tomcat:8.0 #拉取tomcat镜像 8.0: Pulling from library/tomcat f189db1b88b3: Pull complete 3d06cf2f1b5e: Pull complete edd0da9e3091: Pull complete eb7768aae14e: Pull complete e2780f585e0f: Pull complete e5ed720afeba: Pull complete d9e134700cfc: Pull complete e4804b33d02a: Pull complete b9df0c24315e: Pull complete 49fdae8eaa20: Pull complete 1aea3d9a32e6: Pull complete Digest: sha256:8ecb10948deb32c34aeadf7bf95d12a93fbd3527911fa629c1a3e7823b89ce6f Status: Downloaded newer image for tomcat:8.0 docker.io/library/tomcat:8.0 [root@wangazure ~]#
再看一下docker里的镜像,这里就有了tomcat镜像
[root@wangazure ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat 8.0 ef6a7c98d192 3 years ago 356MB [root@wangazure ~]#2、运行tomcat镜像
这里的 -p 8080:8080 的意思就是把容器的端口号8080映射到linux系统里的8080端口这里的两个端口8080前边的是linux里的端口号,后边的是运行的tomcat里的端口,外边也就是linux里的端口号可以随便换的,而容器里的是不能换的。
这里的 -d 的意思是后台运行
这里 --name 是给容器起一个别名
[root@wangazure ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat 8.0 ef6a7c98d192 3 years ago 356MB [root@wangazure ~]# docker run -p 8080:8080 -d --name tomcat01 tomcat:8.0 643b0adcfd17b23b918aba02d4a6a39bc51f5e9968b2d22d9419137fccda6dff [root@wangazure ~]#
再次查看docker里的容器
[root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 643b0adcfd17 tomcat:8.0 "catalina.sh run" 6 minutes ago Up 6 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]#
3、开多个端口给tomcat这里去访问浏览器8080端口就可以看见tom猫了,版本也是8.0的(如果用的阿里云的服务器注意要开放8080端口再访问)
这里我又开放了一个8082的作为tomcat,名字为tomcat02,注意:这里的名字(tomcat02)和Linux端口不能和上边的名字(tomcat01)一样!
[root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 643b0adcfd17 tomcat:8.0 "catalina.sh run" 6 minutes ago Up 6 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]# docker run -p 8082:8080 -d --name tomcat02 tomcat:8.0 1adf1a9b21a145c5e5d732170260f1f85f2edc38eef38ea1e1a91f791f78a26c [root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1adf1a9b21a1 tomcat:8.0 "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat02 643b0adcfd17 tomcat:8.0 "catalina.sh run" 9 minutes ago Up 9 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]#
上浏览器访问8082,又见tomcat猫了
- 关闭容器
[root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1adf1a9b21a1 tomcat:8.0 "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat02 643b0adcfd17 tomcat:8.0 "catalina.sh run" 9 minutes ago Up 9 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]# docker stop 1adf1a9b21a1 #这里写容器的id或者写NAMES里的名字都行(关闭的是8082的tomcat) 1adf1a9b21a1 [root@wangazure ~]#
再次查看容器,就只剩一个8080端口的了
[root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 643b0adcfd17 tomcat:8.0 "catalina.sh run" 15 minutes ago Up 15 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]#
- 启动关闭的容器
[root@wangazure ~]# docker start tomcat02 tomcat02 [root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1adf1a9b21a1 tomcat:8.0 "catalina.sh run" 11 minutes ago Up 1 second 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat02 643b0adcfd17 tomcat:8.0 "catalina.sh run" 18 minutes ago Up 18 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]#
- 暂停容器
暂停之后这里的STATUS会显示Up about a minute (Paused)表示已暂停,你访问8082也访问不到。
[root@wangazure ~]# docker pause tomcat02 tomcat02 [root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1adf1a9b21a1 tomcat:8.0 "catalina.sh run" 12 minutes ago Up about a minute (Paused) 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat02 643b0adcfd17 tomcat:8.0 "catalina.sh run" 19 minutes ago Up 19 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]#
- 恢复容器
恢复之后Up about a minute (Paused)就消失了,8082也能访问了。
[root@wangazure ~]# docker unpause tomcat02 tomcat02 [root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1adf1a9b21a1 tomcat:8.0 "catalina.sh run" 14 minutes ago Up 3 minutes 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat02 643b0adcfd17 tomcat:8.0 "catalina.sh run" 21 minutes ago Up 21 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]#
- 杀死容器
[root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1adf1a9b21a1 tomcat:8.0 "catalina.sh run" 17 minutes ago Up 5 minutes 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat02 643b0adcfd17 tomcat:8.0 "catalina.sh run" 23 minutes ago Up 23 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]# docker kill tomcat02 tomcat02 [root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 643b0adcfd17 tomcat:8.0 "catalina.sh run" 26 minutes ago Up 26 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]#
- 删除容器
rm如果不行就加-f
[root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1adf1a9b21a1 tomcat:8.0 "catalina.sh run" 20 minutes ago Up 2 seconds 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat02 643b0adcfd17 tomcat:8.0 "catalina.sh run" 27 minutes ago Up 27 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]# docker rm tomcat02 Error response from daemon: You cannot remove a running container 1adf1a9b21a145c5e5d732170260f1f85f2edc38eef38ea1e1a91f791f78a26c. Stop the container before attempting removal or force remove [root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1adf1a9b21a1 tomcat:8.0 "catalina.sh run" 20 minutes ago Up 26 seconds 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat02 643b0adcfd17 tomcat:8.0 "catalina.sh run" 27 minutes ago Up 27 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]# docker rm -f tomcat02 tomcat02 [root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 643b0adcfd17 tomcat:8.0 "catalina.sh run" 27 minutes ago Up 27 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]#
- 删除所有的容器
[root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fec4dc60a4e3 tomcat:8.0 "catalina.sh run" 6 seconds ago Up 6 seconds 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat02 643b0adcfd17 tomcat:8.0 "catalina.sh run" 29 minutes ago Up 29 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]# docker rm -f $(docker ps -aq) fec4dc60a4e3 643b0adcfd17 [root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@wangazure ~]#
- 查看容器运行的日志
[root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2fa97418e5df tomcat:8.0 "catalina.sh run" about a minute ago Up about a minute 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat02 [root@wangazure ~]# docker logs tomcat02 22-Jan-2022 09:19:30.773 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.53 22-Jan-2022 09:19:30.774 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 29 2018 14:42:45 UTC 22-Jan-2022 09:19:30.774 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.53.0 22-Jan-2022 09:19:30.775 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux 22-Jan-2022 09:19:30.775 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-1160.49.1.el7.x86_64 22-Jan-2022 09:19:30.775 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 22-Jan-2022 09:19:30.775 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-7-openjdk-amd64/jre 22-Jan-2022 09:19:30.775 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.7.0_181-b01 22-Jan-2022 09:19:30.775 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation 22-Jan-2022 09:19:30.775 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_base: /usr/local/tomcat 22-Jan-2022 09:19:30.775 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat 22-Jan-2022 09:19:30.775 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties 22-Jan-2022 09:19:30.775 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 22-Jan-2022 09:19:30.776 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048 22-Jan-2022 09:19:30.776 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources 22-Jan-2022 09:19:30.776 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs= 22-Jan-2022 09:19:30.776 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat 22-Jan-2022 09:19:30.776 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat 22-Jan-2022 09:19:30.776 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp 22-Jan-2022 09:19:30.776 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.17 using APR version 1.5.1. 22-Jan-2022 09:19:30.776 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 22-Jan-2022 09:19:30.781 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.1.0f 25 May 2017) 22-Jan-2022 09:19:31.057 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"] 22-Jan-2022 09:19:31.123 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"] 22-Jan-2022 09:19:31.128 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1225 ms 22-Jan-2022 09:19:31.222 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina 22-Jan-2022 09:19:31.223 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.53 22-Jan-2022 09:19:31.269 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/examples 22-Jan-2022 09:19:32.965 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 1,695 ms 22-Jan-2022 09:19:32.965 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/host-manager 22-Jan-2022 09:19:33.065 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 100 ms 22-Jan-2022 09:19:33.066 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/manager 22-Jan-2022 09:19:33.164 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 98 ms 22-Jan-2022 09:19:33.164 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/docs 22-Jan-2022 09:19:33.288 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 123 ms 22-Jan-2022 09:19:33.288 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/ROOT 22-Jan-2022 09:19:33.375 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/ROOT has finished in 86 ms 22-Jan-2022 09:19:33.381 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"] 22-Jan-2022 09:19:33.500 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"] 22-Jan-2022 09:19:33.504 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 2376 ms [root@wangazure ~]#
也可以用docker logs -f id/name(容器的id或名字),用这个命令之后就进去了,想出来就按CTRL+C
- 进入容器内部
之后就可以到容器里了,镜像是不能动的,容器是可以进去的
[root@wangazure ~]# docker run -p 8080:8080 -d --name tomcat01 tomcat:8.0 #开启tomcat01 91ff3bca3aeed284f91326fb09eca09636224bce7055e0691dadf603551461be [root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 91ff3bca3aee tomcat:8.0 "catalina.sh run" 5 seconds ago Up 3 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]# docker exec -it tomcat01 bash #-it进入交互模式最后记得加bash root@91ff3bca3aee:/usr/local/tomcat#
退出容器
root@91ff3bca3aee:/usr/local/tomcat# exit exit [root@wangazure ~]#
- 容器与宿主机文件相互复制拷贝
用vi命令建一个test.html
[root@wangazure ~]# vi test.html #建一个test.html [root@wangazure ~]# cat test.html #查看test.html hello,docker!!! [root@wangazure ~]#
CP命令
[root@wangazure ~]# docker cp test.html tomcat01:/usr/local/tomcat/webapps #复制到webapps里 [root@wangazure ~]# docker exec -it tomcat01 bash #进入到容器里 root@91ff3bca3aee:/usr/local/tomcat# ls LICENSE NOTICE RELEASE-NOTES RUNNING.txt bin conf include lib logs native-jni-lib temp webapps work root@91ff3bca3aee:/usr/local/tomcat# cd webapps/ root@91ff3bca3aee:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager test.html root@91ff3bca3aee:/usr/local/tomcat/webapps# mkdir test #创建test文件 root@91ff3bca3aee:/usr/local/tomcat/webapps# cp test.html test #把test.html复制到test文件夹里 root@91ff3bca3aee:/usr/local/tomcat/webapps# cd test root@91ff3bca3aee:/usr/local/tomcat/webapps/test# ls test.html root@91ff3bca3aee:/usr/local/tomcat/webapps/test#
如果想把容器里的东西复制出来,那就把两个地址一换就好了
[root@wangazure ~]# ls install.sh [root@wangazure ~]# docker cp tomcat01:/usr/local/tomcat/webapps/examples /root [root@wangazure ~]# ls examples install.sh [root@wangazure ~]# cd examples/ [root@wangazure examples]# ls index.html jsp servlets WEB-INF websocket [root@wangazure examples]#5、查看所有容器
[root@wangazure ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1adf1a9b21a1 tomcat:8.0 "catalina.sh run" 17 minutes ago Up 5 minutes 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat02 643b0adcfd17 tomcat:8.0 "catalina.sh run" 23 minutes ago Up 23 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]# docker ps -aq 1adf1a9b21a1 643b0adcfd17 [root@wangazure ~]# docker ps -qa 1adf1a9b21a1 643b0adcfd17 [root@wangazure ~]# docker ps -a ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1adf1a9b21a1 tomcat:8.0 "catalina.sh run" 16 minutes ago Up 4 minutes 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat02 643b0adcfd17 tomcat:8.0 "catalina.sh run" 22 minutes ago Up 22 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01 [root@wangazure ~]#
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)