Docker端口+数据卷
1)、暴露端口
1.在不暴露端口的情况下启动tomcat
docker run -d --name mTM01 tomcat
通过宿主机IP+localhost访问,访问会被拒绝,原因是没有向外暴露端口
2.docker 在启动容器的时候可以添加 ‘-p’ 命令来指定暴露端口
docker run -p xxxx:服务端端口(例如tomcat 8080) --name (自定义名称) 容器镜像:[TAG]
通过访问暴露端口就可以访问到tomcat了(关于访问tomcat404原理+解决方案,请戳这里【传送门】)
3. 使用curl localhost:暴露端口号 命令,返回HTML格式即暴露成功
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Apache Tomcat/9.0.35</title>
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<link href="favicon.ico" rel="shortcut 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/9.0.35</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 src="tomcat.png" 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>
......
补充:如果失败了会提示 Connection refused
[root@localhost /]# curl localhost:7777
curl: (7) Failed connect to localhost:7777; Connection refused
2)、容器数据卷使用
说明:可以帮助我们映射容器中的目录到宿主机上,因为docker容器中的vim命令是没有的,所以每次构建一个容器就要重新更新和下载vim命令,这样就很麻烦,通过容器数据卷的使用以后,可以直接通过修改宿主机上的文件来修改容器中的文件,这样就不需要再进入容器中操作了
1.docker 在启动容器的时候配置 -v Source(宿主机目录):Destination(容器路径)启动即可映射成功
docker run -d -p xxxx:80 -v /nginx/home:/etc/nginx--name mNG01 nginx
2.创建之后可以查看里面映射是否成功,在其中查看到这些内容就算成功
docker inspect 容器ID
3.测试同步效果
#宿主机(nginx/conf)创建一个test.java文件
[root@localhost /]# cd nginx
[root@localhost nginx]# ls
conf
[root@localhost nginx]# cd conf
[root@localhost conf]# ls
[root@localhost conf]# touch test.java
[root@localhost conf]# ls
test.java
#容器内部的home路径下也同步更新
[root@localhost ~]# docker exec -it 62b4ba25edb7 /bin/bash
root@62b4ba25edb7:/# docker ps
bash: docker: command not found
root@62b4ba25edb7:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@62b4ba25edb7:/# cd /home
root@62b4ba25edb7:/home# ls
test.java
通过小编的不断学习,会继续完善数据卷的使用