Setting up NGINX inside Docker
Having become familiar with Docker Compose, we’re set to advance our container usage. In this section, we’ll improve our NGINX container by adding personalized configurations and website content.
Let’s begin by revisiting the docker-compose.yml
file we created earlier. This file already specifies the NGINX service and maps the container’s port 80 to port 80 on the host machine, making the web server accessible from the host.
Next, we want our NGINX container to serve our website using the actual configuration files and content from our host machine. To achieve this, we’ll use Docker volumes. Volumes are the preferred mechanism for persisting data generated and used by Docker containers. Here’s how you can modify the docker-compose.yml
file to mount a volume:
version: '3' services: nginx: image: nginx:latest ports: - «80:80» volumes: - ./config/nginx.conf:/etc/nginx/nginx.conf - ./html:/usr/share/nginx/html
In this setup, ./config/nginx.conf
is the path to your custom NGINX configuration file on your host machine. /etc/nginx/nginx.conf
is the path where the NGINX container expects to find the configuration file. The second volume, ./html
, is the directory on your host machine that contains your website’s content, mounted into /usr/share/nginx/html/
in the Docker container.
This approach ensures that you can edit your NGINX configuration or website content directly on your host machine, and those changes will be reflected inside the container.
A note
NGINX is preconfigured; therefore, you can skip mounting the nginx.conf
volume and use the default settings. Do mount the html
directory as it is where NGINX expects to find web content to serve.
Integrating PHP with NGINX using Docker Compose
As we progress, our next step is to elevate our NGINX server to handle dynamic content with PHP. This combination is common in the web development world, and Docker Compose makes it easy to implement.
We’ll start by extending our existing docker-compose.yml
file to include a PHP service, and then we will ensure NGINX can communicate with the PHP service using FastCGI:
- First, we need to add a PHP service that runs FastCGI Process Manager (
php-fpm
). Here’s how the updateddocker-compose.yml
file might look:version: '3' services: nginx: image: nginx:latest ports: - "80:80" volumes: - ./config/nginx.conf:/etc/nginx/nginx.conf - ./html:/usr/share/nginx/html depends_on: - php php: image: php:8.3-fpm volumes: - ./html:/usr/share/nginx/html
In this setup, the
php
service uses the official PHP image withphp-fpm
. We also mount thehtml
directory into the PHP container to ensure it has access to the same web files as NGINX. - Before starting the Docker containers, we need to make sure NGINX and PHP communicate. Let’s edit the
nginx.conf
file to include FastCGI parameters. Here’s an example ofnginx.conf
with included FastCGI parameters:events {} http { server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } location ~ \.php$ { try_files $uri =404; fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } }
This configuration instructs NGINX to forward requests for PHP files to the PHP-FPM service running in the
php
container.
Note
NGINX resolves the php
service name using Docker’s internal DNS to communicate with the php-fpm
service. If you rename the service in your docker-compose.yml
file, remember to update the nginx.conf
file accordingly to match the new service name.
- With this
docker-compose.yml
andnginx.conf
configuration, you can rundocker compose up
, and NGINX will serve both your static content and your dynamic PHP pages:root@nginx:~/nginx# docker compose up [+] Running 2/0 ✔ Container docker-php-1 Created ✔ Container docker-nginx-1 Created Attaching to nginx-1, php-1 php-1 [02-Mar-2024 11:42:30] NOTICE: fpm is running, pid 1
- After launching your containers, to check that NGINX and PHP are interacting correctly, create a PHP file containing
<?php phpinfo(); ?>
. This will display a PHP information page when accessed, confirming successful communication between the containers:
Figure 8.4: phpinfo running within Docker
This subsection has guided you through adding a PHP service to your Docker environment and configuring NGINX to process PHP scripts via FastCGI. This configuration mimics a production environment, providing a solid foundation for building and deploying PHP applications with Docker.
In the next section, we’ll explore how to configure a Docker-contained NGINX to proxy applications running directly on the host server.