在 docker 中搭建 swoole 运行环境

码农天地 -
在 docker 中搭建 swoole 运行环境

这里使用 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).
特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

php介绍

PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。利于学习,使用广泛,主要适用于Web开发领域。

Tags 标签

phpdocker容器swoole

扩展阅读

加个好友,技术交流

1628738909466805.jpg