docker-compose之Nginx+Tomcat代理配置

Tomcat镜像制作与启动

Dockerfile如下

    FROM docker.io/sssllc/centos7.2-jdk1.8

    #update
    RUN yum -y update; yum clean all && yum install -y wget unzip tar net-tools

    #install jdk && tomcat
    #ADD jdk-8u151-linux-x64.tar.gz /usr/local/        #原始镜像不存在jdk环境是使用(上下文有这个文件存在)
    ADD tomcat7.tar.gz /usr/local/

    #设置环境变量
    #ENV JAVA_HOME /usr/local/jdk1.8.0_151                    #如果有jdk环境的话略过
    #ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
    ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.82
    ENV CATALINA_BASE /usr/local/apache-tomcat-7.0.82
    #ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
    ENV LC_ALL en_US.UTF-8                  #防止乱码

    EXPOSE 8080

    CMD /usr/local/apache-tomcat-7.0.82/bin/catalina.sh run

 

build镜像

docker build -t tomcat7 .

 

启动tomcat并将镜像push到私有仓库

//启动
docker run -itd -p 8090:8080 --name tomcat1 -v /data/admin-web/:/data/webapp tomcat7      #/data/webapp为tomcat代码发布目录

//push到私有仓库
docker tag tomcat7 10.10.10.12/hztest/tomcat7-jdk1.8      #首先打标签
docker push 10.10.10.12/hztest/tomcat7-jdk1.8

 

nginx镜像配置

pull原始镜像

docker pull registry.cn-hangzhou.aliyuncs.com/kkf_centos7/nginx

 

自定义镜像,dockerfile如下

FROM registry.cn-hangzhou.aliyuncs.com/kkf_centos7/nginx

COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

 

nginx.conf如下

user  nginx;
        worker_processes  1;

        error_log  /var/log/nginx/error.log warn;
        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;
            types_hash_max_size 2048;
            gzip  on;

            #include /etc/nginx/conf.d/*.conf;
       #负载均衡,下面的tomcat01和tomcat02为nginx连接tomcat容器时给容器设置的别名的名称,即配置文件写啥名,link指定的别名就为啥名 upstream tomcat_client {               server tomcat01:8080 weight=1;       server tomcat02:8080 weight=1; } server { server_name ""; listen 80 default_server; listen [::]:80 default_server ipv6only=on; location / { proxy_pass
http://tomcat_client;      #反向代理 proxy_redirect default; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } }

 

build 打包并push到私有仓库

docker build -t nginx .
docker tag nginx 10.10.10.12/hztest/nginx docker push 10.10.10.12/hztest/nginx

 

启动所有容器

#注意:如果需要查看日志,需要加上log目录的映射,log目录查看dockerfile和nginx配置文件即可得到

//启动tomcat容器(两个)
docker run
-itd -p 8090:8080 --name tomcat1 -v /data/admin-web/:/data/webapp tomcat7       docker run -itd -p 8091:8080 --name tomcat2 -v /data/admin-web/:/data/webapp tomcat7 //启动nginx容器,指定--link,会在nginx的容器hosts文件里生成一条tomcat容器的host记录,格式为 tomcat1容器ip 容器名(tomcat1) 容器别名(tomcat02) tomcat容器主机名(containerid) docker run --name nginx --link=tomcat1:tomcat01 --link=tomcat2:tomcat02 -p 80:80 -idt nginx #link下 第一个tomcat1为 tomcat1容器的容器名,第二个tomcat1为此容器的别名。在nginx配置文件里指定的别名

 

docker-compose

创建docker-compose.yml文件

version: '2'
services:
  nginx1:
    container_name: nginx 
    image: 10.10.10.12/hztest/nginx
    links:                   #links这里tomcat1为容器名,tomcat01为tomcat1的别名。nginx连接tomcat1的配置文件里,写tomcat01即可
      - tomcat1:tomcat01 
      - tomcat2:tomcat02
    ports: 
      - "80:80" 
    volumes:
      - /data/logs/nginx:/var/log/nginx/
    depends_on:                            #依赖redis启动才能启动tomcat,可忽略
      - tomcat01
      - tomcat02
    restart: always 

  tomcat1:
    container_name: tomcat1 
    image: 10.10.10.12/hztest/tomcat7-jdk1.8
    ports: 
      - "8081:8080"
    volumes:
      - /data/admin-web:/data/webapp
      - /data/logs/admin/tomcat1:/usr/local/apache-tomcat-7.0.82/logs
    environment:
      TOMCAT_SERVER_ID: tomcat_server_001
      
    depends_on:                            #依赖redis起来后才能启动tomcat,如果没有可忽略
      - redis
      
    restart: always

  tomcat2:
    container_name: tomcat2 
    image: 10.10.10.12/hztest/tomcat7-jdk1.8
    ports: 
      - "8082:8080"
    volumes:
      - /data/admin-web:/data/webapp
      - /data/logs/admin/tomcat2:/usr/local/apache-tomcat-7.0.82/logs
    environment:
      TOMCAT_SERVER_ID: tomcat_server_002
    
    depends_on:                            #依赖redis起来后才能启动tomcat,如果没有可忽略
      - redis
      - rabbitmq
      - mongodb
     
    restart: always

 

启动

docker-compose up          #前台启动

docker-compose up -d      #后台启动

docker-compose restart   #重启

 

posted @ 2018-08-16 17:04  FRESHMANS  阅读(1034)  评论(0编辑  收藏  举报