共计 5002 个字符,预计需要花费 13 分钟才能阅读完成。
这里应用 swoole 官网的镜像:https://github.com/swoole/doc…
版本阐明:
- latest:应用的是最新版本的 PHP 和 Swoole 的 master 分支
- php7.x:应用的是 7.x 版本的 PHP 和 Swoole 的 master 分支
- 4.x.x-php7.x:应用的是 7.x.xx 版本的 PHP 和 Swoole 的 4.x.x 分支
装置测试:
拉取指定版本的镜像:
docker pull phpswoole/swoole:4.5.9-php7.4
测试镜像环境:
docker run --rm phpswoole/swoole:4.5.9-php7.4 "php -m"
docker run --rm phpswoole/swoole:4.5.9-php7.4 "php --ri swoole"
docker run --rm phpswoole/swoole:4.5.9-php7.4 "composer --version"
根本应用:
创立 HTTP 服务器端:
# /d/swoole/www/server.php
#!/usr/bin/env php
<?php
declare(strict_types=1);
$http = new Swoole\Http\Server("0.0.0.0", 9501);
$http->on(
"request",
function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
$response->end(
<<<EOT
Hello, world!
EOT
);
}
);
$http->start();
启动容器:
docker run --rm -p 8000:9501 --name swoole -v /d/swoole/www:/var/www phpswoole/swoole:4.5.9-php7.4
测试拜访:
curl http://127.0.0.1:8000
容器启动后会尝试执行 php server.php
命令启动服务,所以无需手动进入容器执行。
应用 docker-compose
主动重启服务
创立 docker-compose.yml
文件:
# This docker-compose.yml file shows how Supervisor program(s) are reloaded automatically when file changes detected
# under web root /var/www. There are two environment variables used:
# 1. AUTORELOAD_PROGRAMS: space-separated Supervisor program(s) to be reloaded when file changes detected. e.g.,
# AUTORELOAD_PROGRAMS: "swoole" # Autoreload Supervisor program "swoole" only.
# AUTORELOAD_PROGRAMS: "swoole nginx" # Autoreload Supervisor program "swoole" and "nginx".
# 2. AUTORELOAD_ANY_FILES: Optional. If set to "true", "1", "yes", or "y", reload Supervisor program(s) when any
# files under the root directory (/var/www in this example) is changed; otherwise, reload only when PHP file(s)
# are changed.
#
version: '3'
services:
app:
build: .
environment:
AUTORELOAD_PROGRAMS: "swoole"
AUTORELOAD_ANY_FILES: 0
ports:
- 8000:9501
volumes:
- ./www:/var/www
同目录下创立 Dockerfile
文件:
FROM phpswoole/swoole:4.5.9-php7.4
# COPY ./www/ /var/www/
构建并启动容器:
docker-compose -f "docker-compose.yml" up --build --force-recreate -d
docker-compose -f "docker-compose.yml" stop
docker-compose
参数阐明:
-f, –file FILE 指定 Compose 模板文件,默认为 docker-compose.yml
docker-compose up
参数阐明:
-–build 在启动容器前构建服务镜像
–force-recreate 强制从新创立容器,不能与–no-recreate 同时应用
-d 在后盾运行服务容器并打印名称
主动执行 php server.php
探索
官网镜像的 Dockerfile
中,构建实现后会执行以下内容:
# dockerfiles\4.5.9\php7.4\cli\Dockerfile
ENTRYPOINT ["/entrypoint.sh"]
CMD []
WORKDIR "/var/www/"
而后会去扫描 usr/local/boot/
目录下的脚本并执行:
# rootfilesystem\entrypoint.sh
# Now run .php and .sh scripts under folder /usr/local/boot in order.
boot_scripts=()
shopt -s nullglob
for f in /usr/local/boot/*.sh ; do
boot_scripts+=("$f")
done
shopt -u nullglob
IFS=$'\n' boot_scripts=($(sort <<<"${boot_scripts[*]}"))
unset IFS
for f in "${boot_scripts[@]}" ; do
. "$f"
done
而后会去扫描 etc/supervisor/service.d/
目录下的配置并执行:
# usr\local\boot\supervisor.sh
case "${BOOT_MODE}" in
"SERVICE")
echo "INFO: Supervisord configuration files copied from folder'/etc/supervisor/${BOOT_MODE,,}.d/'."
find /etc/supervisor/${BOOT_MODE,,}.d/ -mindepth 1 -type f -name "*.conf" -exec cp -t /etc/supervisor/conf.d/ {} +
;;
在配置中执行了执行的命令为 php /var/www/server.php
C:\Users\Administrator\Downloads\docker-swoole-master\rootfilesystem\etc\supervisor\service.d\swoole.conf
[supervisord]
user = root
[program:swoole]
command = php /var/www/server.php
user = root
其余官网提供的 compose
-
Basic examples:
- 00-autoload: Restart the Swoole web server automatically if file changes detected under web root.
- 01-basic: print out “Hello, World!” using Swoole as backend HTTP server.
- 02-www: to use some customized PHP script(s) in the Docker image built.
- 03-nginx: to use Swoole behind an Nginx server.
- 04-entrypoint: to use a self-defined entrypoint script in the Docker image built.
- 05-boot: to update content in the Docker container through a booting script.
- 06-update-token: to show how to update server configurations with built-in script update-token.sh.
- 07-disable-default-server: Please check the docker-compose.yml file included to see show how to disable the default web server created with Swoole.
-
Manage PHP extensions and configurations:
- 10-install-php-extension: how to install/enable PHP extensions.
- 11-customize-extension-options: how to overwrite/customize PHP extension options.
- 12-php.ini: how to overwrite/customize PHP options.
- 13-install-swoole-extension: Please check the README file included to see how to install Swoole extensions like async, orm, postgresql, and serialize.
- 14-install-phpx: Please check the README file included to see how to install PHP-X.
- 15-install-phpx-extension: Please check the README file included to see how to install PHP-X based extensions like zookeeper.
-
Manage Supervisord programs:
- 20-supervisord-services: to show how to run Supervisord program(s) in Docker.
- 21-supervisord-tasks: to show how to run Supervisord program(s) when launching a one-off command with Docker. Please check the README file included to see how to run the example.
- 22-supervisord-enable-program: to show how to enable program(s) in Supervisord program.
- 23-supervisord-disable-program: to show how to disable Supervisord program(s).
-
Debugging:
- 30-debug-with-gdb: Please check the README file included to see how to debug your PHP code with gdb.
- 31-debug-with-valgrind: Please check the README file included to see how to debug your PHP code with Valgrind.
- 32-debug-with-strace: Please check the README file included to see how to debug your PHP code with strace.
- 33-debug-with-blackfire: Please check the README file included to see how to debug your PHP code with Blackfire.
- 34-debug-with-sdebug: Please check the README file included to see how to debug your PHP code in different ways with sdebug (forked from Xdebug to work with Swoole).
正文完