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



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.


  • jdiminic says:

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

    • Zaher says:

      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.

  • Peter says:

    Yes, it works. I am using Windows 10 Pro. Invoking the default Lumen welcome page with Apache Benchmark I get 280 req/sec.
    Command: ./ab -c 9 -n 200 “http://localhost/”
    Hardware: i7 3770 4C 3400MHz. I recommend Windows 10 Professional for docker, although it can run on Windows 10 Home as well, but with a different setup.
    Excellent article, btw. Thanks!

    • Zaher says:

      For development this should be good, but for production I do expect that you are using Linux for sure, as it will be more better than windows (that’s what I know)

  • Chamnab says:

    What is the best way to run it in production.

    • Zaher says:

      There is no direct answer to this question, as this depends on your production infrastructure, as for example some will put this image behind a loadbalancer, some will use Docker swarm or even K8s .. etc.

Leave a Reply to jdiminic Cancel reply

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

%d bloggers like this: