Docker Compose部署 nginx代理Tomcat集群

一、简介

  使用Docker镜像部署Nginx代理的多个Tomcat集群:

  • 使用Dockerfile构建nginx镜像
  • 使用Dockerfile构建tomcat镜像
  • mysql镜像使用docker hub 镜像

  1.1 结构

docker-compose.yml 
etc
└── localtime
mysql
├── conf
│   └── my.cnf
└── mysqldb
nginx
├── conf.d
│   └── test.conf
├── Dockerfile
├── logs
├── nginx-1.12.1.tar.gz
└── nginx.conf
tomcat
├── apache-tomcat-8.5.12.tar.gz
├── Dockerfile
├── jdk-8u121-linux-x64.tar.gz
└── server.xml
webapps

# webapps 用于放置tomcat项目使用

# mysqldb 数据将会存放在 mysql/mysqldb中

  

二、部署和构建

  2.1 Nginx Dockerfile

FROM centos:latest
MAINTAINER bigberg
RUN yum -y install pcre-devel openssl-devel net-tools gcc gcc-c++ zlib zlib-devel \
    make openssl
ADD nginx-1.12.1.tar.gz /tmp/
RUN cd /tmp/nginx-1.12.1 \
    && ./configure --prefix=/usr/local/nginx \
     --with-http_ssl_module \
     --with-http_gzip_static_module \
     --with-http_realip_module \
    && make && make install
RUN mkdir -p /usr/local/nginx/logs \
    && groupadd -g 1001 nginx \
    && useradd -g 1001 -u 1001 -s /sbin/nologin -M nginx

CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
nginx dockerfile

  2.2 Tomcat Dockerfile

FROM centos
MAINTAINER bigberg

ADD jdk-8u121-linux-x64.tar.gz /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0_121
ADD apache-tomcat-8.5.12.tar.gz /usr/local
COPY server.xml /usr/local/apache-tomcat-8.5.12/conf/server.xml

EXPOSE 8080
ENTRYPOINT ["/usr/local/apache-tomcat-8.5.12/bin/catalina.sh", "run"]
Tomcat Dockerfile

  2.3 docker-compose.yml  

version: '3'
services:
  # web-server  
  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - "80:80"
      - "443:443"
    links:
      - tomcat01:tomcat01
      - tomcat02:tomcat02
      - tomcat03:tomcat03
    volumes:
      - ./webapps:/opt/webapps
      - ./nginx/logs:/usr/local/nginx/logs
      - ./etc/localtime:/etc/localtime
      - ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf:ro
      - ./nginx/conf.d/:/usr/local/nginx/conf.d/:ro
    depends_on:
      - mysql
      - tomcat01
      - tomcat02
      - tomcat03
  # tomcat
  tomcat01:
    hostname: tomcat01
    build: ./tomcat
    links:
      -  mysql:mysql
    volumes:
      - ./webapps:/usr/local/apache-tomcat-8.5.12/webapps
      - ./etc/localtime:/etc/localtime

  tomcat02:
    hostname: tomcat02
    build: ./tomcat
    links:
      -  mysql:mysql
    volumes:
      - ./webapps:/usr/local/apache-tomcat-8.5.12/webapps
      - ./etc/localtime:/etc/localtime

  tomcat03:
    hostname: tomcat03
    build: ./tomcat
    links:
      -  mysql:mysql
    volumes:
      - ./webapps:/usr/local/apache-tomcat-8.5.12/webapps
      - ./etc/localtime:/etc/localtime

  # mysql
  mysql:
    hostname: mysql
    image: mysql:latest
    ports:
     - "3307:3306"
    volumes:
      - ./mysql/conf/my.cnf:/etc/mysql/conf.d/my.cnf
      - ./mysql/mysqldb:/var/lib/mysql
      - ./etc/localtime:/etc/localtime:ro
    environment:
      - MYSQL_ROOT_PASSWORD=123456
docker-compose.yml

 

 

 

  2.4 编写index.jsp文件

sudo mkdir webapps/ROOT

sudo vim webapps/ROOT/index.jsp


nginx-tomcat cluster

  2.5 构建

docker-compose up

  2.6 查看

$ docker-compose ps
         Name                       Command               State                    Ports                  
----------------------------------------------------------------------------------------------------------
nginxtomcat_mysql_1      docker-entrypoint.sh mysqld      Up      0.0.0.0:3307->3306/tcp                  
nginxtomcat_nginx_1      /usr/local/nginx/sbin/ngin ...   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
nginxtomcat_tomcat01_1   /usr/local/apache-tomcat-8 ...   Up      8080/tcp                                
nginxtomcat_tomcat02_1   /usr/local/apache-tomcat-8 ...   Up      8080/tcp                                
nginxtomcat_tomcat03_1   /usr/local/apache-tomcat-8 ...   Up      8080/tcp 

  

  相关文档:https://github.com/Bigberg/docker/tree/master/nginx-tomcat

                         里面去掉了nginx/tomcat/jdk的安装包,需添加(版本在Dockerfile中)

posted @ 2018-03-07 11:13  Bigberg  阅读(3834)  评论(0编辑  收藏  举报