Using Swoole PHP 7.2 Docker image

We all read about Swoole in Laravel-news article, and the results they provided looks nice, this pushed me to create a new Docker image which has PHP 7.2 CLI and Swoole complied with it directly.

In this article am going to show you how you can use it with a simple lumen application, and show you the results I got from using Swoole comparing to using nginx.

1. Creating Lumen Project

First of all, let’s create a simple Lumen project

2. Install Required Packages

Secondly, we need to install the lumen package for Swoole, and made small change

3. Register the Service Provider

Then we need to edit our bootstrap/app.php file and register Swoole service provider, just add the following line within Register Service Providers section

4. Prepare Our Dockerfile

Lastly, we need to prepare our simple Dockerfile which should live within our lumen project directory

5. Build the Docker image

Now we are ready to build our docker image

6. Run our Docker image

Once it’s finished you can run it with the command

Open your browser to localhost and you will be greeted with lumen default route.

Some tests with real data:

I did a small test with real data, to return 500 records at once from MySQL, and I tried hard to make the environment equal in both, but the only difference is that the nginx box is running PHP 7.1 which comes with alpine by default, meanwhile Swoole is running PHP 7.2

And this is what I got

Nginx:

Swoole:

Basically with Swoole, I was able to execute 160 requests per second, meanwhile, with Nginx, I was able to execute 3.18 requests per second.

Now it is up to you which one to use.

Filed under: Code, docker, Laravel, Nginx, PHP

2 Comments

  1. Is this work on windows? I tried but did not work wher I enter 6. Run our Docker image
    command

    • I didn’t test it on windows, but it should work as there is nothing special which should prevent it, it would help more if you can provide me with the error message you get.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.