简单的docker-compose编写介绍(包含 nginx+tomcat 实例演示)

Posted 小白的成功进阶之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的docker-compose编写介绍(包含 nginx+tomcat 实例演示)相关的知识,希望对你有一定的参考价值。

简单的docker-compose编写介绍(包含实例演示)

一、docker compose常用命令

字段描述
build重新构建服务
ps列出容器
up创建和启动容器
exec在容器里面执行命令
scale指定一个服务容器启动数量
top显示容器进程
logs查看容器输出
down删除容器、网络、数据卷和镜像
stop/start/restart停止/启动/重启服务

二、docker compose配置常用字段

字段描述
build docker context指定Dockerfile文件名构建镜像上下文路径
image指定镜像
command执行命令,覆盖默认命令
container_name指定容器名称,由于容器名称是唯一的,如果指定自定义名称,则无法scale
environment添加环境变量
networks加入网络
ports暴露容器端口,与-p相同,但不能低于60
volumes挂载宿主机路径或命令卷
restart重启策略,默认no,always,no-failure,unless-stoped
hostname容器主机名

三、docker compose 演示(tomcat+nginx

1、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.17.0.3: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\\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;"]

2、dockerfile撰写 tomcat 镜像

FROM centos:7.4.1708
ADD jdk-8u91-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.16.tar.gz /usr/local
RUN mv /usr/local/jdk1.8.0_91 /usr/local/java  && mv /usr/local/apache-tomcat-9.0.16 /usr/local/tomcat
ENV JAVA_HOME /usr/local/java
ENV JAVA_BIN /usr/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
ENV PATH $JAVA_BIN:/usr/local/java/jre/bin:$PATH 
RUN 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(\\"123456\\");%>\\n</body>\\n</html>" > /usr/local/tomcat/webapps/lucien/index.jsp \\
    && 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
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]

3、撰写docker compose.yml文件

version: '3'
networks:
  lnmp:
    external: true
services:
  tomcat:
    hostname: tomcat
    build:
      context: ./tomcat
      dockerfile: dockerfile
    ports:
      - 8080:8080
    networks:
      lnmp:
        ipv4_address: 172.168.184.100
  nginx:
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: dockerfile
    ports:
      - 80:80
    networks:
      lnmp:
        ipv4_address: 172.168.184.50

4、开启,并查看运行状态



5、进行网页测试(分别输入192.168.184.70、192.168.184.70:8080/192.168.184.70/lucien/index.jsp)



以上是关于简单的docker-compose编写介绍(包含 nginx+tomcat 实例演示)的主要内容,如果未能解决你的问题,请参考以下文章

简单的docker-compose编写介绍(包含 nginx+tomcat 实例演示)

docker-compose

docker-compose.yml说明和编写

Docker-Compose介绍,安装和使用

Docker之docker-compose的介绍与命令的使用

docker-compose 基础和介绍