美文网首页
Docker 安装nginx和tomcat

Docker 安装nginx和tomcat

作者: 平头哥2 | 来源:发表于2019-03-05 16:47 被阅读0次

1. Docker 安装Nginx

[root@langzi01 nginx]# pwd
/data/nginx
[root@langzi01 nginx]# ls
conf  conf.d  html  logs

[root@langzi01 nginx]# docker search nginx
[root@langzi01 nginx]# docker pull nginx:1.14.2
[root@langzi01 nginx]# docker images nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx     1.14.2              e706cb01fa6b        2 hours ago         109 MB

##运行容器
[root@langzi01 nginx]# docker run --name mynginx -d -p 82:80  -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/logs:/var/log/nginx -d docker.io/nginx
命令说明:

-p 80:80:将容器的80端口映射到主机的80端口

--name mynginx:将容器命名为mynginx

## -v $PWD/www:/www:将主机中当前目录下的www挂载到容器的/www

-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:将主机中当前目录下的nginx.conf挂载到容器的/etc/nginx/nginx.conf

-v /data/nginx/logs:/var/log/nginx:将主机中当前目录下的logs挂载到容器的/var/log/nginx

其中nginx.conf的内容如下

[root@langzi01 nginx]# cat conf/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  localhost;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_pass http://pic; 
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

    upstream pic{
               server 172.17.0.1:8088 weight=5;
               server 172.17.0.1:8089 weight=5;
    }

}

2. Docker 安装Tomcat

[root@langzi01 tomcat]# pwd
/data/tomcat
[root@langzi01 tomcat]# ls
conf  logs  test  webapps

[root@langzi01 test]# docker images|grep tomcat
docker.io/tomcat    8.5                 168588387c68        3 weeks ago         463 MB
docker.io/tomcat    latest              168588387c68        3 weeks ago         463 MB

#启动两个tomcat服务
[root@langzi01 test]# docker run --name tomcat1 -p 8088:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat
[root@langzi01 test]# docker run --name tomcat2 -p 8089:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat

[root@langzi01 test]# docker ps -a|grep tomcat
4714c3831de7        tomcat              "catalina.sh run"        41 minutes ago      Up 41 minutes       0.0.0.0:8089->8080/tcp   tomcat2
9b01100cf846        tomcat              "catalina.sh run"        41 minutes ago      Up 41 minutes       0.0.0.0:8088->8080/tcp   tomcat1

# 访问
[root@langzi01 test]# curl '172.17.0.1:8088'
[root@langzi01 test]# curl '172.17.0.1:8089'

3. nginx代理tomcat服务器

upstream pic{
               server 172.17.0.1:8088 weight=5;
               server 172.17.0.1:8089 weight=5;
}

查看nginx的端口

[root@langzi01 test]# docker ps -a|grep nginx
cce508aa20bc        docker.io/nginx     "nginx -g 'daemon ..."   About an hour ago   Up 58 minutes       0.0.0.0:82->80/tcp       mynginx

我的公网ip为:http://47.104.136.162 浏览器访问:http://47.104.136.162:82/

结果如下:


捕获.PNG

相关文章

网友评论

      本文标题:Docker 安装nginx和tomcat

      本文链接:https://www.haomeiwen.com/subject/xoeouqtx.html