Docker应用部署(Nginx、Tomcat)

2022/7/23 5:25:23

本文主要是介绍Docker应用部署(Nginx、Tomcat),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Docker hub官方链接: https://hub.docker.com

部署Nginx

官方已经给出了方法: https://hub.docker.com/_/nginx

运行容器

$ docker run --name nginx-server -v /opt/nginx-server:/usr/share/nginx/html:ro -d nginx

使用--name为容器命名,使用-v将指定的宿主机的目录挂载到指定的容器中的目录,并且赋予一个read-only权限,使用-d则是创建为守护进程。如果本地并没有该容器,那么Docker会自动下载镜像,如果没有显式地指定tag,则默认使用latest

$ sudo docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
ff6b4d326548   nginx     "/docker-entrypoint.…"   18 seconds ago   Up 17 seconds   80/tcp    nginx-server

使用docker ps查看运行的容器

$ sudo docker inspect ff6 | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

使用docker inspect查看容器信息并进行匹配,得到容器的IP地址

$ curl http://172.17.0.2
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.23.1</center>
</body>
</html>

使用curl发起http请求,发现403禁止访问,因为nginx还未配置首页html文件,向上述挂载的宿主机目录下写入一个html文件

$ sudo echo "nginx is working" > /opt/nginx-server/index.html

再次访问:

$ curl http://172.17.0.2
nginx is working

暴露端口

仅仅通过上述流程的话,只有宿主机之外的机器是没办法访问容器的,这时使用端口映射,将宿主机的端口映射到容器的端口上

$ sudo docker run -d -p 8080:80 --name nginx-server-port -v /opt/nginx-server-port:/usr/share/html:ro nginx

使用-p建立端口映射,使用docker ps可以查看到端口信息:

$ sudo docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
1a64b2782138   nginx     "/docker-entrypoint.…"   13 minutes ago   Up 13 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx-server-port

访问通过宿主机的IP:端口就可以访问(如果防火墙没有拦截)

配置文件

启动容器:

$ sudo docker run -d -p 8080:80 --name nginx-server-port -v /opt/nginx-server-port:/usr/share/html:ro nginx
cd94235a9a4d937887b83ff7ff2dcc4323d03db9bea95a145b9314fcba43bf5f

使用docker top查看容器中进程:

$ sudo docker top cd9
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                16969               16948               0                   23:16               ?                   00:00:00            nginx: master process nginx -g daemon off;
systemd+            17037               16969               0                   23:16               ?                   00:00:00            nginx: worker process
systemd+            17038               16969               0                   23:16               ?                   00:00:00            nginx: worker process
systemd+            17039               16969               0                   23:16               ?                   00:00:00            nginx: worker process
systemd+            17040               16969               0                   23:16               ?                   00:00:00            nginx: worker process
systemd+            17041               16969               0                   23:16               ?                   00:00:00            nginx: worker process
...

拷贝文件

现在将容器中的Nginx的配置文件拷贝出来

$ sudo mkdir /opt/nginxcon
$ sudo docker cp nginx-server-port:/etc/nginx/nginx.conf /opt/nginxcon/ 

如上所示拷贝到了nginxcon目录下,使用cat查看

$ sudo cat /opt/nginxcon/nginx.conf 

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

写入文件

将配置文件的worker_processes改成2,尝试写回文件

挂载配置文件并且启动容器:

$ sudo docker run -d \
> -p 9090:80 --name nginx-server-conf \
> -v /opt/nginx-server-conf:/usr/share/nginx/html:ro \
> -v /opt/nginxcon/nginx.conf:/etc/nginx/nginx.conf:ro \
> nginx

使用-p建立端口映射,-v挂载目录,此处将配置文件和HTML目录都进行挂载

使用top命令查看:

$ sudo docker top ac4
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                18903               18882               0                   23:32               ?                   00:00:00            nginx: master process nginx -g daemon off;
systemd+            18968               18903               0                   23:32               ?                   00:00:00            nginx: worker process
systemd+            18969               18903               0                   23:32               ?                   00:00:00            nginx: worker process

确实只有两个worker process了

在容器中执行一个cat命令也可以查看:

$ sudo docker exec -it nginx-server-conf cat /etc/nginx/nginx.conf

user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}
...

部署Tomcat

官方: https://hub.docker.com/_/tomcat

不暴露端口安装

$ sudo docker run -d --rm tomcat:9.0

直接一个命令进行安装并启动,--rm表示容器停止后就删除这个容器,可以看到容器正在运行:

$ sudo docker ps
CONTAINER ID   IMAGE        COMMAND             CREATED              STATUS              PORTS      NAMES
3fbae2e0fd13   tomcat:9.0   "catalina.sh run"   About a minute ago   Up About a minute   8080/tcp   gifted_ptolemy

查看IP地址:

$ sudo docker inspect 3fb | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

使用curl访问:

$ curl http://172.17.0.2:8080
<!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/9.0.64</h3></body></html>

页面返回404 Not Found,因为还未创建网站首页,页面没有被发现

暴露端口并添加网页

同样可以使用-p进行端口映射,将宿主机端口映射到容器上,这时通过主机IP地址就可以访问

$ sudo docker run -d -p 8080:8080 -v /opt/tomcat-server:/usr/local/tomcat/webapps/ROOT tomcat:9.0
b5c3ba3f2e94d7fe5d3dd54fa075c3d10b225483e98f2f54976feb744e69a52d

将8080端口进行映射,并且进行目录的挂载

echo "tomcat running" > /opt/tomcat-server/index.html

向宿主机挂载目录写入信息

$ curl http://127.0.0.1:8080
tomcat running

得到了正常的页面



这篇关于Docker应用部署(Nginx、Tomcat)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程