2. 案例二 整套项目多容器分离通过docker-compose部署lanmt环境
详细的安装准备环境,省略,配置以及部署参考案例一即可 即可实现批量创建web,也可以实现多web共用一个php或mysql容器
2.1 目录路径的案例
[root@localhost compose_lanmt]# pwd
/app/docker_web/compose_lanmt
[root@localhost compose_lanmt]# ll
总用量 20
drwxr-xr-x 6 3200 3200 4096 7月 19 12:08 apache
-rw-r--r-- 1 3200 3200 2339 7月 19 15:07 docker-compose-lanmt.yml
drwxr-xr-x 4 3200 3200 4096 1月 19 2018 mysql
drwxr-xr-x 5 3200 3200 4096 7月 19 12:06 nginx
drwxr-xr-x 8 3200 3200 4096 7月 19 12:12 tomcat
2.2 nginx的Dockerfile文件配置
注意: 和lnmp里面的dockerfile相同
进入到nginx目录下配置
将需要安装的nginx包,放入到nginx指定目录下
cat Dockerfile
FROM centos:7
MAINTAINER Mr.JingWen
RUN yum install -y wget telnet vim iproute iputils gcc gcc-c++ make openssl-devel pcre pcre-devel
RUN useradd -s /sbin/nologin -u 3000 nginx
ENV TZ="Asia/Shanghai" nginx_v="1.14.2" soft_sign="PXJY_WebSer" nginx_dir="/usr/local/nginx"
ADD source_file/nginx-${nginx_v}.tar.gz /usr/local/src/
# 隐藏服务和版本号
#注意: 若nginx的版本不是 1.14.2 ,需要修改 nginx.h 里面的版本号,使其对应
COPY ./source_file/nginx.h /usr/local/src/nginx-${nginx_v}/src/core/
COPY ./source_file/ngx_http_header_filter_module.c /usr/local/src/nginx-${nginx_v}/src/http/
COPY ./source_file/ngx_http_special_response.c /usr/local/src/nginx-${nginx_v}/src/http/
RUN cd /usr/local/src/nginx-${nginx_v} \
&& ./configure --prefix=${nginx_dir} --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module \
--with-http_gzip_static_module --with-pcre \
&& make -j 4 && make install
RUN rm -rf /usr/local/src/nginx-${nginx_v}* && yum clean all
RUN chown -R nginx ${nginx_dir} && rm -rf /usr/local/src/nginx-${nginx_v}
WORKDIR ${nginx_dir}
EXPOSE 80
CMD ["./sbin/nginx", "-g", "daemon off;"]
注:通过此镜像编译后,可直接启动无需挂载配置文件,若有需要,根据自己需要进行挂载配置。
cat nginx.conf
user nginx;
worker_processes auto;
error_log logs/error.log info;
pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 3072;
}
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;
tcp_nodelay on;
server_tokens off;
#proxy_buffer_size 64k;
#proxy_buffers 4 256k;
#proxy_busy_buffers_size 256k;
#client_header_buffer_size 64k;
#large_client_header_buffers 4 64k;
#fastcgi_connect_timeout 60;
#fastcgi_send_timeout 180;
fastcgi_read_timeout 600;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
client_body_buffer_size 20M;
client_max_body_size 20M; #设置允许客户端请求的最大的单个文件字节数
client_header_buffer_size 20M; #指定来自客户端请求头的headebuffer大小
include conf.d/*.conf;
}
cat conf.d/project.conf
upstream lb_tomcat {
ip_hash;
server tomcat01:8080;
server tomcat02:8080;
}
server {
listen 80;
server_name localhost;
access_log /app1log/access.log main;
error_log /app1log/error.log;
location / {
proxy_pass http://lb_tomcat;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
2.3 apache代理服务的Dockerfile文件配置
cat ./apache/Dockerfile
FROM centos:7
MAINTAINER Mr.JingWen
# apache和php环境的环境变量参数;
# 注: 编译php版本前,需要先修改下面httpd.conf中php模块的版本号,libphp
ENV TZ="Asia/Shanghai" apr_v="1.7.0" apr_util_v="1.6.0" httpd_v="2.4.39" httpd_dir="/usr/local/httpd"
RUN useradd -s /sbin/nologin -u 3300 apache
RUN yum install -y gcc gcc-c++ make cmake autoconf automake ncurses-devel \
libxml2-devel perl-devel libcurl-devel libgcrypt libgcrypt-devel \
libxslt libxslt-devel pcre-devel openssl-devel openssl wget \
curl-devel db4-devel libXpm-devel gmp-* libc-client-devel vim \
openldap-devel freetype freetype-devel gd gd-devel libjpeg-devel \
libpng-devel zlib zlib-devel bzip2-devel libtool expat-devel bison \
iproute telnet iputils libtermcap-devel
#=============== 构建 Apache 服务镜像配置 ==============#
RUN echo -e "\033[44;37m 开始编译 apache 服务 \033[0m" && sleep 3
ADD source_file/apr-${apr_v}.tar.gz /usr/local/src
RUN cd /usr/local/src/apr-${apr_v} \
&& ./configure --prefix=/usr/local/apr \
&& make -j 4 && make install
ADD source_file/apr-util-${apr_util_v}.tar.gz /usr/local/src
RUN cd /usr/local/src/apr-util-${apr_util_v} \
&& ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr \
&& make -j 4 && make install
ADD source_file/httpd-${httpd_v}.tar.gz /usr/local/src
WORKDIR /usr/local/src/httpd-${httpd_v}
#优化apache请求数 || 隐藏apache版本号和更改软件名字标志的函数
RUN perl -pi -e "s/DEFAULT_SERVER_LIMIT 256/DEFAULT_SERVER_LIMIT 15000/g" server/mpm/prefork/prefork.c \
&& sed -i "/#define AP_SERVER_BASEPRODUCT/s/Apache/PXJY WebSer/" include/ap_release.h \
#开始编译
&& ./configure --prefix=${httpd_dir} -enable-cgi --with-zlib --with-pcre --with-apr=/usr/local/apr \
--enable-expires=shared --enable-vhost-alias=shared --with-apr-util=/usr/local/apr-util \
--enable-rewrite=shared --enable-speling=shared --enable-proxy=shared --enable-so --enable-ssl \
--with-mpm=prefork --with-expat=builtin --enable-modules=all --enable-usertrack=shared \
&& make -j 4 && make install
WORKDIR ${httpd_dir}/conf
RUN sed -i 's/#ServerName www.example.com:80/ServerName 0.0.0.0:80/g' httpd.conf \
&& sed -i '$a Include conf/extra/httpd-vhosts.conf' httpd.conf \
&& sed -i 's/User daemon/User apache/g' httpd.conf \
&& sed -i 's/Group daemon/Group apache/g' httpd.conf \
&& sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/g' httpd.conf \
&& sed -i '$a ServerTokens Prod' httpd.conf \
&& sed -i '$a ServerSignature Off' httpd.conf \
&& sed -i 's/#LoadModule/LoadModule/g' httpd.conf
RUN chown -R apache.apache ${httpd_dir}
WORKDIR ${httpd_dir}
EXPOSE 80
ENTRYPOINT ["./bin/httpd", "-D", "FOREGROUND"]
注:通过此镜像编译后,可直接启动无需挂载配置文件,若有需要,根据自己需要进行挂载配置,也可以编译自己想要编译的版本
2.4 tomcat的Dockerfile文件配置
进入到tomcat的目录
将需要的源码包 jdk和tomcat包,放入到tomcat目录下
或者,jdk环境,用物理机上面的,做一下挂载即可,考虑到jdk包比较大
cat Dockerfile
FROM centos:7
MAINTAINER Mr.JingWen
RUN yum install -y wget telnet vim iproute iputils gcc gcc-c++ make cmake
RUN useradd -u 3200 tomcat
ADD source_file/jdk1.8.0_111.tgz /usr/local
ENV TZ="Asia/Shanghai" tomcat_dir="/usr/local/tomcat" JAVA_HOME="/usr/local/jdk1.8.0_111" tomcat_v="9.0.20"
ENV CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar
ENV PATH=$JAVA_HOME/bin:$PATH:$HOME/bin:$JAVA_HOME/jre/bin:/usr/local/libexec/git-core
ENV LD_LIBRARY_PATH=$JAVA_HOME/jre/bin:/usr/local/lib
ENV export JAVA_HOME CLASSPATH PATH LD_LIBRARY_PATH
ADD source_file/apache-tomcat-${tomcat_v}.tar.gz /usr/local/
RUN mv /usr/local/apache-tomcat-${tomcat_v} ${tomcat_dir}
ADD source_file/cronolog-1.6.2.tar.gz /usr/local/src
WORKDIR /usr/local/src/cronolog-1.6.2
RUN ./configure && make -j 4 && make install
COPY source_file/catalina.sh ${tomcat_dir}/bin
RUN chmod +x ${tomcat_dir}/bin/*.sh \
&& chmod +x ${JAVA_HOME}/bin/* \
&& chown -R tomcat.tomcat ${tomcat_dir}
USER tomcat
WORKDIR ${tomcat_dir}
EXPOSE 8005 8080 8009
ENTRYPOINT ["./bin/catalina.sh", "run"]
2.5 mysql的Dockerfile文件配置
注意:mysql的Dockerfile 可以自己编译,亦可以用镜像直接启动即可,方法和lnmp里面的安装启动方法是一样的
进入到mysql目录
mkdir -p conf data
再进入到conf目录下,配置my.cnf文件
cat my.cnf
[mysqld]
user=mysql
port=3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysql/mysql.pid
log_error=/var/log/mysql/error.log
character_set_server = utf8
max_connections=3600
2.6 docker-copose-lanmt.yml配置文件
附: lanmt环境目录
[root@localhost compose_lanmt]# pwd
/app/docker_web/compose_lanmt
[root@localhost compose_lanmt]# ll
总用量 20
drwxr-xr-x 6 3200 3200 4096 7月 19 12:08 apache
-rw-r--r-- 1 3200 3200 2339 7月 19 15:07 docker-compose-lanmt.yml
drwxr-xr-x 4 3200 3200 4096 1月 19 2018 mysql
drwxr-xr-x 5 3200 3200 4096 7月 19 15:45 nginx
drwxr-xr-x 8 3200 3200 4096 7月 19 15:52 tomcat
docker-compose.yml 文件所在位置和nginx,myslq,tomcat目录同级
管理多个容器的工具,执行一条命令即可安装启动多个容器
-
docker-copose-lanmt.yml配置如下
cat docker-compose.yml
#===============================================
#========== 定义了版本信息 ===============
version: '3'
#===============================================
#=========== Network Service ===============
networks:
lanmt:
driver: bridge
ipam:
config:
- subnet: 192.168.30.0/24
#===============================================
#========= 定义了服务的配置信息 ==========
services:
#Nginx Service
nginx-proxy:
hostname: nginx-proxy
container_name: compose-nginx-proxy
image: yangsir/nginx-proxy:1.14.2
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 82:80
networks:
lanmt:
ipv4_address: 192.168.30.11
depends_on:
- tomcat01
- tomcat02
environment:
TZ: Asia/Shanghai
volumes:
- ./nginx/conf82:/usr/local/nginx/conf
- ./nginx/logs/app1log:/app1log
#apache Service
apahce-proxy:
hostname: apache-proxy
container_name: compose-apache-proxy
image: yangsir/apache-proxy:2.4.39
build:
context: ./apache
dockerfile: Dockerfile
ports:
- 83:80
networks:
lanmt:
ipv4_address: 192.168.30.12
depends_on:
- tomcat01
- tomcat02
environment:
TZ: Asia/Shanghai
volumes:
- ./apache/conf83:/usr/local/httpd/conf
- ./apache/logs/app1log:/app1log
#tomcat01 Service
tomcat01:
hostname: tomcat01
container_name: compose-tomcat01
image: yangsir/tomcat:9.0.20
build:
context: ./tomcat
dockerfile: Dockerfile
networks:
lanmt:
ipv4_address: 192.168.30.13
environment:
TZ: Asia/Shanghai
volumes:
- ./tomcat/conf-01:/usr/local/tomcat/conf
- ./tomcat/logs/app1log:/usr/local/tomcat/logs
- ./tomcat/webapps/tomcat01/ROOT:/usr/local/tomcat/webapps/ROOT
#tomcat02 Service
tomcat02:
hostname: tomcat02
container_name: compose-tomcat02
image: yangsir/tomcat:9.0.20
build:
context: ./tomcat
dockerfile: Dockerfile
networks:
lanmt:
ipv4_address: 192.168.30.14
environment:
TZ: Asia/Shanghai
volumes:
- ./tomcat/conf-02:/usr/local/tomcat/conf
- ./tomcat/logs/app2log:/usr/local/tomcat/logs
- ./tomcat/webapps/tomcat02/ROOT:/usr/local/tomcat/webapps/ROOT
下载地址:
https://cdn.mysql.com/Downloads/Connector-J/mysql-connector-java-5.1.47.tar.gz
注意:
1) 编写yml文件和tomcat的Dockerfile的时候,需要注意下,jdk的环境变量
2) 默认如上安装后,是连接不上数据库的,需要一个连接数据库的jar包,mysql-connector-java-5.1.31.jar,上传到tomcat/lib下面
3) 修改数据库的连接配置
eg: 配置数据库如下
cat db.properties
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://mysql:3306/gongyike?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTime\
Behavior=convertToNull
jdbc.username = root
jdbc.password = 123456
jdbc.maxActive = 100
jdbc.maxWait = 20000
jdbc.maxIdle = 2000
jdbc.defaultAutoCommit = false
jdbc.minPoolSize = 10
jdbc.maxPoolSize = 100
jdbc.acquireIncrement = 3
jdbc.initialPoolSize = 20
注意:数据库连接的配置文件,主机名有三种配置方法,均能实现
- 通过mysql的容器名来连接
- 通过mysql容器的主机名连接
- 通过映射到宿主机的端口来连接
如上面的配置,端口前面的mysql,为容器主机名
2.7 启动lnmt环境
启动方法雷同 lanmp 的启动
前台执行:
docker-compose -f docker-compose.yml up
后台执行:
docker-compose -f docker-compose.yml up -d
常用命令:(省略-f)
docker-compose rm/down/pause/unpause/stop/start/restart/logs/up
删除所有未被任何容器关联引用的卷“
docker volume rm $(docker volume ls -qf dangling=true)
删除所有已退出的容器
docker rm -v $(docker ps -aq -f status=exited)
删除所有状态为dead的容器
docker rm -v $(docker ps -aq -f status=dead)
转载于:https://www.cnblogs.com/yangsirs/p/11238056.html