docker-compose.yml构建php开发环境

docker-compose.yml

version: '3'
services:
  nginx:
    image: nginx:1.15.8
    restart: always
    depends_on:
      - php-fpm
      - mysql
      - redis
    ports:
      - 8080:80
      - 4431:443
    volumes:
      - /Users/feng/Documents/work/docker/html/:/var/www/html/
      - /Users/feng/Documents/work/docker/test/nginx/:/etc/nginx/
      - /Users/feng/Documents/work/docker/test/log/nginx/:/var/log/nginx/
    links:
      - php-fpm:fpm
    networks:
      - default

  php-fpm:
    image: hjflelo/hjf-php-fpm-7.4.25:v1
    expose:
      - 9000
    restart: always
    tty: true
    container_name: php-fpm
    volumes:
      - /Users/feng/Documents/work/docker/html/:/var/www/html/
      - /Users/feng/Documents/work/docker/test/php/etc/:/usr/local/etc/
      - /Users/feng/Documents/work/docker/test/log/php/:/var/log/php/
    links:
      - mysql:mysql
      - redis:redis
    networks:
      - default

  mysql:
    image: mysql:5.7.36
    restart: always
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - 33060:3306
    volumes:
      - /Users/feng/Documents/work/docker/test/mysql/data:/var/lib/mysql 
      - /Users/feng/Documents/work/docker/test/mysql/conf:/etc/mysql
      - /Users/feng/Documents/work/docker/test/log/mysql:/var/log/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=123456
    networks:
      - default

  redis:
    image: redis:latest
    restart: always
    command: redis-server --requirepass 123456 #给redis设置密码
    ports:
      - 63378:6379
    networks:
      - default

networks:
  default:
#docker-compose up -d 启动容器

nginx 配置连接php

location ~ \.php$ {
        #php 容器脚本的目录
        root /var/www/html/docker1.test.com;
        fastcgi_pass   php-fpm:9000; #php-fpm是php容器名称
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
        include        fastcgi_params;
    }

php 连接mysql

$mysqli = new mysqli('mysql', 'root', '123456', 'test');
if(mysqli_connect_errno())
{
    exit('Connect failed1:' . mysqli_connect_error());
}
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_errno);
    exit();
}

php连接redis

$redis = new redis();
$redis->connect('redis');
$redis->auth('123456');
echo "Server is running: " . $redis->ping();

你可能感兴趣的:(docker-compose.yml构建php开发环境)