微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Docker部署Nginx及Tomcat

部署Nginx

1.下载镜像:docker pull Nginx

2.运行测试:

[root@iZwz9g94s1ylux707g0qu3Z /]# docker images    #查看镜像
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
MysqL         5.7       a70d36bc331a   3 weeks ago     449MB
MysqL         latest    c8562eaf9d81   3 weeks ago     546MB
Nginx         latest    f6d0b4767a6c   4 weeks ago     133MB
centos        latest    300e315adb2f   2 months ago    209MB
hello-world   latest    bf756fb1ae65   13 months ago   13.3kB
[root@iZwz9g94s1ylux707g0qu3Z /]# docker run -d --name Nginx01 -p 3344:80 Nginx   #运行测试
afd1aa8bc5f9326b745a03abfb64da79113470c32460fdb0c48beda28dd70736
[root@iZwz9g94s1ylux707g0qu3Z /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         POR
afd1aa8bc5f9   Nginx     "/docker-entrypoint.…"   5 seconds ago   Up 4 seconds   0.0
[root@iZwz9g94s1ylux707g0qu3Z /]# curl localhost:3344    #本机测试
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Nginx!</title>
<style>
    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>


重点是:docker run -d --name Nginx01 -p 3344:80 Nginx
其中-p 3344:80意思是 -p 宿主机端口:容器内部端口

端口暴露理解:

在这里插入图片描述

首先外网访问时,首先验证阿里云的安全组,通过之后,再进行防火墙的验证,之后就进入到了linux,为了能从linux访问到docker的容器,这个时候就需要暴露端口:外面的3344和容器的80,这个时候,就可以通过外网访问3344来访问到容器的80了,这有点像是映射的关系!

在这里插入图片描述

那如何进入容器呢?
docker exec -it Nginx01 /bin/bash
进入之后,可以通过:whereis Nginx01 来查看相应的配置文件位置

[root@iZwz9g94s1ylux707g0qu3Z /]# docker exec -it Nginx01 /bin/bash
root@afd1aa8bc5f9:/# whereis Nginx01
Nginx01:
root@afd1aa8bc5f9:/# whereis Nginx
Nginx: /usr/sbin/Nginx /usr/lib/Nginx /etc/Nginx /usr/share/Nginx
root@afd1aa8bc5f9:/usr/share/Nginx# cd /etc/Nginx   #html页面就存在这个/Nginx下面
root@afd1aa8bc5f9:/etc/Nginx# ls
conf.d		koi-utf  mime.types  Nginx.conf   uwsgi_params
fastcgi_params	koi-win  modules     scgi_params  win-utf

部署tomcat

1.下载镜像:docker pull tomcat

2.运行测试:docker run -d -p 3355:8080 --name tomcat01 tomcat(安全组和防火墙要开放3355端口)。

这个时候发现,通过外网访问3355端口时,出现404错误

进入tomcat容器查看:docker exec -it tomcat01 /bin/bash

[root@iZwz9g94s1ylux707g0qu3Z home]# docker exec -it tomcat01 /bin/bash
root@78de0b9024e0:/usr/local/tomcat# ls
BUILDING.txt	 NOTICE		RUNNING.txt  lib	     temp	   work
CONTRIBUTING.md  README.md	bin	     logs	     webapps
LICENSE		 RELEASE-NOTES	conf	     native-jni-lib  webapps.dist
root@78de0b9024e0:/usr/local/tomcat# ls -al
total 128
drwxr-xr-x 1 root root    30 Jan 13 08:25 .
drwxr-xr-x 1 root root    20 Jan 13 08:19 ..
-rw-r--r-- 1 root root 18982 Dec  3 11:48 BUILDING.txt
-rw-r--r-- 1 root root  5409 Dec  3 11:48 CONTRIBUTING.md
-rw-r--r-- 1 root root 57092 Dec  3 11:48 LICENSE
-rw-r--r-- 1 root root  2333 Dec  3 11:48 NOTICE
-rw-r--r-- 1 root root  3257 Dec  3 11:48 README.md
-rw-r--r-- 1 root root  6898 Dec  3 11:48 RELEASE-NOTES
-rw-r--r-- 1 root root 16507 Dec  3 11:48 RUNNING.txt
drwxr-xr-x 2 root root  4096 Jan 13 08:25 bin
drwxr-xr-x 1 root root    40 Feb 13 09:41 conf
drwxr-xr-x 2 root root  4096 Jan 13 08:25 lib
drwxrwxrwx 1 root root   177 Feb 13 09:41 logs
drwxr-xr-x 2 root root   134 Jan 13 08:25 native-jni-lib
drwxrwxrwx 2 root root    30 Jan 13 08:25 temp
drwxr-xr-x 2 root root     6 Jan 13 08:25 webapps
drwxr-xr-x 7 root root    81 Dec  3 11:45 webapps.dist
drwxrwxrwx 2 root root     6 Dec  3 11:43 work
root@78de0b9024e0:/usr/local/tomcat# cd webapps
root@78de0b9024e0:/usr/local/tomcat/webapps# ls    #webapps目录下没有东西
root@78de0b9024e0:/usr/local/tomcat/webapps# 

发现存放项目的/tomcat/webapps文件夹下没有东西!这也是导致出现404错误的原因。

解决:只需要把 /webapps.dist文件下的东西拷贝到/webapps文件下就可以访问tomcat了

root@78de0b9024e0:/usr/local/tomcat# cd webapps.dist
root@78de0b9024e0:/usr/local/tomcat/webapps.dist# ls
ROOT  docs  examples  host-manager  manager
root@78de0b9024e0:/usr/local/tomcat/webapps.dist# cd ..
root@78de0b9024e0:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@78de0b9024e0:/usr/local/tomcat# cd webapps
root@78de0b9024e0:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager

如果自己写项目的话,只需要自己在/webapps文件下改就可以了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐