Mezzio with docker and nginx web server

Hi.
Are there some Docker images to work with Nginx and mezzio? Or do I need to create from scratch?

Thank you.

Any php-fpm image should work fine.

Marco Pivetta

1 Like

@ Sirpyerre
Hi - I wrote a post on setting up dockerized mezzio with nginx (will working php-8.1 and nginx dockerfiles and docker-compose).
^ This specific post is for mezzio/swoole variant specifically - bu I plan to follow up soon with php-fpm version as well soon.

https://bytepursuits.com/mezzio-using-swoole-openswoole-with-nginx-reverse-proxy-with-docker-containers-and-docker-compose

@ Sirpyerre here’s the example of how to set up nginx with php-fpm mezzio container (using docker containers):

https://bytepursuits.com/mezzio-using-phpfpm-with-nginx-reverse-proxy-with-docker-containers-and-docker-compose

I use this config to run mezzio/swoole as a microservice

add swoole with composer

composer require mezzio/mezzio-swoole

file config/autoload/swoole.global.php

<?php

use Mezzio\Swoole\Event\TaskEvent;
use Mezzio\Swoole\Task\DeferredServiceListenerDelegator;
use Mezzio\Swoole\Task\TaskInvokerListener;
use Psr\EventDispatcher\EventDispatcherInterface;
use SwooleTasks\ServicesTasks\Factory\RunServiceTaskFactory;
use SwooleTasks\ServicesTasks\Factory\ServicesTasksEventListenerFactory;
use SwooleTasks\ServicesTasks\RunServiceTask;
use SwooleTasks\ServicesTasks\ServicesTaskPayloadEvent;
use SwooleTasks\ServicesTasks\ServicesTasksEventListener;

return [

    'dependencies' => [
        'invokables' => [
            EventDispatcherInterface::class   => EventDispatcherInterface::class,
        ],
        'factories'  => [
            //ServicesTasksEventListener::class => ServicesTasksEventListenerFactory::class,
            //RunServiceTask::class             => RunServiceTaskFactory::class
        ],
        'delegators' => [
            //ServicesTasksEventListener::class => [
            //    DeferredServiceListenerDelegator::class,
            //]
        ],
    ],

    'mezzio-swoole' => [

        //'enable_coroutine'   => true   , //optional to enable coroutines and useful for tasks coroutines
        'swoole-http-server' => [

            'host' => 'localhost',
            'port' => 9501,
            //'mode' => SWOOLE_PROCESS,
            'mode' => SWOOLE_BASE,
            'protocol' => SWOOLE_SOCK_TCP,
            //'protocol' => SWOOLE_SOCK_TCP|SWOOLE_SSL,
            'options'  => [
                'enable_coroutine'      => false,
                'max_coroutine'         => 3000,

                'worker_num'            => 10,     // The number of HTTP Server Workers
                'task_worker_num'       => 0,     // The number of Task Workers
                'task_enable_coroutine' => false,  // optional to turn on task coroutine support
                'task_use_object'       => true,  // allow json object as string

                'max_request'           => 0,
                'max_request_grace'     => 0 / 2,

                'open_tcp_keepalive'       => false,   // Enable TCP Keep-Alive check
                'tcp_keepidle'             => 1,       // Check if there is no data for 4s.
                'tcp_keepinterval'         => 0.5,       // Check if there is data every 1s
                'tcp_keepcount'            => 1,       // Close the connection if there is no data for 5 cycles.

                'heartbeat_check_interval' => 5,
                'heartbeat_idle_time'      => 5,

                'max_conn'                 => 100_000,
                //'pipe_buffer_size'         => 32 * 1024*1024,
                //'socket_buffer_size'       => 128 * 1024*1024,

                'ssl_cert_file'            => __DIR__ . '/../../data/ssl/certificate.pem',
                'ssl_key_file'             => __DIR__ . '/../../data/ssl/certificate.key',

                /**
                 * ozzpy
                 * this do not work, because chrome(extension) detect man in the middle
                 * wireshark logs confirm it, and wrong hello response
                'ssl_cert_file' => __DIR__.'/config/ssl.crt',
                'ssl_key_file' => __DIR__.'/config/ssl.key',
                'ssl_verify_peer' => true,
                'ssl_allow_self_signed' => true,
                'ssl_client_cert_file' => __DIR__ . '/config/client.crt',
                 */

            ],

            'listeners' => [
                /*
                ServicesTaskPayloadEvent::class => [
                    ServicesTasksEventListener::class
                ],
                TaskEvent::class => [
                    TaskInvokerListener::class,
                    #TaskEventDispatchListener::class,
                ],
                */
            ],
        ]
    ],


];

Dockerfile

FROM php:8.0-cli

ENV TZ=America/Sao_Paulo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions swoole-4.8.2 uuid mcrypt intl mysqli pdo_mysql pcntl posix
RUN install-php-extensions @composer
RUN mkdir -p /var/www/app/

COPY ./php.ini "$PHP_INI_DIR/php.ini"
COPY . /var/www/app/
WORKDIR /var/www/app/

EXPOSE 9501
CMD ["php", "./public/index.php"]

Build image

docker build -t username/categories-micro .
docker push username/categories-micro

Run service on server or localhost

docker swarm init
docker network create -d overlay backend

docker login -u username -p [password]

docker pull username/categories-micro
docker service create --name categories-api-srv -p 9501:9501 --network backend --replicas 4 username/categories-micro

# running other microservice
docker service create --name sections-api-srv -p 9502:9501 --network backend --replicas 4 username/sections-micro

This code and image also works with kubernetes