Docker images/nginx-php

From Nasqueron Agora
Revision as of 14:48, 13 February 2022 by Dereckson (talk | contribs) (Created page with "The nasqueron/nginx-php-fpm and nasqueron/nginx-php7-fpm images provide a solution to run PHP applications as a standalone container. == Project goals == While Java, Ruby, Go and Python applications ship with their own server, PHP applications generally needs a webserver to call a FastCGI process, and handle static data. This make harder to adhere to the [https://12factor.net/ twelve-factor app methodology]. Meanwhile Docker encourages to run each process in their own...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The nasqueron/nginx-php-fpm and nasqueron/nginx-php7-fpm images provide a solution to run PHP applications as a standalone container.

Project goals

While Java, Ruby, Go and Python applications ship with their own server, PHP applications generally needs a webserver to call a FastCGI process, and handle static data. This make harder to adhere to the twelve-factor app methodology.

Meanwhile Docker encourages to run each process in their own container. As such we want to provide 3 options:

  1. Easy deployment of a unique container for the application listening to the port 80: runit, nginx, php-fpm [DONE, IN PROD]
  2. Deployment of a service composed of two containers, one per process: the same image can be used for both nginx and php-fpm containers. [DONE, READY FOR TESTING]
  3. Deployment of only php-fpm, and a standard to publish metadata so a front-end can pick configuration. [UNDER INVESTIGATION]

The PHP offers a fairly comprehensive distribution, providing standard extensions like MySQL and PostgreSQL drivers, XML and internalization ones. We also provide widely used composer and APCu.

Howto

Serve a PHP application located inside a container

Write a Dockerfile deploying your application to /var/wwwroot/default, and start with FROM nginx-php7-fpm:novolume to use our image.

Your files should be readable by the user 'app', with uid 431.

You can find an example for a generic Laravel application at https://github.com/nasqueron/docker-pixelfed.

Serve a PHP application living in a host directory

On your machine, you've a Docker engine, a directory with your PHP application, and you want to quickly run it in a container.

$ docker run -d -it -v '/path/to/your/application:/var/wwwroot/default' -p 8080:80 nasqueron/nginx-php7-fpm

Your application is now ready to http://localhost:8080.

If you need to test a specific nginx configuration, you can also mount a volume with a complete nginx configuration folder as /etc/nginx.

Run standalone nginx and php-fpm containers

If you use docker-compose, Docker Swarm or Kubernetes, you can maintain the application as a set of containers:

  • a container app-phpfpm to host the php-fpm FastCGI process
  • a container app-nginx for nginx to serve static content and call php-fpm
  • the same image for both, to provide them a consistent set of files for your application

To make your image compatible with this method, there are a few preparations to do.

First, your nginx configuration MUST define a hostname to call app-phpfpm: fastcgi_pass app-phpfpm:9000. If you use localhost, for example with fastcgi_pass 127.0.0.1:9000;, it will try to find your php-fpm process in the same container.

Then, your application interacts with user-writable content, your application SHOULD have a separate volume for the content directories, volume shared by both containers. For example if you allow to upload images, this volume could be used by PHP to handle the actual upload and a thumbnail creation, and by nginx to serve the images.

We're interested to document this method ease of use in production and identify pitfalls. You can reach Dereckson for assistance in such a deployment.

Deprecation

runsvdir-init

The Debian package runit offered a runsvdir-init to call runit with a standard service directory, /etc/service. It also cleaned up the environment.

Several PHP applications images depend of it, calling it at the end of their initialization script. To maintain backwards compatibility of those scripts, the docker-php7-fpm image provides an equivalent script, with a deprecation notice.

You can replace your runsvdir-init line by:

/usr/bin/runsvdir -P /etc/service

If you've specified it as entrypoint, CMD ['/usr/bin/runsvdir', '-P', '/etc/service'] could be used, but that's the default entrypoint of the image and as such isn't needed.