闲着无聊,今天就写个 dockerfile 定制 tomcat+nginx 集群
Posted 小白的成功进阶之路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了闲着无聊,今天就写个 dockerfile 定制 tomcat+nginx 集群相关的知识,希望对你有一定的参考价值。
dockerfile 定制 tomcat+nginx 集群
前言(来张图)
一、dockerfile撰写 tomcat 镜像
FROM centos:7.4.1708
COPY jdk-8u201-linux-x64.rpm /opt
ADD apache-tomcat-9.0.16.tar.gz /opt
RUN cd /opt && rpm -qpl jdk-8u201-linux-x64.rpm \\
&& rpm -ivh jdk-8u201-linux-x64.rpm
ENV JAVA_HOME /usr/java/jdk1.8.0_201-amd64
ENV CLASSPATH .:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
ENV PATH $JAVA_HOME/bin:$PATH
RUN mv /opt/apache-tomcat-9.0.16 /usr/local/tomcat \\
&& mkdir /usr/local/tomcat/webapps/lucien \\
&& echo -e "<%@ page language=\\"java\\" import=\\"java.util.*\\" pageEncoding=\\"UTF-8\\"%> \\n<html>\\n<head>\\n<title>JSP test1 page</title>\\n</head>\\n<body>\\n<% out.println("1234567890");%>\\n</body>\\n</html>" > /usr/local/tomcat/webapps/lucien/index.jsp
RUN sed -i '71a <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">' /usr/local/tomcat/conf/server.xml \\
&& sed -i '72a <Context docBase="/usr/local/tomcat/webapps/lucien" path="" reloadable="true">' /usr/local/tomcat/conf/server.xml \\
&& sed -i '73a </Context>' /usr/local/tomcat/conf/server.xml \\
&& sed -i '74a </Host>' /usr/local/tomcat/conf/server.xml
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh","start"]
文件结构:
创建tomcat镜像:
二、dockerfile撰写 nginx 镜像
FROM centos:7.4.1708
ADD nginx-1.12.0.tar.gz /opt
RUN yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make \\
&& useradd -M -s /sbin/nologin nginx \\
&& cd /opt/nginx-1.12.0/ \\
&& ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-file-aio --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module \\
&& make \\
&& make install \\
&& ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx \\
&& echo -e "events {\\nuse epoll;\\nworker_connections 1024;\\n}\\nhttp {\\nupstream tomcat_server {\\nserver 172.168.184.100:8080 weight=1;\\n}\\nserver_tokens on;\\nserver {\\nlisten 80;\\nserver_name localhost;\\nlocation ~ .*\\.(gif|jpg|jpeg|png|bmp|swf|css)\\$ {\\nroot /usr/local/nginx/html/xxx;\\nexpires 10d;\\n}\\nlocation / {\\nroot html;\\nindex index.html index.htmi index.php;\\n}\\nerror_page 500 502 503 504 /50x.html;\\nlocation = /50x.html {\\nroot html;\\n}\\nlocation ~ .*.jsp\\$ {\\nproxy_pass http://tomcat_server;\\nproxy_set_header HOST \\$host;\\nproxy_set_header X-Real-IP \\$remote_addr;\\nproxy_set_header X-Forwarded-For \\$proxy_add_x_forwarded_for;\\n}\\nlocation ~ \\.php$ {\\nroot html;\\nfastcgi_pass 172.168.184.30:9000;\\nfastcgi_index index.php;\\nfastcgi_param SCRIPT_FILENAME \\$document_root\\$fastcgi_script_name;\\ninclude fastcgi_params;\\n}\\n}\\n}" > /usr/local/nginx/conf/nginx.conf
ENV PATH /usr/local/nginx/sbin:$PATH
WORKDIR /usr/local/nginx/
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
文件结构:
创建 nginx 镜像:
三、查看镜像构建情况
四、分别开启nginx和tomcat
五、测试
以上是关于闲着无聊,今天就写个 dockerfile 定制 tomcat+nginx 集群的主要内容,如果未能解决你的问题,请参考以下文章