1. 程式人生 > >Docker部署web環境之Lanmp

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-config-manager    --add-repo   https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce

注意:
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-ce 18.06版本rpm安裝
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

centos7系統直接yum安裝docker

注: 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@localhost compose_lanmp]# pwd
/app/docker_web/compose_lanmp
[root@localhost compose_lanmp]# ll
總用量 125724
drwxr-xr-x 7 root root      4096 7月  19 14:17 apache_php
-rw-r--r-- 1 root root      1456 7月  19 15:07 docker-compose-lamp.yml
-rw-r--r-- 1 root root      2244 7月  19 15:08 docker-compose-lnmp.yml
drwxr-xr-x 4 root root      4096 7月  19 11:56 mysql
drwxr-xr-x 5 root root      4096 7月  19 14:19 nginx
drwxr-xr-x 4 root root      4096 6月  28 15:46 php
-rwxr-xr-x 1 root root      1335 7月  19 14:26 README
drwxr-xr-x 4 3000 3000      4096 7月  19 11:55 webroot

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   /app1log/access.log  main;
    error_log    /app1log/error.log;
    root         /app1/public;  
    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   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=/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
  apache_web1:
    hostname: apache_web1
    container_name: compose-apache_web1
    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:
      - mysql5.7
    environment:
      TZ: Asia/Shanghai
    volumes:
      - ./apache_php/conf81:/usr/local/httpd/conf
      - ./apache_php/logs/app1log:/app1log
      - ./webroot/apache_web:/app1
      - ./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
  mysql5.7:
    hostname: mysql5.7
    container_name: compose-mysql5.7
    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
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: admin
      MYSQL_USER: user
      MYSQL_PASSWORD: user123
  • lnmp環境yml配置檔案

cat docker-compose-lnmp.yml

#===============================================
#==========    定義了版本資訊    ===============
version: '3'

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

#===============================================
#=========    定義了服務的配置資訊    ==========
services:
  #Nginx Service
  wxjh_web:
    hostname: wxjh_web
    container_name: compose-wxjh_web
    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:
      - php7.2
    environment:
      TZ: Asia/Shanghai
    volumes:
      - ./nginx/conf80:/usr/local/nginx/conf
      - ./nginx/logs/app1log:/app1log
      - ./webroot/px_wxjh_web:/app1
  #PHP Service
  php7.2:
    hostname: php7.2
    container_name: compose-php7.2
    image: yangsir/php-fpm:7.2.19
    build:
      context: ./php/php7.2
      dockerfile: Dockerfile
    networks:
      lanmp:
        ipv4_address: 192.168.20.12
    depends_on:
      - mysql5.7
    environment:
      TZ: Asia/Shanghai
    volumes:
      - ./php/php7.2/etc:/usr/local/php/etc
      - ./php/php7.2/log:/applog
      - ./webroot/px_wxjh_web:/app1

  #MySQL Service
  mysql5.7:
    hostname: mysql5.7
    container_name: compose-mysql5.7
    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
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: admin
      MYSQL_USER: user
      MYSQL_PASSWORD: user123

注意: 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>