Docker nginx+tomcat构建

docker

司有一项目比较老,使用Java jdk7版本,且因OpenJDK源代码不完整,需要手动构建一版Oracle Tomcat + Java,且需要nginx做前置代理转发请求到Tomcat。

思路:配置Oracle Tomcat版本,挂载日志和webapps目录到宿主机,nginx日志和conf.d挂载到宿主机,nginx代理转发到Tomcat服务。使用docker-compose启动服务。

准备工作:

下载:

  • apache-tomcat-7.0.92.tar.gz
  • jdk-7u80-linux-x64.tar.gz

下载好的安装包,放在tomcat下。

整个项目目录如下:

└── example
    ├── docker-compose.yml
    ├── nginx
    │   ├── Dockerfile
    │   ├── conf
    │   │   ├── conf.d
    │   │   │   ├── test-tomcat.conf
    │   │   │   └── test.conf
    │   │   └── nginx.conf
    │   └── index.html
    └── tomcat
        ├── Dockerfile
        ├── logs
        └── webapps

docker-compose.yml

version: '3'
services:
  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - "8002:80"
      - "8004:81"
    links:
      - tomcat:tomcat
    volumes:
      - ./nginx/logs:/var/log/nginxi
      - ./nginx/conf/conf.d:/etc/nginx/conf.d
    depends_on:
      - tomcat
  tomcat:
    hostname: tomcat
    build: ./tomcat
    ports:
      - "8003:8080"
    volumes:
      - ./tomcat/webapps:/usr/local/apache-tomcat-7.0.92/webapps
      - ./tomcat/logs:/usr/local/apache-tomcat-7.0.92/logs


Tomcat Dockerfile:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
FROM centos:7.5.1804
MAINTAINER [email protected]

# 添加Java SDK和Tomcat到容器中
ADD ./jdk-7u80-linux-x64.tar.gz /usr/local/
ADD ./apache-tomcat-7.0.92.tar.gz /usr/local/

# 设置环境变量
ENV JAVA_HOME /usr/local/jdk1.7.0_80
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.92
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

# 设置端口号
EXPOSE 8080

# 启动tomcat
CMD $CATALINA_HOME/bin/catalina.sh run

nginx Dockerfile

FROM nginx:1.14.1

COPY ./conf/nginx.conf /etc/nginx
COPY index.html /usr/share/nginx/html

nginx 转发请求配置,test-tomcat.conf

upstream backend {
    server tomcat:8080;
}

server {
    listen  80;
    # listen  443 ssl;
    server_name  localhost;
    
    location / {
      proxy_pass http://backend$request_uri;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
    }
}
updatedupdated2018-12-282018-12-28
Load Comments?