DNMP集成环境

准备工作

镜像

nginx:latest
php:7.3-fpm
mysql:5.7
redis:latest

文件目录结构

目录名 作用
conf 存放服务的配置文件
log 存放服务的日志文件
docker 存放需要构建的Dockerfile
www 项目目录
docker-compose.yml 配置

配置

docker-compose.yml

version: "3"
services:
  mysql:
    image: mysql:${MYSQL_VERSION}
    container_name: "mysql" # 容器名称
    restart: always
    ports:
      - "${MYSQL_HOST_PORT}:3306" # 映射端口
    volumes:
      - ${MYSQL_DATA_DIR}:/var/lib/mysql:rw # 挂载文件夹
      - ${MYSQL_CONF_FILE}:/etc/mysql/conf.d/mysql.cnf:ro # 挂载文件夹
    environment:
      - TZ=Asia/Shanghai  # 设置时区
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} # 设置mysqlRoot密码
  redis:
    image: redis:${REDIS_VERSION}
    container_name: "redis"
    ports:
      - "${REDIS_HOST_PORT}:6379"
    volumes:
      - ${REDIS_DATA_DIR}:/data/:rw
      - ${REDIS_CONF_FILE}:/etc/redis/redis.conf:ro
    command: redis-server /etc/redis/redis.conf
    restart: always
    environment:  # 配置环境变量
      - TZ=Asia/Shanghai  # 设置时区
  php:
    build: ./docker/php-swoole
    container_name: "php"
    restart: always
    ports:
      - "9000:9000"
    volumes:
      - ${APP_DIR}:/var/www/html:rw # 挂载项目目录
      - ${PHP72_PHP_CONF_FILE}:/usr/local/etc/php/php.ini:ro # 挂载配置文件
      - ${PHP72_OPENSSL_FILE}:/usr/local/openssl/cert.pem:ro # 挂载配置文件
    links:
      - "mysql"
      - "redis"
    #      - "myes"
    #      - "mymq"
    environment:  # 配置环境变量
      - TZ=Asia/Shanghai  # 设置时区
  nginx:
    image: nginx:${NGINX_VERSION}
    container_name: "nginx"
    restart: always
    ports:
      - "80:80"
    volumes:
      - ${APP_DIR}:/usr/share/nginx/html:rw
      - ${NGINX_CONF_FILE}:/etc/nginx/nginx.conf/:ro
      - ${NGINX_CONFD_DIR}:/etc/nginx/conf.d/:rw
      - ${NGINX_LOG_DIR}:/var/log/nginx/:rw
    environment:  # 配置环境变量
      - TZ=Asia/Shanghai  # 设置时区
    links:
      - "php"

php-swoole-Dockerfile

FROM php:7.3-fpm-alpine

ENV SWOOLE_VERSION=4.5.3
ENV PHP_REDIS=5.3.1

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# Libs
RUN apk update
RUN apk add --no-cache curl wget vim gcc g++ autoconf make \
    openssl-dev \
    librdkafka-dev \
    libmcrypt-dev \
    libxml2-dev \
    icu-dev \
    libzip-dev \
    libpng-dev

RUN echo "Asia/Shanghai" > /etc/timezone

# Extensions
RUN docker-php-ext-install gd bcmath opcache mysqli pdo pdo_mysql sockets zip


RUN echo "Asia/Shanghai" > /etc/timezone

# Composer
RUN curl -sS https://getcomposer.org/installer | php \
    && mv composer.phar /usr/local/bin/composer \
    && composer self-update --clean-backups \
    && composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/


# Extension redis bcmath mongodb rdkafka
RUN pecl install redis mcrypt mongodb rdkafka \
        && docker-php-ext-enable redis mcrypt mongodb rdkafka

# Redis extension
RUN wget http://pecl.php.net/get/redis-${PHP_REDIS}.tgz -O /tmp/redis.tar.tgz \
    && pecl install /tmp/redis.tar.tgz \
    && rm -rf /tmp/redis.tar.tgz \
    && docker-php-ext-enable redis

# XDEBUG extension
# RUN wget https://xdebug.org/files/xdebug-${PHP_XDEBUG}.tgz -O /tmp/xdebug.tar.tgz \
#     && pecl install /tmp/xdebug.tar.tgz \
#     && rm -rf /tmp/xdebug.tar.tgz \
#     && docker-php-ext-enable xdebug

# Hiredis
#RUN wget https://github.com/redis/hiredis/archive/v${HIREDIS_VERSION}.tar.gz -O hiredis.tar.gz \
#    && mkdir -p hiredis \
#    && tar -xf hiredis.tar.gz -C hiredis --strip-components=1 \
#    && rm hiredis.tar.gz \
#    && ( \
#        cd hiredis \
#        && make -j$(nproc) \
#        && make install \
#        && ldconfig \
#    ) \
#    && rm -r hiredis

# Swoole extension
RUN wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz \
    && mkdir -p swoole \
    && tar -xf swoole.tar.gz -C swoole --strip-components=1 \
    && rm swoole.tar.gz \
    && ( \
    cd swoole \
        && phpize \
        && ./configure --enable-async-redis --enable-mysqlnd --enable-openssl --enable-http2 \
        && make -j$(nproc) \
        && make install \
    ) \
    && rm -r swoole \
    && docker-php-ext-enable swoole

RUN php -m

EXPOSE 9500 9501 9502 9503 9504

注意

nginx在配置fpm转发时

fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
# 不能使用$document_root需要使用php挂载的项目目录

需要ssl证书时

查看证书配置的位置
使用 php函数openssl_get_cert_locations(),也可以到php.ini文件中寻找到。
在ssh中输入如下命令php -r "print_r(openssl_get_cert_locations());"

证书地址

配置挂载该文件即可。

安装拓展

docker-php-ext-install 拓展名

详情可看↓

Docker php安装扩展步骤详解