-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Docker PHP best practices #187
* Update Docker PHP best practices #187
- Loading branch information
Showing
30 changed files
with
508 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.git | ||
.ide | ||
.idea | ||
vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
workspace: | ||
base: /app | ||
path: . | ||
|
||
# | ||
# khs1994.com PHP CI/CD 由 khs1994-docker/ci 提供。 | ||
# | ||
# @link https://github.com/khs1994-docker/ci | ||
# | ||
|
||
pipeline: | ||
# | ||
# 测试 PHP 项目 | ||
# | ||
php: | ||
image: khs1994/php-fpm:${PHP_VERSION} | ||
# pull: true | ||
commands: | ||
- pwd | ||
- composer install -q | ||
- composer update -q | ||
# | ||
# 服务初始化需要时间,这里延迟执行 | ||
# | ||
- sleep 20 | ||
- vendor/bin/phpunit | ||
volumes: | ||
# - /Users/khs1994/khs1994/khs1994-lnmp/tmp/cache:/tmp/cache | ||
- /data/lnmp/tmp/cache:/tmp/cache | ||
|
||
# | ||
# CI/CD 构建预上线环境 PHP NGINX 镜像 | ||
# | ||
publish: | ||
image: plugins/docker | ||
registry: docker.khs1994.com | ||
repo: khs1994/php-fpm | ||
tags: swarm-7.2.2-alpine3.7-${DRONE_TAG} | ||
when: | ||
# | ||
# 手动在命令行执行以下命令才会执行 publish | ||
# | ||
# $ drone deploy khs1994-php/demo 24 staging | ||
# | ||
# | ||
event: deployment | ||
environment: staging | ||
secrets: [ docker_usernmae, docker_password ] | ||
|
||
# | ||
# CI/CD 构建生产环境 PHP NGINX 镜像 | ||
# | ||
publish_to_prod: | ||
image: plugins/docker | ||
registry: docker.khs1994.com | ||
repo: khs1994/php-fpm | ||
tags: swarm-7.2.2-alpine3.7-${DRONE_TAG} | ||
when: | ||
# | ||
# 手动在命令行执行以下命令才会执行 publish | ||
# | ||
# $ drone deploy khs1994-php/demo 24 production | ||
# | ||
# | ||
event: deployment | ||
environment: production | ||
secrets: [ docker_usernmae, docker_password ] | ||
|
||
services: | ||
mysql: | ||
image: mysql:8.0.3 | ||
environment: | ||
- MYSQL_DATABASE=test | ||
- MYSQL_ROOT_PASSWORD=mytest | ||
|
||
redis: | ||
image: redis:4.0.8-alpine | ||
|
||
matrix: | ||
PHP_VERSION: | ||
- 7.2.2-alpine3.7 | ||
# - 7.1.0-alpine3.4 | ||
# - 7.0.0-alpine3.4 | ||
# - 5.6.0-alpine3.4 | ||
|
||
branches: | ||
include: [ master, dev, feature/* ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
PHP_VERSION=7.2.2 | ||
NGINX_VERSION=1.13.9 | ||
|
||
# | ||
# 项目使用哪个 PHP 版本就保留哪个变量,其他注释 | ||
# | ||
|
||
PHP_VERSION_7_2_X=7.2.2 | ||
|
||
# PHP_VERSION_7_1_X=7.1.0 | ||
|
||
# PHP_VERSION_7_0_X=7.0.0 | ||
|
||
# PHP_VERSION_5_6_X=5.6.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
preset: laravel | ||
|
||
linting: true | ||
|
||
enabled: | ||
- strict | ||
- unalign_double_arrow | ||
- phpdoc_order | ||
- phpdoc_separation | ||
|
||
disabled: | ||
- short_array_syntax | ||
- not_operator_with_successor_space | ||
|
||
finder: | ||
name: | ||
- "*.php" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
language: php | ||
# cache: | ||
# directories: | ||
# - vendor | ||
php: | ||
# - 5.6 | ||
# - 7.0 | ||
- 7.1 | ||
- 7.2 | ||
script: | ||
- composer self-update | ||
- composer install -q | ||
- composer update -q | ||
- vendor/bin/phpunit | ||
# | ||
# 设置数据库 | ||
# | ||
# @link https://docs.travis-ci.com/user/database-setup/ | ||
# | ||
branches: | ||
only: | ||
- master | ||
- dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# CHANGELOG | ||
|
||
## YY.MM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,75 @@ | ||
FROM khs1994/php-fpm:7.2.2-alpine3.7 | ||
# | ||
# 此 Dockerfile 使用了多阶段构建,同时构建了 PHP 及 NGINX 两个镜像 | ||
# | ||
# @link https://docs.docker.com/develop/develop-images/multistage-build/ | ||
# | ||
|
||
ARG PHP_VERSION=7.2.2-alpine3.7 | ||
|
||
ARG NGINX_VERSION=1.13.9 | ||
|
||
# | ||
# 安装 composer 依赖 | ||
# | ||
|
||
FROM khs1994/php-fpm:${PHP_VERSION} as composer | ||
|
||
COPY . /app/demo/ | ||
|
||
RUN cd /app/demo \ | ||
# | ||
# 安装 composer 依赖 | ||
# | ||
&& if [ -f composer.json ];then \ | ||
echo "Compose packages installing..."; \ | ||
echo "Composer packages installing..."; \ | ||
composer install --no-dev; \ | ||
echo $?; \ | ||
echo "Composer packages install success"; \ | ||
else \ | ||
echo "composer.json NOT exists"; \ | ||
fi | ||
|
||
FROM khs1994/php-fpm:7.2.2-alpine3.7 | ||
# | ||
# 将 PHP 项目打入 PHP 镜像 | ||
# | ||
|
||
FROM khs1994/php-fpm:${PHP_VERSION} as php | ||
|
||
COPY --from=composer /app /app | ||
|
||
CMD ["php-fpm", "-R", "-d", "variables_order='EGPCS'"] | ||
|
||
# | ||
# $ docker build -t khs1994/php-fpm:swarm-7.2.2-alpine3.7 --target=php . | ||
# | ||
# @link https://docs.docker.com/develop/develop-images/multistage-build/#stop-at-a-specific-build-stage | ||
# | ||
|
||
# | ||
# 将 PHP 项目及 NGINX 配置文件打入 NGINX 镜像 | ||
# | ||
|
||
# | ||
# Debian 镜像缺少网络相关工具,这里为了方便测试 LinuxKit 继续使用 Alpine 镜像 | ||
# | ||
|
||
# FROM khs1994/nginx:1.13.9-tls1.3-stretch | ||
|
||
FROM nginx:${NGINX_VERSION}-alpine as nginx | ||
|
||
RUN rm -rf /etc/nginx/conf.d | ||
|
||
COPY demo-php.conf /etc/nginx/conf.d/ | ||
|
||
COPY --from=php /app /app | ||
|
||
ADD https://raw.githubusercontent.com/khs1994-docker/lnmp/master/config/etc/nginx/nginx.production.conf /etc/nginx/nginx.conf | ||
|
||
ADD https://raw.githubusercontent.com/khs1994-docker/lnmp-nginx-conf-demo/master/wait-for-php.sh /wait-for-php.sh | ||
|
||
COPY --from=0 /app /app | ||
CMD ["/wait-for-php.sh"] | ||
|
||
CMD ["php-fpm", "-R"] | ||
# | ||
# $ docker build -t khs1994/nginx:swarm-1.13.9-alpine . | ||
# | ||
# @link | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,135 @@ | ||
示例 PHP 文件,测试各组件功能,访问本机 IP 即可。 | ||
# Docker 化 PHP 项目最佳实践 | ||
|
||
完全使用 Docker 开发、部署 PHP 项目 | ||
|
||
* [问题反馈](https://github.com/khs1994-docker/lnmp/issues/187) | ||
|
||
## 开发 | ||
|
||
### 环境 | ||
|
||
* LNMP [khs1994-docker/lnmp](https://github.com/khs1994-docker/lnmp) | ||
|
||
* IDE `PHPStorm` | ||
|
||
### 1.新建 PHP 项目 | ||
|
||
```bash | ||
$ cd lnmp | ||
|
||
$ mkdir -p app/demo | ||
|
||
$ echo -e "<?php\nphpinfo();" >> app/demo/index.php | ||
``` | ||
|
||
### 2.新增 NGINX 配置 | ||
|
||
参考示例配置文件在 `config/nginx` 新建 `php.conf` NGINX 配置文件 | ||
|
||
### 3.启动 khs1994-docker/lnmp | ||
|
||
```bash | ||
$ ./lnmp-docker.sh development | ||
``` | ||
|
||
### 4.浏览器验证 | ||
|
||
浏览器打开页面,出现 php 信息 | ||
|
||
### 5.PHPStorm 打开已有项目 | ||
|
||
### 6.设置 CLI | ||
|
||
`PHPStorm 设置`-> `Languages & ...` -> `PHP` -> `CLI Interpreter` -> `点击后边三个点` | ||
-> `左上角添加` -> `From Docker ...` -> `Remote` -> `选择 Docker` | ||
-> `Image name` -> `选择 khs1994/php-fpm:7.2.2-alpine3.7` | ||
-> `点击 OK 确认` | ||
|
||
### 7.设置 xdebug | ||
|
||
请查看 https://github.com/khs1994-docker/lnmp/blob/master/docs/xdebug.md | ||
|
||
### 8.引入 Composer 依赖 | ||
|
||
容器化 PHPer 常用命令请查看 https://github.com/khs1994-docker/lnmp/blob/master/docs/command.md | ||
|
||
```bash | ||
$ lnmp-composer require phpunit/phpunit | ||
``` | ||
|
||
### 9.编写 PHP 代码 | ||
|
||
### 10.编写 PHPUnit 测试代码 | ||
|
||
### 11.使用 PHPUnit 测试 | ||
|
||
#### 使用 PHPStorm | ||
|
||
`PHPStorm 设置`-> `Languages & ...` -> `PHP` ->`Test Frameworks` -> `左上角添加` | ||
-> `PHPUnit by Remote Interpreter` -> `选择第五步添加的 Docker 镜像` | ||
-> `点击 OK` -> `PHPUnit Library` -> `选择 Use Composer autoloader` | ||
-> `Path to script` -> `填写 /opt/project/vendor/autoload.php` | ||
-> `点击右边刷新` -> `点击 OK 确认` | ||
|
||
|
||
在测试函数名单击右键 `run FunName` 开始测试。 | ||
|
||
#### 使用命令行 | ||
|
||
```bash | ||
$ lnmp-phpunit | ||
``` | ||
|
||
### 12.测试构建 PHP 及 NGINX 镜像 | ||
|
||
自行修改 `.env` `docker-compose.yml` 文件,保留所需的 PHP 版本,其他的注释 | ||
|
||
```bash | ||
$ docker-compose build | ||
``` | ||
|
||
### 13.将项目提交到 Git | ||
|
||
```bash | ||
$ git init | ||
|
||
$ git add . | ||
|
||
$ git commit -m "First" | ||
|
||
$ git remote add origin GIT_URL | ||
|
||
$ git push origin master | ||
``` | ||
|
||
## 测试(全自动) | ||
|
||
### 1.Git 通知到 CI/CD 服务器 | ||
|
||
* Travis CI (公共的、仅支持 GitHub CI/CD) | ||
|
||
* Drone (私有化 CI/CD) | ||
|
||
### 2. CI/CD 服务器测试 | ||
|
||
## 开发、测试循环 | ||
|
||
## git 添加 tag | ||
|
||
只有添加了 `tag` 的代码才能部署 | ||
|
||
Docker 镜像名包含 git `tag` | ||
|
||
## 部署 (全自动) | ||
|
||
生产环境部署 [khs1994-docker/lnmp](https://github.com/khs1994-docker/lnmp) 请查看 https://github.com/khs1994-docker/lnmp/tree/master/docs/production | ||
|
||
### 1. CI/CD 服务器构建 Docker 镜像 | ||
|
||
### 2. CI/CD 服务器推送 Docker 镜像到私有 Docker 仓库 | ||
|
||
### 3.Docker 私有仓库通知到指定地址 | ||
|
||
### 4.Swarm mode 或 k8s 集群自动更新服务 | ||
|
||
### 5.完成部署 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
{ | ||
"require": { | ||
"php-amqplib/php-amqplib": "2.7.*" | ||
"php-amqplib/php-amqplib": "2.7.*", | ||
"phpunit/phpunit": "^7.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "7.*.*" | ||
} | ||
} |
Oops, something went wrong.