This is a very simple dockerised PHP-FPM and Nginx application setup.


Structure


$ tree
.
├── docker
│ ├── .env
│ ├── docker-compose.yml
│ ├── nginx
│ │ ├── app.conf
│ │ └── Dockerfile
│ └── php
│ ├── Dockerfile
│ └── www.conf
├── index.html
└── index.php

Files


index.html


Success: HTML page (Frontend)

index.php


echo 'Success: PHP page (Frontend)'.PHP_EOL;

docker/.env


COMPOSE_PROJECT_NAME=frontend
IMAGE_TAG=latest

docker/docker-compose.yml


version: "3"

services:
php:
build:
context: ./php
image: "${COMPOSE_PROJECT_NAME}/php:${IMAGE_TAG}"
hostname: "${COMPOSE_PROJECT_NAME}-php"
volumes:
- ..:/app:cached
- ./php/www.conf:/usr/local/etc/php-fpm.d/www.conf:cached
working_dir: /app
nginx:
build:
context: ./nginx
image: "${COMPOSE_PROJECT_NAME}/nginx:${IMAGE_TAG}"
hostname: "${COMPOSE_PROJECT_NAME}-nginx"
ports:
- "8081:80"
volumes:
- ..:/app:cached
- ./nginx/app.conf:/etc/nginx/conf.d/default.conf:cached
depends_on:
- php

docker/php/www.conf


[www]

user = www-data
group = www-data

listen = nginx:9000

pm = dynamic
pm.max_children = 40
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500

docker/php/Dockerfile


FROM php:7.2-fpm-alpine

ENV PS1="\u@\h:\w\\$ "

RUN apk add --no-cache bash

CMD ["php-fpm"]

docker/nginx/app.conf


server {
listen 80 default_server;

server_name frontend.com www.frontend.com;

root /app;

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}

error_log /var/log/nginx/app_error.log;
access_log /var/log/nginx/app_access.log;
}

docker/nginx/Dockerfile


FROM nginx:1.15-alpine

ENV PS1="\u@\h:\w\\$ "

RUN apk add --no-cache bash

Build


docker$ docker-compose up -d
Creating network "frontend_default" with the default driver
Building php
Successfully tagged frontend/php:latest
Building nginx
Successfully tagged frontend/nginx:latest
Creating frontend_php_1 ... done
Creating frontend_nginx_1 ... done

Verify


$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
frontend/nginx latest 1a6733564021 32 minutes ago 21.6MB
frontend/php latest 8fb15087688c About an hour ago 79.5MB
nginx 1.15-alpine 63356c558c79 10 days ago 17.8MB
php 7.2-fpm-alpine 9966b8ea0bdc 3 weeks ago 78.1MB

$ docker network ls
NETWORK ID NAME DRIVER SCOPE
8cbe907420d5 frontend_default bridge local

$ docker ps
CONTAINER ID IMAGE COMMAND PORTS NAMES
d24d5ef9e31a frontend/nginx:latest "nginx -g 'daemon of…" 0.0.0.0:8081->80/tcp frontend_nginx_1
b9bf557b7ac7 frontend/php:latest "docker-php-entrypoi…" 9000/tcp frontend_php_1

docker$ docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------
frontend_nginx_1 nginx -g daemon off; Up 0.0.0.0:8081->80/tcp
frontend_php_1 docker-php-entrypoint php-fpm Up 9000/tcp

Test


$ curl http://localhost:8081
Success: HTML page (Frontend)

$ curl http://localhost:8081/index.php
Success: PHP page (Frontend)