Docker部署web环境之Lanmp

1. 案例一 整套项目多容器分离通过docker-compose部署lanmp环境

中方文档参考网址: docker/kubernets网址
http://www.dockerinfo.net/document

docker网络关系

  • 同一网段之间容器互通,不同网络之间容器相互隔离,比如,docker0网段和docker1网段内的容器;
  • 不同网段内的容器如果想要互通,可以通过映射到物理网卡上的端口来进行通信,也是可以的;

1.1 介绍

compose 是一个定义和管理多容器的工具,使用python语言编写。使用compose配置文件描述多个容器应用的架构,比如使用什么镜像,数据卷,网络,映射端口等;然后一条命令管理所有服务,比如启动,停止,重启等。

1.2 安装docker环境

参考网址
https://docs.docker.com/install/linux/docker-ce/centos/

参考网址
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager    --add-repo   https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce
systemctl start docker
systemctl enable docker

docker tab键自动补全
yum install -y bash-completion
source /usr/share/bash-completion/completions/docker
source /usr/share/bash-completion/bash_completion

注意:
a)修改docker默认的安装路径
# vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd  --graph /app/docker

b)修改默认的网络地址段
# vim /etc/docker/daemon.json
{"bip":"192.168.10.1/24"}
{"icc": false}
注: docker的版本从2017年3月份开始,以年月份来展示版本,并且分为企业版和社区版

1.3 安装docker-compose

下载网址
https://github.com/docker/compose/releases

安装步骤:

curl -L https://github.com/docker/compose/releases/download/1.23.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose   
chmod +x /usr/local/bin/docker-compose   

1.4 目录路径的案例

[root@test compose_lanmp]# pwd
/home/yangjw/compose_lanmp
[root@test compose_lanmp]# ll
总用量 28
drwxr-xr-x 7 root root 4096 11月 11 17:44 apache_php
-rw-r--r-- 1 root root 1613 11月 11 17:44 docker-compose-lamp.yml
-rw-r--r-- 1 root root 2689 11月 11 17:42 docker-compose-lnmp.yml
drwxr-xr-x 4 root root 4096 11月 11 17:42 mysql
drwxr-xr-x 6 root root 4096 11月 11 17:44 nginx
drwxr-xr-x 4 root root 4096 11月 11 17:42 php
-rwxr-xr-x 1 root root 1541 11月 11 17:42 README

1.5 nginx 的Dockerfile文件的编写

进入到nginx的目录
将下载好的nignx源码包,放入到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;"]

备注:此处启动用自己创建的普通用户启动,但是考虑到和宿主机的用户避免冲突,所以指定了uid,授权的时候需要注意下,建议最好用uid号进行授权。
     通过此编译文件编译后可直接启动,根据自己需求挂载配置文件。

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

server {
    listen       80;
    server_name  localhost;
    access_log   /applog/app1.access.log  main;
    error_log    /applog/app2.error.log;
    root         /webroot/app1/;    
    location / {
        index  index.php index.html index.htm;
        if ( !-e $request_filename) {
            rewrite ^(.*)$ /index.php?info=/$1 last;
            break;
        }
    }   
    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }   
    location ~ \.php$ {
        fastcgi_pass   compose-php7.2:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_param  PHP_VALUE  "open_basedir=/webroot/app1:/tmp/";
    }
}

1.6 php的Dockerfile文件的编写

进入到php目录

cat Dockerfile

FROM   centos:7
MAINTAINER  Mr.JingWen
RUN    yum install -y wget telnet vim iproute  iputils gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel \
       libpng-devel openssl-devel
RUN    useradd  -s /sbin/nologin  -u 3000  nginx

#环境变量参数
ENV    TZ="Asia/Shanghai" mhash_v="0.9.9.9" libiconv_v="1.13" libmcrypt_v="2.5.8" php_v="7.2.19" php_dir="/usr/local/php"

ADD    source_file/mhash-${mhash_v}.tar.gz   /usr/local/src
RUN    cd  /usr/local/src/mhash-${mhash_v}         &&  ./configure &&  make  -j 4  &&  make  install

ADD    source_file/libiconv-${libiconv_v}.tar.gz   /usr/local/src
RUN    cd  /usr/local/src/libiconv-${libiconv_v}   &&  ./configure &&  make  -j 4  &&  make  install

ADD    source_file/libmcrypt-${libmcrypt_v}.tar.gz /usr/local/src
RUN    cd  /usr/local/src/libmcrypt-${libmcrypt_v} &&  ./configure &&  make  -j 4  &&  make  install \
       &&  cd libltdl &&  ./configure --with-gmetad --enable-gexec --enable-ltdl-install             \
       &&  make -j 4 && make install
RUN    echo '/usr/local/lib'  >>  /etc/ld.so.conf  &&  ldconfig

ADD    source_file/php-${php_v}.tar.gz      /usr/local/src
RUN    cd  /usr/local/src/php-${php_v}           \
       &&  ./configure --prefix=${php_dir} --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd  --enable-sysvsem    \
       --with-config-file-path=${php_dir}/etc --with-config-file-scan-dir=${php_dir}/etc/php-fpm.d             \
       --enable-bcmath  --without-pear  --with-openssl --with-gd --with-jpeg-dir --with-freetype-dir           \
       --with-zlib  --enable-xml --with-libxml-dir  --with-mhash  --enable-fpm --enable-mbregex                \
       --with-mcrypt  --with-curl   --with-png-dir  --enable-shmop --enable-mbstring  --enable-zip             \
       --enable-json --enable-phar --with-iconv-dir --enable-gd-native-ttf --enable-pcntl --enable-sockets     \
       --enable-inline-optimization              \
       &&    make -j 4 ZEND_EXTRA_LIBS='-liconv' \
       &&    make install                        \
       &&    cp  ${php_dir}/etc/php-fpm.conf.default  ${php_dir}/etc/php-fpm.conf        \
       #&&    cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf  \
       #注意: 容器中启动php时,启动进程的参数,需要配置禁止后台运行 配置为no,否则启动失败
       &&    sed  -i "21a \daemonize = no"                  ${php_dir}/etc/php-fpm.conf  \
       &&    cp   php.ini-production                        ${php_dir}/etc/php.ini       \
       &&    sed  -i '/upload_max_filesize/s/2/50/'         ${php_dir}/etc/php.ini       \
       &&    sed  -i '/post_max_size/s/8/50/'               ${php_dir}/etc/php.ini       \
       &&    sed  -i "/expose_php/s/On/OFF/"                ${php_dir}/etc/php.ini       \
       &&    sed  -i '$a ;date.timezone=PRC\ndate.timezone = Asia/Shanghai'                              ${php_dir}/etc/php.ini \
       &&    sed  -i '$a ;关闭高危函数应用\ndisable_functions = passthru,exec,shell_exec,system,chroot'  ${php_dir}/etc/php.ini   \
       &&    rm   -rf /usr/local/src/mhash-${mhash_v}         \
       &&    rm   -rf /usr/local/src/libiconv-${libiconv_v}   \
       &&    rm   -rf /usr/local/src/libmcrypt-${libmcrypt_v}
WORKDIR ${php_dir}
EXPOSE  9000
CMD    ["./sbin/php-fpm", "-c", "./etc/php-fpm.conf"]

备注:此处的程序启动用户,和nginx用的是一样的uid用户

1.7 apache和php后的Dockerfile编写

cat ./compose_lanmp/apache_php/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" \
        qdbm_v="1.8.78" mhash_v="0.9.9.9" libiconv_v="1.13" libmcrypt_v="2.5.8" php_dir="/usr/local/php" php_v="7.2.19"

RUN     useradd   -s /sbin/nologin  -u 3100  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 \
        #   php相关的参数,用php服务器时,才会生效
        &&  sed -i '/<IfModule mime_module>/a AddType application/x-httpd-php .php'         httpd.conf \
        &&  sed -i '/<IfModule mime_module>/a AddType application/x-httpd-php-source .phps' httpd.conf \
        #
        #   php版本不同所用模块也不同
        &&  sed -i '/<IfModule unixd_module>/i LoadModule php7_module modules/libphp7.so'   httpd.conf
        #&&  sed -i '/<IfModule unixd_module>/i LoadModule php5_module modules/libphp5.so'   httpd.conf \

#==================  构建 php 服务镜像  =================#

RUN     echo -e "\033[44;37m    开始编译 php 服务 \033[0m"  &&  sleep 3
ADD     source_file/qdbm-${qdbm_v}.tar.gz           /usr/local/src
RUN     cd  /usr/local/src/qdbm-${qdbm_v}           && ./configure && make -j 4 && make install

ADD     source_file/mhash-${mhash_v}.tar.gz         /usr/local/src
RUN     cd  /usr/local/src/mhash-${mhash_v}         && ./configure && make -j 4 && make install

ADD     source_file/libiconv-${libiconv_v}.tar.gz   /usr/local/src
RUN     cd  /usr/local/src/libiconv-${libiconv_v}   && ./configure && make -j 4 && make install

ADD     source_file/libmcrypt-${libmcrypt_v}.tar.gz /usr/local/src
RUN     cd  /usr/local/src/libmcrypt-${libmcrypt_v} && ./configure && make -j 4 && make install \
        &&  cd libltdl &&  ./configure --with-gmetad --enable-gexec --enable-ltdl-install       \
        &&  make -j 4 && make install
RUN     echo '/usr/local/lib'  >>  /etc/ld.so.conf  && ldconfig

ADD     source_file/php-${php_v}.tar.gz             /usr/local/src
RUN     cd  /usr/local/src/php-${php_v}           \
        &&  ./configure --prefix=${php_dir} --with-mysql=mysqlnd --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd   \
        --with-apxs2=${httpd_dir}/bin/apxs  --with-config-file-path=${php_dir}/etc --enable-pic  --disable-static  \
        --enable-mbstring --enable-inline-optimization   --with-regex=system --with-gettext --with-gd --with-png   \
        --with-zlib --enable-debugger --disable-debug  --enable-magic-quotes   --enable-safe-mode --enable-sysvmsg \
        --enable-sysvsem --enable-sysvshm --enable-track-vars --enable-yp --with-freetype-dir=/usr  --enable-ftp   \
        --with-xml  --with-png-dir=/usr --enable-gd-native-ttf --with-ttf  --with-ncurses --with-gmp --with-iconv  \
        --with-jpeg-dir=/usr  --with-expat-dir=/usr --enable-bcmath --enable-exif --enable-sockets --enable-wddx   \
        --enable-discard-path  --enable-trans-sid --enable-wddx --enable-versioning --enable-calendar --enable-dbx \
        --enable-dio --enable-mcal --enable-shmop --with-curl --with-openssl --with-qdbm --enable-fastcgi          \
        --enable-zip  --with-mcrypt --with-mhash --with-qdbm  --enable-fpm                                         \
        &&    make -j 4 ZEND_EXTRA_LIBS='-liconv' \
        &&    make install                        \
        &&    cp ${php_dir}/etc/php-fpm.conf.default ${php_dir}/etc/php-fpm.conf         \
        #&&    cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf  \
        #注意: 容器中启动php时,启动进程的参数,需要配置禁止后台运行 配置为no,否则启动失败
        &&    sed  -i "21a \daemonize = no"                 ${php_dir}/etc/php-fpm.conf  \
        &&    cp   php.ini-production                       ${php_dir}/etc/php.ini       \
        &&    sed  -i '/upload_max_filesize/s/2/50/'        ${php_dir}/etc/php.ini       \
        &&    sed  -i '/post_max_size/s/8/50/'              ${php_dir}/etc/php.ini       \
        &&    sed  -i "/expose_php/s/On/OFF/"               ${php_dir}/etc/php.ini       \
        &&    sed  -i '$a ;date.timezone=PRC\ndate.timezone = Asia/Shanghai'                              ${php_dir}/etc/php.ini \
        &&    sed  -i '$a ;关闭高危函数应用\ndisable_functions = passthru,exec,shell_exec,system,chroot'  ${php_dir}/etc/php.ini \
        &&    rm  -rf  /usr/local/src/apr-${apr_v}             \
        &&    rm  -rf  /usr/local/src/apr-util-${apr_util_v}   \
        &&    rm  -rf  /usr/local/src/httpd-${httpd_v}         \
        &&    rm  -rf  /usr/local/src/qdbm-${qdbm_v}           \
        &&    rm  -rf  /usr/local/src/mhash-${mhash_v}         \
        &&    rm  -rf  /usr/local/src/libmcrypt-${libmcrypt_v} \
        &&    rm  -rf  /usr/local/src/libiconv-${libiconv_v}

RUN     chown -R  apache.apache  ${httpd_dir}
WORKDIR ${httpd_dir}
EXPOSE  80
ENTRYPOINT  ["./bin/httpd", "-D", "FOREGROUND"]

1.7 mysql的Dockerfile文件的配置

mysql的编写,略,直接下载镜像启动即可
进入到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

1.8 docker-compose YML文件格式以及编写注意事项

YML数据结构通过缩进来表示,连续的项目通过减号来表示,键值对用冒号分隔,数组用中括号括起来,hash用花括号括起来。

YML文件格式注意事项:
1.3.1 不支持制表符tab键缩进,需要使用空格缩进
1.3.2 通常开头缩进2个空格
1.3.3 字符后缩进1个空格,如冒号,逗号,横杠
1.3.4 用井号注释
1.3.5 如果包含特殊字符用单引号引起来
1.3.6 布尔值(true,false,yes,no,on,off)必须用引号括起来,这样分析器会将他们解释为字符串。

1.9 docker-compose配置文件常用字段

配置文件常用字段:

字段描述
build dockerfile context 指定Dockerfile文件名构建镜像上下文路径
image 指定镜像
command 执行命令,覆盖默认命令
container_name 指定容器名称,由于容器名称是唯一的,如果指定自定义名称,则无法scale
deploy 指定部署和运行服务相关配置,只能在Swarm模式使用
enviroment 添加环境变量
networks 加入网络,引入自定义网络
ports 暴露端口,与-p相同,但端口不能低于60
volumes 挂载宿主机路径或命名卷。如果是命令卷在顶级volumes定义卷名称
restart 重启策略,默认no,此参数的作用是是否随着docker服务自启动,可选always/on-failure/unless-stopped
hostname 容器主机名

常用命令:

字段描述
build 重新构建服务
ps 列出容器
up 创建和启动容器,一般和 -d 结合使用,一个是前台运行,一个是后台运行
exec 在容器里执行命令
scale 指定一个服务容器启动数量
top 显示容器进程
logs 查看容器输出
down 删除容器,网络,数据卷和镜像
stop/start/restart 停止/启动/重启服务
config 验证并查看撰写文件

1.10 docker-copose.yml配置文件

此配置是管理多容器的一个工具

  • lamp环境yml配置文件

cat docker-compose-lamp.yml

#===============================================
#==========    定义了版本信息    ===============
version: '3'

#===============================================
#===========   Network Service   ===============
networks:
  lanmp: 
    driver: bridge
    ipam:
      config:
        - subnet: 192.168.20.0/24

#===============================================
#=========    定义了服务的配置信息    ==========
services:
  #Apache  Service
  compose-apache:
    hostname: compose-apache
    container_name: compose-apache
    restart: always
    image:  yangsir/apache-php:2.4.39_7.2.19
    build:
      context: ./apache_php
      dockerfile: Dockerfile
    ports:
      - 81:80
    networks:
      lanmp:
        ipv4_address: 192.168.20.14
    depends_on:
      - compose-mysql5.7
    environment:
      TZ: Asia/Shanghai
    volumes:
      - ./apache_php/config/conf81:/usr/local/httpd/conf
      - ./apache_php/applog:/applog
      - ./apache_php/webroot:/webroot
      - ./apache_php/php/php7.2/etc:/usr/local/php/etc
    #健康检查
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 5s
      timeout: 3s
      retries: 5

  #MySQL Service
  compose-mysql5.7:
    hostname: compose-mysql5.7
    container_name: compose-mysql5.7
    restart: always
    image: mysql:5.7
    ports:
      - 3306:3306
    networks:
      lanmp:
        ipv4_address: 192.168.20.13
    volumes:
      - ./mysql/mysql5.7/conf:/etc/mysql/conf.d
      - ./mysql/mysql5.7/data:/var/lib/mysql
    command: --character-set-server=utf8
    env_file:
      - ./mysql/mysql5.7/conf/env
  • lnmp环境yml配置文件

cat docker-compose-lnmp.yml

#===============================================
#==========    定义了版本信息    ===============
version: '3.0'

#===============================================
#===========   Network Service   ===============
networks:
  lanmp:
    driver: bridge
    ipam:
      config:
        - subnet: 192.168.20.0/24

#===============================================
#=========    定义了服务的配置信息    ==========
services:
  #Nginx Service
  compose-wxjh:
    hostname: compose-wxjh
    container_name: compose-wxjh
    restart: always
    image: yangsir/nginx-proxy:1.14.2
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 80:80
    networks:
      lanmp:
        ipv4_address: 192.168.20.11
    depends_on:
      - compose-php7.2
    environment:
      TZ: Asia/Shanghai
    volumes:
      - ./nginx/config/conf80:/usr/local/nginx/conf
      - ./nginx/applog:/applog
      - ./nginx/webroot:/webroot
    #健康检查
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 5s
      timeout: 3s
      retries: 5

  #Openresty Service
  compose-openresty:
    hostname: compose-openresty
    container_name: compose-openresty
    restart: always
    image: yangsir/openresty:1.13.6.2
    build:
      context: ./nginx
      dockerfile: dockerfile
    ports:
      - 84:80
    networks:
      lanmp:
        ipv4_address: 192.168.20.15
    depends_on:
      - compose-php7.2
    environment:
      TZ: Asia/Shanghai
    volumes:
      - ./nginx/config/conf84:/usr/local/openresty/nginx/conf
      - ./nginx/applog:/applog
      - ./nginx/webroot:/webroot
    #健康检查
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 5s
      timeout: 3s
      retries: 5

  #PHP Service
  compose-php7.2:
    hostname: compose-php7.2
    container_name: compose-php7.2
    restart: always
    image: yangsir/php-fpm:7.2.19
    build:
      context: ./php/php7.2
      dockerfile: Dockerfile
    ports:
      - 9000:9000
    networks:
      lanmp:
        ipv4_address: 192.168.20.12
    depends_on:
      - compose-mysql5.7
    environment:
      TZ: Asia/Shanghai
    volumes:
      - ./php/php7.2/etc:/usr/local/php/etc
      - ./nginx/webroot:/webroot
  #MySQL Service
  compose-mysql5.7:
    hostname: compose-mysql5.7
    container_name: compose-mysql5.7
    restart: always
    image: mysql:5.7
    ports:
      - 3306:3306
    networks:
      lanmp:
        ipv4_address: 192.168.20.13
    volumes:
      - ./mysql/mysql5.7/conf:/etc/mysql/conf.d
      - ./mysql/mysql5.7/data:/var/lib/mysql
    command: --character-set-server=utf8
    env_file:
      - ./mysql/mysql5.7/conf/env

cat ./mysql/mysql5.7/conf/env

TZ=Asia/Shanghai
MYSQL_ROOT_PASSWORD=admin
MYSQL_USER=user
MYSQL_PASSWORD=user123

修改默认的网络名称

# echo 'export COMPOSE_PROJECT_NAME="compose"' >> /etc/profile

eg: docker 网络名称类型如下
# docker network ls
ec4609c83ee5        compose_lanmp                   bridge              local

注意: networks 下面的lanmp 是 新创建的网络,将整套业务类型,都放在了同一个网络下面;
      利用lnmp环境时,一定要记住,nginx下面映射的目录一定也要在php服务下面做相同的映射,否则php解析不到文件   

1.11 启动容器工具进行安装

docker-compose.yml文件 见 1.5
注意: 通过 docker-compose.yml文件来创建容器,默认是会执行 docker-compose.yml文件,如果是默认的文件,可以省略掉 "-f docker-compose.yml",如果是其他的yml文件,或者路径不在当前路径下,需要用 -f 指定一下

前台执行:
docker-compose -f docker-compose.yml up
后台执行:
docker-compose -f docker-compose.yml up -d  

重启单个容器
docker-compose -f docker-compose.yml  restart  <Service_name>
 


 

posted @ 2019-07-24 15:24  淡淡定定  阅读(671)  评论(0编辑  收藏  举报