Docker compose 編排LNP

mowang發表於2021-01-04
Docker-compose 編排 LNP
目錄結構
# 基礎目錄
/e/dq_lnp/
# 配置目錄
- mysql
- nginx
    -- conf.d
        --- dq.conf            # 擴充套件配置
    -- logs
        --- access.log
        --- error.log
    Dockerfile                # dockerfile檔案
    nginx.conf                # nginx配置檔案
- php
    -- logs
    Dockerfile                # dockerfile檔案
    php.ini                    # php配置檔案
    www.conf                
- www                        # www目錄
docker-compose.yml            # 編排檔案
php的dockerfile
# 基礎
FROM php:7.2-fpm
# 維護者
MAINTAINER dqdocker1@11.com
# 修改時區
ENV TZ Asia/Shanghai
RUN date -R

# 換源
RUN echo 'deb http://mirrors.ustc.edu.cn/debian stable main contrib non-free' >/etc/apt/sources.list
RUN echo 'deb http://mirrors.ustc.edu.cn/debian stable-updates main contrib non-free' >>/etc/apt/sources.list
RUN apt-get update --fix-missing && apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev  \
        && docker-php-ext-configure gd --with-freetype-dir=/usr/include --with-jpeg-dir=/usr/include/jpeg \
        && docker-php-ext-install gd mysqli opcache pdo_mysql gd zip

ENV PHPREDIS_VERSION 5.0.1
ENV PHPXDEBUG_VERSION 2.6.0
ENV PHPSWOOLE_VERSION 4.5.10

RUN pecl install redis-$PHPREDIS_VERSION \
    && pecl install xdebug-$PHPXDEBUG_VERSION \
    && pecl install swoole-$PHPSWOOLE_VERSION \
    && docker-php-ext-enable redis xdebug swoole

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
     && php composer-setup.php \
     && php -r "unlink('composer-setup.php');" \
     && mv composer.phar /usr/local/bin/composer \
     && composer config -g repo.packagist composer https://packagist.laravel-china.org
RUN apt-get install -y git

RUN rm -rf /var/cache/apt/* \
    && rm -rf /var/lib/apt/lists/*
RUN mkdir /var/lib/sessions \
    && chmod o=rwx -R /var/lib/sessions
#容器啟動時執行指令
CMD ["php-fpm"]
nginx的dockerfile
FROM nginx:1.15.5
MAINTAINER dqdocker1 mowang@qq.com
ENV TZ Asia/Shanghai
RUN date -R
CMD ["nginx", "-g", "daemon off;"]
docker-compose.yml 編排檔案
version: '3'
services:
  php7.2:
    build:
      context: ./php
      dockerfile: Dockerfile
    container_name: php7.2
    volumes:
      - /e/docker/dq_lnp/php/php.ini:/usr/local/etc/php/php.ini
      - /e/docker/dq_lnp/php/www.conf:/usr/local/etc/php-fpm.d/www.conf
      - /e/docker/dq_lnp/php/logs:/var/log/php
      - /e/docker/dq_lnp/www:/var/www/html
    ports:
      - 29000:9000
    depends_on:
      - nginx
    networks:
      - dq-lnp
  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    container_name: nginx
    ports:
      - 80:80
    volumes:
      - /e/docker/dq_lnp/www:/var/www/html
      - /e/docker/dq_lnp/nginx/logs:/var/log/nginx
      - /e/docker/dq_lnp/nginx/nginx.conf:/etc/nginx/nginx.conf
      - /e/docker/dq_lnp/nginx/conf.d:/etc/nginx/conf.d
    networks:
      - dq-lnp
networks:
  dq-lnp:
nginx配置檔案
user  www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;


    include /etc/nginx/conf.d/*.conf;

}
nginx/conf.d/dq.conf
server {
    charset utf-8;
    client_max_body_size 128M;

    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name mine.nginx.com;
    root        /var/www/html;
    index       index index.html index.php;

    location / {
        #-e表示只要filename存在,則為真
        if (!-e $request_filename){
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
        deny all;
    }

    location ~ \.php$ {
        root           /var/www/html;
        fastcgi_pass   php7.2:9000;                                #php7.2 為php的容器名稱
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }

    location ~* /\. {
        deny all;
    }
}
建立容器
docker-compose up -d
編排注意
  1. php.ini , www.conf 檔案需要開啟一個測試php容器中複製出來php.ini-development/php.ini-production,重新命名php.ini即可

  2. 我這個是在windows的環境下,好像docker無法掛載檔案,在第一次執行docker-compose up -d時無法掛載成功,需要刪除之後重新掛載一次

  3. 注意需要在php.ini檔案中開啟對應的擴充套件

    extension=php_pdo_mysql.dll
    extension=php_pdo_oci.dll
    extension=php_pdo_odbc.dll
    extension=php_pdo_pgsql.dll
    extension=php_pdo_sqlite.dll
    extension=php_pgsql.dll
    extension=php_redis.dll
常用命令
# 建立映象
docker-compose up -d
# 檢視容器
docker ps -a
# 如果有容器未啟動
docker start 容器名稱
# 重啟yml/關閉
docker-compose restart/stop
本作品採用《CC 協議》,轉載必須註明作者和本文連結
別問我八十年代的哪首歌

相關文章