石头记

Docker、Kubernetes、CI/CD 等技术分享

Docker Compose 搭建 PHP 开发环境

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

启动服务

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

目录结构

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

安装使用

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
git clone https://github.com/ist0ne/docker-php.git

cd docker-php

# 配置应用目录(重要)、数据库密码、端口等
vim .env

# 给组件文件夹可写权限
sudo chmod -R 777 data logs

# 构建镜像并启动容器
sudo docker-compose up --build -d

# 仅启动容器
sudo docker-compose up -d

# 单独编译PHP容器
sudo docker-compose build php7

# 停止开发环境
sudo docker-compose stop

# 启动开发环境
sudo docker-compose start

# 销毁开发环境
sudo docker-compose down

创建新项目

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

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

修改hosts:

1
127.0.0.1 foo.example.com

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

.env配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 项目放置目录(必须配置正确)
GLOBAL_APP_PATH=/data1/docker-php

# HTTP 本地映射端口
HTTP_PORT=80
HTTPS_PORT=443

# MySQL 密码及端口
MYSQL_PASSWORD=DockerLNMP
MYSQL_PORT=3306

# Reids 本地映射端口
REDIS_PORT=6379

# Memcached 本地映射端口
MEMCACHED_PORT=11211

docker-compose.yml配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
version: '2'

services:

nginx:
depends_on:
- php5
- php7
build:
context: ./build/nginx
privileged: true
ports:
- "${HTTP_PORT}:80"
- "${HTTPS_PORT}:443"
links:
- php5
- php7
volumes:
- ${GLOBAL_APP_PATH}/webapps:/data/webapps
- ${GLOBAL_APP_PATH}/config/nginx/conf.d:/etc/nginx/conf.d
- ${GLOBAL_APP_PATH}/config/nginx/nginx.conf:/etc/nginx/nginx.conf
- ${GLOBAL_APP_PATH}/config/nginx/fastcgi_web:/etc/nginx/fastcgi_web
- ${GLOBAL_APP_PATH}/config/nginx/fastcgi_mysql:/etc/nginx/fastcgi_mysql
- ${GLOBAL_APP_PATH}/logs:/data/logs
- ${GLOBAL_APP_PATH}/data/nginx/data:/data/data
- ${GLOBAL_APP_PATH}/data/nginx/cache:/data/cache
restart: always
networks:
- frontend

php5:
depends_on:
- mysql
- redis
- memcached
build:
context: ./build/php5
privileged: true
ports:
- "9000:9000"
links:
- "mysql"
- "redis"
- "memcached"
volumes:
- ${GLOBAL_APP_PATH}/webapps:/data/webapps
- ${GLOBAL_APP_PATH}/config/php5/php.ini:/usr/local/etc/php/php.ini
- ${GLOBAL_APP_PATH}/logs/srv/php5:/data/logs/srv/php
restart: always
networks:
- frontend

php7:
depends_on:
- mysql
- redis
- memcached
build:
context: ./build/php7
privileged: true
ports:
- "9001:9000"
links:
- "mysql"
- "redis"
- "memcached"
volumes:
- ${GLOBAL_APP_PATH}/webapps:/data/webapps
- ${GLOBAL_APP_PATH}/config/php7/php.ini:/usr/local/etc/php/php.ini
- ${GLOBAL_APP_PATH}/logs/srv/php7:/data/logs/srv/php
restart: always
networks:
- frontend

mysql:
build:
context: ./build/mysql
privileged: true
ports:
- "${MYSQL_PORT}:3306"
volumes:
- ${GLOBAL_APP_PATH}/data/mysql:/var/lib/mysql
- ${GLOBAL_APP_PATH}/config/mysql/config/mysql.cnf:/etc/mysql/conf.d/mysql.cnf
- ${GLOBAL_APP_PATH}/logs/srv/mysql:/var/log/mysql
- ${GLOBAL_APP_PATH}/config/mysql/backup:/var/backup
- ${GLOBAL_APP_PATH}/config/mysql/crontabs:/var/spool/cron/crontabs
- ${GLOBAL_APP_PATH}/config/mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
restart: always
networks:
- backend

redis:
build:
context: ./build/redis
privileged: true
ports:
- "${REDIS_PORT}:6379"
volumes:
- ${GLOBAL_APP_PATH}/config/redis/redis.conf:/usr/local/etc/redis/redis.conf
- ${GLOBAL_APP_PATH}/logs/srv/redis:/var/log/redis
- ${GLOBAL_APP_PATH}/data/redis:/data
restart: always
networks:
- backend

memcached:
build:
context: ./build/memcached
privileged: true
ports:
- "${MEMCACHED_PORT}:11211"
volumes:
- ${GLOBAL_APP_PATH}/logs/srv/memcached:/var/log/memcached
restart: always
networks:
- backend

networks:
frontend:
driver: bridge
ipam:
config:
- subnet: 172.28.0.0/16
backend:
driver: bridge
ipam:
config:
- subnet: 172.29.0.0/16

Proudly powered by Hexo and Theme by Hacker
© 2019 ist0ne