上一篇Blog详细介绍了如何在CentOS上进行Docker的安装、卸载以及如何进行镜像加速,了解了Docker大致的运行流程以及常用的命令。时隔半个月之后,度过了过节失落期后再次拾起来自己的年度计划继续进行这方面知识的学习。在了解了常用命令后,就要开始对常用容器下手训练了,比如一些常用的中间件以及服务器。
常用容器部署
通过上一篇Blog命令的学习,来运行一下以下几个常用的中间件和服务器的容器。
Nginx部署
Nginx常用做负载均衡和反向代理,我们通过Docker来使用容器化的Nginx,从容器站点搜索:https://hub.docker.com/:
1 搜索并下载镜像
搜索到nginx镜像后进行下载
Last login: Fri Feb 4 00:32:05 2022 from 192.168.5.1 [root@192 ~]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED bitnami/nginx Bitnami nginx Docker Image 117 [OK] bitnami/wordpress-nginx Bitnami Docker Image for WordPress with NGINX 55 [OK] ubuntu/nginx Nginx, a high-performance reverse proxy & we… 31 bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 15 [OK] rancher/nginx-ingress-controller 9 ibmcom/nginx-ingress-controller Docker Image for IBM Cloud Private-CE (Commu… 4 bitnami/nginx-ldap-auth-daemon 3 rancher/nginx-ingress-controller-defaultbackend 2 circleci/nginx This image is for internal use 2 bitnami/nginx-exporter 1 rancher/nginx 1 rancher/nginx-ingress-controller-amd64 0 kasmweb/nginx An Nginx image based off nginx:alpine and in… 0 rancher/nginx-conf 0 rancher/nginx-ssl 0 ibmcom/nginx-ppc64le Docker image for nginx-ppc64le 0 wallarm/nginx-ingress-controller Kubernetes Ingress Controller with Wallarm e… 0 ibmcom/nginx-ingress-controller-ppc64le Docker Image for IBM Cloud Private-CE (Commu… 0 rancher/nginx-proxy 0 bitnami/nginx-intel 0 wallarm/nginx-ingress-controller-amd64 Kubernetes Ingress Controller with Wallarm e… 0 ibmcom/nginx-ingress-controller-amd64 0 ibmcom/nginx-ingress-controller-s390x 0 wallarm/nginx-amd64 0 nginx Official build of Nginx. 0 [OK] [root@192 ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx a2abf6c4d29d: Pull complete a9edb18cadd1: Pull complete 589b7251471a: Pull complete 186b1aaa4aa6: Pull complete b4df32aa5a72: Pull complete a0bcbecc962e: Pull complete Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 7 weeks ago 141MB tomcat latest fb5657adc892 8 weeks ago 680MB mysql latest 3218b38490ce 2 months ago 516MB hello-world latest feb5d9fea6a5 4 months ago 13.3kB centos latest 5d0da3dc9764 5 months ago 231MB [root@192 ~]#
2 运行测试
通过后台模式运行nginx容器
docker run -d --name nginx-tml -p 3334:80 nginx -d 后台运行 --name 给容器命名 -p 3334:80 将宿主机的端口3334映射到该容器的80端口
如果在run的时候报了如下异常:
WARNING: IPv4 forwarding is disabled. Networking will not work.
则说明ip转发没有打开,需要通过如下方式打开:
vim /etc/sysctl.conf #配置转发 net.ipv4.ip_forward=1 #重启服务,让配置生效 systemctl restart network #查看是否成功,如果返回为“net.ipv4.ip_forward = 1”则表示成功 sysctl net.ipv4.ip_forward
运行命令如下:
[root@192 ~]# docker run -d --name nginx-tml -p 3334:80 nginx b8ae778bad8c7b53df465ee033f14b909ac0096306d38a39ea5c211910613aa0 [root@192 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b8ae778bad8c nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:3334->80/tcp, :::3334->80/tcp nginx-tml 4ed9be7f96c8 centos "/bin/bash" 2 weeks ago Up 2 weeks inspiring_rhodes cc886973b2cb centos "/bin/sh -c 'while t…" 2 weeks ago Up 2 weeks suspicious_borg 1c1dd47ce82c centos "/bin/bash" 2 weeks ago Up 2 weeks elated_colden 945abcb48a14 tomcat "catalina.sh run" 2 weeks ago Up 2 weeks 8080/tcp quizzical_golick 0558e745b9b0 tomcat "catalina.sh run" 2 weeks ago Up 2 weeks 8080/tcp heuristic_chebyshev
宿主机与容器的端口暴露映射
3 访问测试
通过公网网络端口访问nginx
[root@192 ~]# curl localhost:3334 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
通过公网也是可以访问到的:
Tomcat部署
Tomcat是我们常用的服务器,同样可以从dockerhub搜索到:
1 搜索并下载镜像
之前虽然下载过tomcat镜像和容器,这里就复习下之前的命令,删除容器和镜像重新下载吧:
[root@192 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b8ae778bad8c nginx "/docker-entrypoint.…" 31 minutes ago Up 31 minutes 0.0.0.0:3334->80/tcp, :::3334->80/tcp nginx-tml 4ed9be7f96c8 centos "/bin/bash" 2 weeks ago Up 2 weeks inspiring_rhodes cc886973b2cb centos "/bin/sh -c 'while t…" 2 weeks ago Up 2 weeks suspicious_borg 1c1dd47ce82c centos "/bin/bash" 2 weeks ago Up 2 weeks elated_colden 945abcb48a14 tomcat "catalina.sh run" 2 weeks ago Up 2 weeks 8080/tcp quizzical_golick 0558e745b9b0 tomcat "catalina.sh run" 2 weeks ago Up 2 weeks 8080/tcp heuristic_chebyshev [root@192 ~]# docker rm -f 945abcb48a14 945abcb48a14 [root@192 ~]# docker rm -f 0558e745b9b0 0558e745b9b0 [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 7 weeks ago 141MB tomcat latest fb5657adc892 8 weeks ago 680MB mysql latest 3218b38490ce 2 months ago 516MB hello-world latest feb5d9fea6a5 4 months ago 13.3kB centos latest 5d0da3dc9764 5 months ago 231MB [root@192 ~]# docker rmi -f fb5657adc892 Untagged: tomcat:latest Untagged: tomcat@sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324 Deleted: sha256:fb5657adc892ed15910445588404c798b57f741e9921ff3c1f1abe01dbb56906 [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 7 weeks ago 141MB mysql latest 3218b38490ce 2 months ago 516MB hello-world latest feb5d9fea6a5 4 months ago 13.3kB centos latest 5d0da3dc9764 5 months ago 231MB
然后再通过命令重新搜索下载tomcat镜像
[root@192 ~]# docker search tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED bitnami/tomcat Bitnami Tomcat Docker Image 44 [OK] aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 12 [OK] arm64v8/tomcat Apache Tomcat is an open source implementati… 7 rightctrl/tomcat CentOS , Oracle Java, tomcat application ssl… 7 [OK] tomcat2111/pisignage-server PiSignage Server 3 [OK] jelastic/tomcat An image of the Tomcat Java application serv… 3 amd64/tomcat Apache Tomcat is an open source implementati… 3 cfje/tomcat-resource Tomcat Concourse Resource 2 chenyufeng/tomcat-centos tomcat基于centos6的镜像 1 [OK] ppc64le/tomcat Apache Tomcat is an open source implementati… 1 tomcatling/jupyterhub_aws 1 softwareplant/tomcat Tomcat images for jira-cloud testing 0 [OK] store/microsoft/defaultpublisher Zulu for Azure build of OpenJDK 0 tomcat2111/phpredisadmin This is a Docker image for phpredisadmin 0 [OK] tomcat2111/bitbucket-pipelines-elasticsearch Elasticsearch for Bitbucket's Pipelines 0 tomcat2111/papercut-mf PaperCut MF Application Server 0 tomcat0823/auto1 0 tomcatengineering/pg_backup_rotated Clone of martianrock/pg_backup_rotated but w… 0 s390x/tomcat Apache Tomcat is an open source implementati… 0 tomcatengineering/docker_swarm_exporter Prometheus metrics exporter for Docker Swarms 0 secoresearch/tomcat-varnish Tomcat and Varnish 5.0 0 [OK] tomcat2111/piwik Matomo (formerly Piwik) image 0 tomee Apache TomEE is an all-Apache Java EE certif… 0 [OK] tomcat2111/redaxo Redaxo 0 tomcat Apache Tomcat is an open source implementati… 0 [OK] [root@192 ~]# docker pull tomcat Using default tag: latest latest: Pulling from library/tomcat 0e29546d541c: Already exists 9b829c73b52b: Already exists cb5b7ae36172: Already exists 6494e4811622: Already exists 668f6fcc5fa5: Already exists dc120c3e0290: Already exists 8f7c0eebb7b1: Already exists 77b694f83996: Already exists 0f611256ec3a: Already exists 4f25def12f23: Already exists Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324 Status: Downloaded newer image for tomcat:latest docker.io/library/tomcat:latest [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 7 weeks ago 141MB tomcat latest fb5657adc892 8 weeks ago 680MB mysql latest 3218b38490ce 2 months ago 516MB hello-world latest feb5d9fea6a5 4 months ago 13.3kB centos latest 5d0da3dc9764 5 months ago 231MB
2 运行测试
然后以后台的方式启动下tomcat:
[root@192 ~]# docker run -d --name tomcat-tml -p 3335:8080 tomcat 2f59536a92da2d8212bb767da507b3b76102bc47df241c6046c866f0ccfeac7c [root@192 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2f59536a92da tomcat "catalina.sh run" 8 seconds ago Up 6 seconds 0.0.0.0:3335->8080/tcp, :::3335->8080/tcp tomcat-tml b8ae778bad8c nginx "/docker-entrypoint.…" 38 minutes ago Up 38 minutes 0.0.0.0:3334->80/tcp, :::3334->80/tcp nginx-tml 4ed9be7f96c8 centos "/bin/bash" 2 weeks ago Up 2 weeks inspiring_rhodes cc886973b2cb centos "/bin/sh -c 'while t…" 2 weeks ago Up 2 weeks suspicious_borg 1c1dd47ce82c centos "/bin/bash" 2 weeks ago Up 2 weeks elated_colden [root@192 ~]# curl localhost:3335 <!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.0.14</h3></body></html>[root@192 ~]#
这里看到是404,这是因为:阿里云镜像默认下载的是最小的镜像,保证最小的运行环境,容器中的命令是少了
oot@192 ~]# docker exec -it tomcat-tml /bin/bash root@2f59536a92da:/usr/local/tomcat# ls BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs native-jni-lib temp webapps webapps.dist work root@2f59536a92da:/usr/local/tomcat# cd webapps.dist root@2f59536a92da:/usr/local/tomcat/webapps.dist# ls ROOT docs examples host-manager manager root@2f59536a92da:/usr/local/tomcat/webapps.dist# cd ROOT root@2f59536a92da:/usr/local/tomcat/webapps.dist/ROOT# ls RELEASE-NOTES.txt WEB-INF asf-logo-wide.svg bg-button.png bg-middle.png bg-nav.png bg-upper.png favicon.ico index.jsp tomcat.css tomcat.svg root@2f59536a92da:/usr/local/tomcat/webapps.dist/ROOT# ls RELEASE-NOTES.txt WEB-INF asf-logo-wide.svg bg-button.png bg-middle.png bg-nav.png bg-upper.png favicon.ico index.jsp tomcat.css tomcat.svg root@2f59536a92da:/usr/local/tomcat/webapps.dist/ROOT# cd ../../ root@2f59536a92da:/usr/local/tomcat# cd webapps root@2f59536a92da:/usr/local/tomcat/webapps# ls root@2f59536a92da:/usr/local/tomcat/webapps# cp -r /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps/ root@2f59536a92da:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager root@2f59536a92da:/usr/local/tomcat/webapps# exit exit
3 访问测试
再次访问测试查看:
[root@192 ~]# curl localhost:3335 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Apache Tomcat/10.0.14</title> <link href="favicon.ico" rel="icon" type="image/x-icon" /> <link href="tomcat.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper"> <div id="navigation" class="curved container"> <span id="nav-home"><a href="https://tomcat.apache.org/">Home</a></span> <span id="nav-hosts"><a href="/docs/">Documentation</a></span> <span id="nav-config"><a href="/docs/config/">Configuration</a></span> <span id="nav-examples"><a href="/examples/">Examples</a></span> <span id="nav-wiki"><a href="https://wiki.apache.org/tomcat/FrontPage">Wiki</a></span> <span id="nav-lists"><a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></span> <span id="nav-help"><a href="https://tomcat.apache.org/findhelp.html">Find Help</a></span> <br class="separator" /> </div> <div id="asf-box"> <h1>Apache Tomcat/10.0.14</h1> </div> <div id="upper" class="curved container"> <div id="congrats" class="curved container"> <h2>If you're seeing this, you've successfully installed Tomcat. Congratulations!</h2> </div> <div id="notice"> <img id="tomcat-logo" src="tomcat.svg" alt="[tomcat logo]" /> <div id="tasks"> <h3>Recommended Reading:</h3> <h4><a href="/docs/security-howto.html">Security Considerations How-To</a></h4> <h4><a href="/docs/manager-howto.html">Manager Application How-To</a></h4> <h4><a href="/docs/cluster-howto.html">Clustering/Session Replication How-To</a></h4> </div> </div> <div id="actions"> <div class="button"> <a class="container shadow" href="/manager/status"><span>Server Status</span></a> </div> <div class="button"> <a class="container shadow" href="/manager/html"><span>Manager App</span></a> </div> <div class="button"> <a class="container shadow" href="/host-manager/html"><span>Host Manager</span></a> </div> </div> <br class="separator" /> </div> <div id="middle" class="curved container"> <h3>Developer Quick Start</h3> <div class="col25"> <div class="container"> <p><a href="/docs/setup.html">Tomcat Setup</a></p> <p><a href="/docs/appdev/">First Web Application</a></p> </div> </div> <div class="col25"> <div class="container"> <p><a href="/docs/realm-howto.html">Realms & AAA</a></p> <p><a href="/docs/jndi-datasource-examples-howto.html">JDBC DataSources</a></p> </div> </div> <div class="col25"> <div class="container"> <p><a href="/examples/">Examples</a></p> </div> </div> <div class="col25"> <div class="container"> <p><a href="https://wiki.apache.org/tomcat/Specifications">Servlet Specifications</a></p> <p><a href="https://wiki.apache.org/tomcat/TomcatVersions">Tomcat Versions</a></p> </div> </div> <br class="separator" /> </div> <div id="lower"> <div id="low-manage" class=""> <div class="curved container"> <h3>Managing Tomcat</h3> <p>For security, access to the <a href="/manager/html">manager webapp</a> is restricted. Users are defined in:</p> <pre>$CATALINA_HOME/conf/tomcat-users.xml</pre> <p>In Tomcat 10.0 access to the manager application is split between different users. <a href="/docs/manager-howto.html">Read more...</a></p> <br /> <h4><a href="/docs/RELEASE-NOTES.txt">Release Notes</a></h4> <h4><a href="/docs/changelog.html">Changelog</a></h4> <h4><a href="https://tomcat.apache.org/migration.html">Migration Guide</a></h4> <h4><a href="https://tomcat.apache.org/security.html">Security Notices</a></h4> </div> </div> <div id="low-docs" class=""> <div class="curved container"> <h3>Documentation</h3> <h4><a href="/docs/">Tomcat 10.0 Documentation</a></h4> <h4><a href="/docs/config/">Tomcat 10.0 Configuration</a></h4> <h4><a href="https://wiki.apache.org/tomcat/FrontPage">Tomcat Wiki</a></h4> <p>Find additional important configuration information in:</p> <pre>$CATALINA_HOME/RUNNING.txt</pre> <p>Developers may be interested in:</p> <ul> <li><a href="https://tomcat.apache.org/bugreport.html">Tomcat 10.0 Bug Database</a></li> <li><a href="/docs/api/index.html">Tomcat 10.0 JavaDocs</a></li> <li><a href="https://github.com/apache/tomcat/tree/10.0.x">Tomcat 10.0 Git Repository at GitHub</a></li> </ul> </div> </div> <div id="low-help" class=""> <div class="curved container"> <h3>Getting Help</h3> <h4><a href="https://tomcat.apache.org/faq/">FAQ</a> and <a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></h4> <p>The following mailing lists are available:</p> <ul> <li id="list-announce"><strong><a href="https://tomcat.apache.org/lists.html#tomcat-announce">tomcat-announce</a><br /> Important announcements, releases, security vulnerability notifications. (Low volume).</strong> </li> <li><a href="https://tomcat.apache.org/lists.html#tomcat-users">tomcat-users</a><br /> User support and discussion </li> <li><a href="https://tomcat.apache.org/lists.html#taglibs-user">taglibs-user</a><br /> User support and discussion for <a href="https://tomcat.apache.org/taglibs/">Apache Taglibs</a> </li> <li><a href="https://tomcat.apache.org/lists.html#tomcat-dev">tomcat-dev</a><br /> Development mailing list, including commit messages </li> </ul> </div> </div> <br class="separator" /> </div> <div id="footer" class="curved container"> <div class="col20"> <div class="container"> <h4>Other Downloads</h4> <ul> <li><a href="https://tomcat.apache.org/download-connectors.cgi">Tomcat Connectors</a></li> <li><a href="https://tomcat.apache.org/download-native.cgi">Tomcat Native</a></li> <li><a href="https://tomcat.apache.org/taglibs/">Taglibs</a></li> <li><a href="/docs/deployer-howto.html">Deployer</a></li> </ul> </div> </div> <div class="col20"> <div class="container"> <h4>Other Documentation</h4> <ul> <li><a href="https://tomcat.apache.org/connectors-doc/">Tomcat Connectors</a></li> <li><a href="https://tomcat.apache.org/connectors-doc/">mod_jk Documentation</a></li> <li><a href="https://tomcat.apache.org/native-doc/">Tomcat Native</a></li> <li><a href="/docs/deployer-howto.html">Deployer</a></li> </ul> </div> </div> <div class="col20"> <div class="container"> <h4>Get Involved</h4> <ul> <li><a href="https://tomcat.apache.org/getinvolved.html">Overview</a></li> <li><a href="https://tomcat.apache.org/source.html">Source Repositories</a></li> <li><a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></li> <li><a href="https://wiki.apache.org/tomcat/FrontPage">Wiki</a></li> </ul> </div> </div> <div class="col20"> <div class="container"> <h4>Miscellaneous</h4> <ul> <li><a href="https://tomcat.apache.org/contact.html">Contact</a></li> <li><a href="https://tomcat.apache.org/legal.html">Legal</a></li> <li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li> <li><a href="https://www.apache.org/foundation/thanks.html">Thanks</a></li> </ul> </div> </div> <div class="col20"> <div class="container"> <h4>Apache Software Foundation</h4> <ul> <li><a href="https://tomcat.apache.org/whoweare.html">Who We Are</a></li> <li><a href="https://tomcat.apache.org/heritage.html">Heritage</a></li> <li><a href="https://www.apache.org">Apache Home</a></li> <li><a href="https://tomcat.apache.org/resources.html">Resources</a></li> </ul> </div> </div> <br class="separator" /> </div> <p class="copyright">Copyright ©1999-2022 Apache Software Foundation. All Rights Reserved</p> </div> </body> </html> [root@192 ~]#
公网访问