Get started with Swoole 4.2.9 PHP 7.2 and Docker


This article introduce how to get started with Swoole without manually compile the Open Swoole extension.

Docker make it easy for us to try Swoole with only several commands.

Make sure you have installed Docker on your machine and updated the swoole version to be the latest version.

Folder structure:


Content of index.php

$http = new swoole_http_server("", 8101);

$http->on("start", function ($server) {
    echo "Swoole http server is started at\n";

$http->on("request", function ($request, $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");


Content of Swoole Dockerfile:

FROM php:7.2-fpm

RUN apt-get update

RUN apt-get install vim -y && \
    apt-get install openssl -y && \
    apt-get install libssl-dev -y && \
    apt-get install wget -y

RUN cd /tmp && wget && \
    tar zxvf swoole-4.2.9.tgz && \
    cd swoole-4.2.9  && \
    phpize  && \
    ./configure  --enable-openssl && \
    make && make install

RUN touch /usr/local/etc/php/conf.d/swoole.ini && \
    echo '' > /usr/local/etc/php/conf.d/swoole.ini

RUN mkdir -p /app/data


COPY ./app /app

CMD ["/usr/local/bin/php", "/app/index.php"]

Compile the Dockerfile:

It may take several minutes to compile the Dockerfile including Swoole PHP.

docker build -f ./Dockerfile -t hello-swoole .

Execute your application with Docker:

docker run -p 8101:8101 hello-swoole

Open your browser and open, you will be able to see the hello world response from Swoole server.

Remeber to kill the docker container with docker ps and docker kill once you have done the test.