MacOS下的docker-compose部署多php开发环境

@高效码农  September 8, 2022

简介

MacOS下PHP开发的集成环境MAMP(收费)、XAMPP(不够灵活);Brew安装(不够灵活)phpstudy(不够成熟)

Docker安装

官网:https://hub.docker.com/ 下载安装

前期准备

1、新建目录docker-php(可自定义)
2、在docker-php中新建nginx目录和www目录
3、在nginx目录中新建conf和logs目录

在docker-php/nginx/conf目录创建文件php.conf

server {
    listen    80;
    server_name    localhost;
 
    location / {
        root    /usr/share/nginx/html;
        index    index.html index.htm index.php;
    }
 
    error_page    500 502 503 504    /50x.html;
    location = /50x.html {
        root    /usr/share/nginx/html;
    }
    location ~ .php$ {
        fastcgi_pass    php:9000;
        fastcgi_index    index.php;
        fastcgi_param    SCRIPT_FILENAME    /web/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

编写docker-compose72.yml文件和docker-compose56.yml文件

一定注意格式,且文件存放在docker-php目录下
# docker-compose72.yml:
version: "2.3"
services:
  nginx:
    image: nginx
    privileged: true
    ports: 
      - "80:80"
    volumes: 
      - /Users/{电脑用户名}/docker-php/www/web:/usr/share/nginx/html
      - /Users/{电脑用户名}/docker-php/nginx/conf:/etc/nginx/conf.d
      - /Users/{电脑用户名}/docker-php/nginx/logs:/var/log/nginx
    networks:
      - web-net
  php:
    image: phpdockerio/php72-fpm
    privileged: true
    volumes:
      - /Users/{电脑用户名}/docker-php/www/web:/web
    networks:
      - web-net
  mysql:
    hostname: mysql
    image: mysql:5.7.26
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root
    networks:
      - web-net
networks: 
  web-net:
# docker-compose56.yml:
version: "2.3"
services:
  nginx:
    image: nginx
    privileged: true
    ports: 
      - "80:80"
    volumes: 
      - /Users/{电脑用户名}/docker-php/www/web:/usr/share/nginx/html
      - /Users/{电脑用户名}/docker-php/nginx/conf:/etc/nginx/conf.d
      - /Users/{电脑用户名}/docker-php/nginx/logs:/var/log/nginx
    networks:
      - web-net
  php:
    image: php:5.6-fpm
    privileged: true
    volumes:
      - /Users/{电脑用户名}/docker-php/www/web:/web
    networks:
      - web-net
  mysql:
    hostname: mysql
    image: mysql:5.7.26
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root
    networks:
      - web-net
networks: 
  web-net:

使用方式:

在docker-php目录下执行

 # 关闭所有容器
 docker-compose down
 # -d参数让程序运行在后台
 docker-compose -f docker-compose72.yml up -d

切换php版本

 # 关闭所有容器
 docker-compose down
 # -d参数让程序运行在后台
 docker-compose -f docker-compose56.yml up -d


评论已关闭

  1. 你的博客也有3年多了,
    现在坚持写博客的人超来越少了。