Home >PHP Framework >Swoole >Let's talk about how to build a swoole environment in docker

Let's talk about how to build a swoole environment in docker

青灯夜游
青灯夜游forward
2022-06-28 21:02:314130browse

How to build a swoole environment in docker? The following article will introduce to you how to use docker to build a swoole environment. I hope it will be helpful to you!

Let's talk about how to build a swoole environment in docker

Are you still troubled by the environment and scratching your head about compatibility? Docker can easily solve these problems. Hahaha. It turns out that docker is so powerful and solves environmental problems. I might have borrowed a new account and spent 180 yuan to buy a server in order to learn something and considered system problems. With docker, I saved another 100 yuan a year. Money, in the hot summer, I went to buy ice cream and fruits.

##1. Set up the swoole environment
拉取镜像
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"

2. Test
启动容器
docker run --rm -p 8000:9501 --name swoole -v /home/malina/project/swoole:/var/www 
phpswoole/swoole:4.5.9-php7.4
在/home/malina/project/swoole中编写server.php代码
<?php

$server = new Swoole\Server(&#39;0.0.0.0&#39;, 9504);
$server->on(&#39;Connect&#39;, function ($server, $fd){
  echo "client:connect\n";
});

$server->on(&#39;Receive&#39;, function ($server, $fd, $reactor_id, $data){
  $server->send($fd, "Server:{$data}");
});

$server->on("Close", function ($server, $fd){
  echo "Client:Close\n";
});

$server->start();
容器里执行:
php server.php
打开新窗口测试:
curl http://127.0.0.1:8000


The swwole server has been successfully set up here. Let’s continue by referring to the documentation and online examples.                                                                                                                                                                                                                                                                                                          

The above is the detailed content of Let's talk about how to build a swoole environment in docker. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete