In previous article we have discuss how to run nginx and php-fpm in single container using supervisord. With base php image, we need to install some packages such as nginx and supervisord. In this article we will discuss how to running with separate official image.

previous article:

Prerequisite

  1. Docker Engine installed on OS, linux / mac / windows

Create one example PHP file

i will create index.php file

<?php phpinfo();

Create Dockerfile

dockerfile for php-fpm

in this file we will copy project file called as ‘index.php’ into the image and make simple script to move from temporary directory into volume.

Dockerfile-php

FROM php:8.2-fpm-buster

RUN mkdir -p /home/
COPY index.php /home/index.php
COPY init.sh /root/init.sh

RUN chmod +x /root/init.sh

CMD ["/bin/bash", "-c", "/root/init.sh;php-fpm" ]

dockerfile for nginx

in the nginx’s dockerfile just copy the host configuration.

Dockerfile-nginx

FROM nginx:stable-bullseye

COPY default /etc/nginx/conf.d/default.conf

Create Necessary Files

init.sh

#!/bin/bash

mv /home/* /var/www/html/

default

server {
         listen       80;
         server_name  _;
         root         /var/www/html/;

         access_log /var/log/nginx/access.log;
         error_log  /var/log/nginx/error.log error;
         index index.html index.htm index.php;

         location / {
                      try_files $uri $uri/ /index.php$is_args$args;
         }

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

so we have like this

❯ tree
.
├── Dockerfile-nginx
├── Dockerfile-php
├── default
├── index.php
└── init.sh

0 directories, 5 files

Building the docker images

docker build -t ghcr.io/man20820/nginx:1 . -f Dockerfile-nginx
docker build -t ghcr.io/man20820/phpfpm:1 . -f Dockerfile-php

Create Volume and Network

volume used to store project’s files and the network used to communicate between nginx and php-fpm

docker volume create php
docker network create php

Run docker container

docker run -d --name php --network php -v php:/var/www/html ghcr.io/man20820/phpfpm:1
docker run -d --name nginx --network php -v php:/var/www/html ghcr.io/man20820/nginx:1

So we have 2 live containers, guys

Access the website

for testing, we can access the website

Congratulation!