Docker compose for PHP 8 and Nginx
useful docker compose file for php 8 and Nginx

Full-stack developer with mastery across mobile (React Native, Ionic), front-end (React, Svelte, Vue.js), back-end (Express.js, FastAPI, Django), e-commerce (Shopify, OpenCart), and CMS (Wordpress, Laravel, Drupal, headless). Building web and mobile applications that deliver!
Here's a Docker Compose configuration for PHP 8, using Nginx as the web server:
YAML
version: '3.8'
services:
nginx:
image: nginx:1.25-alpine
ports:
- "80:80"
volumes:
- ./app:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
php:
image: php:8.1-fpm-alpine
volumes:
- ./app:/var/www/html
working_dir: /var/www/html
Explanation:
version: Specifies the Docker Compose file format version.
services: Defines the services to run in the environment.
nginx:
image: Uses the Nginx image for web serving.
ports: Maps port 80 on the host machine to port 80 in the container.
volumes: Mounts two volumes:
The
appdirectory (containing your PHP code) to the Nginx document root.A custom Nginx configuration file for PHP support.
depends_on: Ensures the
phpservice starts before Nginx.
php:
image: Uses the PHP 8.1 FPM image for processing PHP code.
volumes: Mounts the
appdirectory to the PHP container's working directory.working_dir: Sets the working directory within the container.
To use this configuration:
Create a directory for your project: Bash
mkdir my-php-app cd my-php-appCreate a
docker-compose.ymlfile and paste the configuration above into it.Create an
appdirectory to hold your PHP code.Optionally, create a
nginx/default.conffile for custom Nginx configuration.Run the containers: Bash
docker-compose up -d
Now you can access your PHP application at http://localhost!
Additional notes:
Custom Nginx configuration: If you need to adjust Nginx settings for your application, create a
nginx/default.conffile and mount it as shown in the configuration.Other services: You can add other services to your environment, such as databases or caches, by defining them in the
servicessection of the Docker Compose file.Versioning: Be sure to specify the specific version of Nginx and PHP that you need in the
imagedefinitions.




