1. 创建docker compose目录
[root@docker /]# mkdir -p /compose_lnmt
2. 编写nginx的dockerfile
2.1 创建目录
[root@docker /]# cd /compose_lnmt/
[root@docker compose_lnmt]# ll
总用量 0
[root@docker compose_lnmt]# mkdir -p nginx
[root@docker compose_lnmt]# cd nginx/
2.2 编写nginx配置文件
[root@docker nginx]# vim nginx.conf
[root@docker nginx]# cat nginx.conf
user root;
worker_processes auto;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
}
http {
include 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 logs/access.log main;
sendfile on;
keepalive_timeout 65;
upstream tomcat_8080 {
server tomcat1:8080;
server tomcat2:8080;
}
server {
listen 80;
server_name localhost;
location ~ / {
proxy_pass http://tomcat_8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# When setting up Harbor behind other proxy, such as an Nginx instance, remove the below line if the proxy already has similar settings.
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
proxy_request_buffering off;
}
}
}
2.3 编写nginx的Dockerfile文件
[root@docker nginx]# vim Dockerfile
[root@docker nginx]# cat Dockerfile
#this docker file
#VERSION 1
#author:shichao@scajy.cn
FROM centos:7
MAINTAINER shichao@scajy.cn
RUN yum install -y gcc gcc-c++ make pcre pcre-devel openssl openssl-devel pcre-davel gd-devel iproute net-tools telnet wget curl && yum clean all && rm -rf /var/cache/yum/*
RUN useradd -M -s /sbin/nologin nginx
RUN wget http://nginx.org/download/nginx-1.17.6.tar.gz && tar zxf nginx-1.17.6.tar.gz &&
cd nginx-1.17.6 &&
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module &&
make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]
2.4 创建数据存放目录
[root@docker compose_lnmt]# mkdir -p /var/www/html
3. 编写tomcat的dockerfile
3.1 创建tomcat目录
[root@docker nginx]# cd /compose_lnmt/
[root@docker compose_lnmt]# ll
总用量 0
drwxr-xr-x. 2 root root 42 10月 19 16:37 nginx
[root@docker compose_lnmt]# mkdir -p tomcat
[root@docker compose_lnmt]# cd tomcat/
3.2 下载安装包
3.2.1 下载地址
软件准备
JDK下载:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Tomcat下载:http://tomcat.apache.org/
以上是软件在的连接,可以参考
3.2.2 下载tomcat包
[root@docker tomcat]# wget https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-8/v8.5.59/bin/apache-tomcat-8.5.59.tar.gz
3.3 编写tomcat的dockerfile文件
[root@docker tomcat]# cat Dockerfile
# this docker file
# VERSION 1.0
# author: shichao
# email: shichao@scajy.cn
FROM centos:7
MAINTAINER shichao@scajy.cn
ENV VERSION=8.5.59
RUN yum install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel net-tools wget curl unzip iproute java-1.8.0-openjdk && yum clean all && rm -rf /var/cache/yum/*
ADD apache-tomcat-${VERSION}.tar.gz /usr/local/
RUN mv /usr/local/apache-tomcat-${VERSION} /usr/local/tomcat &&
sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh &&
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH /usr/local/tomcat/bin:$PATH
WORKDIR /usr/local/tomcat
EXPOSE 8080
CMD ["catalina.sh", "run"]
3.4 创建tomcat的war包存放目录
[root@docker compose_lnmt]# mkdir -p /var/www/tomcat
3.5 拷贝tomcat数据目录到容器目录下
[root@docker tomcat]# cp -a /usr/local/tomcat/webapps/* /var/www/tomcat/
[root@docker tomcat]# ll /var/www/tomcat/
总用量 4
drwxr-x---. 15 root root 4096 10月 15 17:38 docs
drwxr-x---. 6 root root 83 10月 15 17:38 examples
drwxr-x---. 5 root root 87 10月 15 17:38 host-manager
drwxr-x---. 6 root root 114 10月 15 17:38 manager
drwxr-x---. 3 root root 283 10月 15 17:38 ROOT
4. 使用docker compose编译lnmt环境
4.1 切换到docker compose目录下
[root@docker tomcat]# cd /compose_lnmt/
4.2 编写docker compose配置文件
[root@docker compose_lnmt]# vim docker-compose.yml
[root@docker compose_lnmt]# cat docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 80:80
networks:
- lnmt
volumes:
- /var/www/html:/usr/local/nginx/html
- ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf
tomcat1:
hostname: tomcat1
build:
context: ./tomcat
dockerfile: Dockerfile
networks:
- lnmt
volumes:
- /var/www/tomcat:/usr/local/tomcat/webapps
tomcat2:
hostname: tomcat2
build:
context: ./tomcat
dockerfile: Dockerfile
networks:
- lnmt
volumes:
- /var/www/tomcat:/usr/local/tomcat/webapps
mysql:
hostname: mysql
image: mysql:5.6
ports:
- 3307:3306
networks:
- lnmt
volumes:
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/data:/var/lib/mysql
command: --character-set-server=utf8
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: tomcat
MYSQL_USER: tomcat
MYSQL_PASSWORD: tomcat123
networks:
lnmt:
5. docker compose 执行构建启动lnmt环境
[root@docker compose_lnmt]# docker-compose -f docker-compose.yml up -d
Starting compose_lnmt_nginx_1 ... done
Starting compose_lnmt_mysql_1 ... done
Starting compose_lnmt_tomcat1_1 ... done
Starting compose_lnmt_tomcat2_1 ... done
6. 查看容器运行是否正常
[root@docker compose_lnmt]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------
compose_lnmt_mysql_1 docker-entrypoint.sh --cha ... Up 0.0.0.0:3307->3306/tcp
compose_lnmt_nginx_1 nginx -g daemon off; Up 0.0.0.0:80->80/tcp
compose_lnmt_tomcat1_1 catalina.sh run Up 8080/tcp
compose_lnmt_tomcat2_1 catalina.sh run Up 8080/tcp
7. 浏览器访问