Docker Compose 搭建 PHP 开发环境

Docker PHP 可以快速构建基于 Docker 的 PHP 本地开发环境,此套 LNMP 环境同时支持 PHP 5 和 PHP 7。请克隆此项目使用:https://github.com/findsec-cn/docker-php.git

启动服务

PHP/FPM 7.2/5.6、Nginx 1.12、Mysql 5.7、Redis 4.0、Memcached 1.5

目录结构

  1. ├── add_project.sh 新建项目脚本(Linux
  2. ├── build 镜像构建目录
  3. ├── memcached
  4. └── Dockerfile
  5. ├── mysql
  6. └── Dockerfile
  7. ├── nginx
  8. └── Dockerfile
  9. ├── php5
  10. └── Dockerfile
  11. ├── php7
  12. └── Dockerfile
  13. └── redis
  14. └── Dockerfile
  15. ├── config 服务配置目录
  16. ├── mysql
  17. ├── backup
  18. ├── config
  19. └── mysql.cnf
  20. ├── crontabs
  21. └── docker-entrypoint-initdb.d 数据库初始化脚本目录
  22. ├── nginx
  23. ├── conf.d
  24. ├── bar.example.com.conf
  25. ├── foo.example.com.conf
  26. └── example.com.conf.template
  27. ├── fastcgi_mysql
  28. ├── fastcgi_web
  29. └── nginx.conf
  30. ├── php5
  31. ├── php.ini
  32. └── php.ini-production
  33. ├── php7
  34. ├── php.ini
  35. └── php.ini-production
  36. └── redis
  37. └── redis.conf
  38. ├── data 服务数据目录
  39. ├── mysql 数据库数据存储目录
  40. ├── nginx
  41. ├── cache 应用缓存目录
  42. └── data 应用数据目录
  43. └── redis 缓存数据目录
  44. ├── docker-compose.yml 项目配置文件
  45. ├── logs 服务日志目录
  46. ├── access Nginx访问日志目录
  47. ├── bar.example.com
  48. └── bar.example.com.log
  49. └── foo.example.com
  50. └── foo.example.com.log
  51. ├── app 应用日志目录
  52. ├── bar.example.com
  53. └── foo.example.com
  54. └── srv 服务日志目录
  55. ├── memcached
  56. ├── mysql
  57. └── error.log
  58. ├── nginx
  59. └── nginx_error.log
  60. ├── php5
  61. └── php_errors.log
  62. ├── php7
  63. └── php_errors.log
  64. └── redis
  65. └── redis.log
  66. ├── README.md
  67. └── webapps 应用代码目录
  68. ├── bar.example.com
  69. └── htdocs
  70. └── index.php
  71. └── foo.example.com
  72. └── htdocs
  73. └── index.php

安装使用

需要先安装Docker,具体可参看:https://docs.docker.com/install/

  1. git clone https://github.com/findsec-cn/docker-php.git
  2. cd docker-php
  3. # 配置应用目录(重要)、数据库密码、端口等
  4. vim .env
  5. # 给组件文件夹可写权限
  6. sudo chmod -R 777 data logs
  7. # 构建镜像并启动容器
  8. sudo docker-compose up --build -d
  9. # 仅启动容器
  10. sudo docker-compose up -d
  11. # 单独编译PHP容器
  12. sudo docker-compose build php7
  13. # 停止开发环境
  14. sudo docker-compose stop
  15. # 启动开发环境
  16. sudo docker-compose start
  17. # 销毁开发环境
  18. sudo docker-compose down

创建新项目

  1. # ./add_project.sh <项目域名> <PHP 版本>
  2. # 默认新建PHP 7 项目
  3. ./add_project.sh foo.example.com 5

重启容器并访问项目访问:

修改hosts:

  1. 127.0.0.1 foo.example.com

访问地址:http://foo.example.com

.env配置文件

  1. # 项目放置目录(必须配置正确)
  2. GLOBAL_APP_PATH=/data1/docker-php
  3. # HTTP 本地映射端口
  4. HTTP_PORT=80
  5. HTTPS_PORT=443
  6. # MySQL 密码及端口
  7. MYSQL_PASSWORD=DockerLNMP
  8. MYSQL_PORT=3306
  9. # Reids 本地映射端口
  10. REDIS_PORT=6379
  11. # Memcached 本地映射端口
  12. MEMCACHED_PORT=11211

docker-compose.yml配置文件

  1. version: '2'
  2. services:
  3. nginx:
  4. depends_on:
  5. - php5
  6. - php7
  7. build:
  8. context: ./build/nginx
  9. privileged: true
  10. ports:
  11. - "${HTTP_PORT}:80"
  12. - "${HTTPS_PORT}:443"
  13. links:
  14. - php5
  15. - php7
  16. volumes:
  17. - ${GLOBAL_APP_PATH}/webapps:/data/webapps
  18. - ${GLOBAL_APP_PATH}/config/nginx/conf.d:/etc/nginx/conf.d
  19. - ${GLOBAL_APP_PATH}/config/nginx/nginx.conf:/etc/nginx/nginx.conf
  20. - ${GLOBAL_APP_PATH}/config/nginx/fastcgi_web:/etc/nginx/fastcgi_web
  21. - ${GLOBAL_APP_PATH}/config/nginx/fastcgi_mysql:/etc/nginx/fastcgi_mysql
  22. - ${GLOBAL_APP_PATH}/logs:/data/logs
  23. - ${GLOBAL_APP_PATH}/data/nginx/data:/data/data
  24. - ${GLOBAL_APP_PATH}/data/nginx/cache:/data/cache
  25. restart: always
  26. networks:
  27. - frontend
  28. php5:
  29. depends_on:
  30. - mysql
  31. - redis
  32. - memcached
  33. build:
  34. context: ./build/php5
  35. privileged: true
  36. ports:
  37. - "9000:9000"
  38. links:
  39. - "mysql"
  40. - "redis"
  41. - "memcached"
  42. volumes:
  43. - ${GLOBAL_APP_PATH}/webapps:/data/webapps
  44. - ${GLOBAL_APP_PATH}/config/php5/php.ini:/usr/local/etc/php/php.ini
  45. - ${GLOBAL_APP_PATH}/logs/srv/php5:/data/logs/srv/php
  46. restart: always
  47. networks:
  48. - frontend
  49. php7:
  50. depends_on:
  51. - mysql
  52. - redis
  53. - memcached
  54. build:
  55. context: ./build/php7
  56. privileged: true
  57. ports:
  58. - "9001:9000"
  59. links:
  60. - "mysql"
  61. - "redis"
  62. - "memcached"
  63. volumes:
  64. - ${GLOBAL_APP_PATH}/webapps:/data/webapps
  65. - ${GLOBAL_APP_PATH}/config/php7/php.ini:/usr/local/etc/php/php.ini
  66. - ${GLOBAL_APP_PATH}/logs/srv/php7:/data/logs/srv/php
  67. restart: always
  68. networks:
  69. - frontend
  70. mysql:
  71. build:
  72. context: ./build/mysql
  73. privileged: true
  74. ports:
  75. - "${MYSQL_PORT}:3306"
  76. volumes:
  77. - ${GLOBAL_APP_PATH}/data/mysql:/var/lib/mysql
  78. - ${GLOBAL_APP_PATH}/config/mysql/config/mysql.cnf:/etc/mysql/conf.d/mysql.cnf
  79. - ${GLOBAL_APP_PATH}/logs/srv/mysql:/var/log/mysql
  80. - ${GLOBAL_APP_PATH}/config/mysql/backup:/var/backup
  81. - ${GLOBAL_APP_PATH}/config/mysql/crontabs:/var/spool/cron/crontabs
  82. - ${GLOBAL_APP_PATH}/config/mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
  83. environment:
  84. MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
  85. restart: always
  86. networks:
  87. - backend
  88. redis:
  89. build:
  90. context: ./build/redis
  91. privileged: true
  92. ports:
  93. - "${REDIS_PORT}:6379"
  94. volumes:
  95. - ${GLOBAL_APP_PATH}/config/redis/redis.conf:/usr/local/etc/redis/redis.conf
  96. - ${GLOBAL_APP_PATH}/logs/srv/redis:/var/log/redis
  97. - ${GLOBAL_APP_PATH}/data/redis:/data
  98. restart: always
  99. networks:
  100. - backend
  101. memcached:
  102. build:
  103. context: ./build/memcached
  104. privileged: true
  105. ports:
  106. - "${MEMCACHED_PORT}:11211"
  107. volumes:
  108. - ${GLOBAL_APP_PATH}/logs/srv/memcached:/var/log/memcached
  109. restart: always
  110. networks:
  111. - backend
  112. networks:
  113. frontend:
  114. driver: bridge
  115. ipam:
  116. config:
  117. - subnet: 172.28.0.0/16
  118. backend:
  119. driver: bridge
  120. ipam:
  121. config:
  122. - subnet: 172.29.0.0/16