docker-compose 简单使用例子 nginx与php-fpm通讯设置

柴岳
2023-12-01

示例

php lnmp+redis环境搭建使用了php7.3、php7.4

docker-compose.yaml

version: '3'

services:
  php74:
    build: 
      context: ./
      dockerfile: DockerPHP74
      args:
        PHP_VERSION: 7.4
    container_name: phpfpm74
    volumes:
      - ./www/php74/:/var/www/html/
      - ./phpconf/php74/:/usr/local/etc/
    privileged: true
    environment:
      TZ: Asia/Shanghai
    links:
      - redis
      - mysql
    networks:
      - back-tier
      - front-tier

  php73:
    build: 
      context: ./
      dockerfile: DockerPHP73
      args:
        PHP_VERSION: 7.3
    container_name: phpfpm73
    volumes:
      - ./www/php73/:/var/www/html/
      - ./phpconf/php73/:/usr/local/etc/
    privileged: true
    environment:
      TZ: Asia/Shanghai
    depends_on: 
      - redis
      - mysql
    links:
      - redis
      - mysql
    networks:
      - back-tier
      - front-tier

  redis:
    image: redis
    container_name: redis
    environment:
      TZ: Asia/Shanghai
    ports:
      - 6379:6379
    networks:
      - front-tier
  
  nginx:
    image: nginx
    ports:
      - 8080:80
      - 8081:8081
      - 8082:8082
    environment:
      TZ: Asia/Shanghai
    depends_on:
      - php73
      - php74
    links:
      - php74
      - php73
    volumes:
      - ./nginx/:/etc/nginx/
    container_name: nginx
    networks:
      - front-tier

  mysql:
    image: mysql:5.6
    ports:
      - 3306:3306
    container_name: mysql56
    volumes:
      - ./mysql/conf/:/etc/mysql/
      - ./mysql/data/:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      TZ: Asia/Shanghai
    networks:
      - front-tier

networks:
  front-tier:
    driver: bridge
  back-tier:
    driver: bridge

Dockerfile文件

php7.4

ARG PHP_VERSION=7.4
FROM php:${PHP_VERSION}-fpm
ENV TZ='Asia/Shanghai'
RUN apt-get update \
    && apt-get install -y apt-utils \
    && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libzstd-dev \
    && pecl install igbinary \
    && pecl install redis \
    && docker-php-ext-enable igbinary  \
    && docker-php-ext-enable redis \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql \
    && docker-php-ext-install bcmath \
    && kill -USR2 1

php7.3

ARG PHP_VERSION=7.3
FROM php:${PHP_VERSION}-fpm
ENV TZ='Asia/Shanghai'
RUN apt-get update \
    && apt-get install -y apt-utils \
    && apt-get install -y \
        apt-utils \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libwebp-dev \
        libzstd-dev \
    && docker-php-ext-configure gd --with-webp-dir=/usr/include/webp --with-jpeg-dir=/usr/include --with-png-dir=/usr/include --with-freetype-dir=/usr/include/freetype2 \
    && docker-php-ext-install gd \
     && pecl install igbinary \
     && pecl install redis \
     && docker-php-ext-enable igbinary redis \
    && docker-php-ext-install pdo_mysql \
    && docker-php-ext-install bcmath \
    && kill -USR2 1

php容器内重启php-fpm方法

# php-fpm的pid就是1
kill -USR2 1

php扩展安装参考

nginx配置文件

server{
    listen 8081;
    access_log logs/test_access.log;
    error_log logs/test_error.log;
    location ~ \.php$ {
    	# 这里的是php容器项目路径
        root /var/www/html;
        # 实用docker-compose中的php73、php74作为协议连接
        fastcgi_pass   php73:9000;
        fastcgi_index  index.php;
        # 这里的是php容器项目路径
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
        #fastcgi_param SCRIPT_FILENAME /www/index.php;
        include        /etc/nginx/fastcgi_params;
    }
}

重要

由于使用了数据卷命令从本地加载了配置文件,可注释掉docker-compose.yaml数据卷参数。或者单独启动对应容器,通过docker cp命令把配置文件从容器中copy到宿主机

volumes:
      - ./mysql/conf/:/etc/mysql/
      - ./mysql/data/:/var/lib/mysql
volumes:
      - ./www/php74/:/var/www/html/
      - ./phpconf/php74/:/usr/local/etc/
 .........
 类似资料: