Here is a docker-compose.yml file to orchestrate your PHP 8.4 FPM service with Nginx and PostgreSQL. This setup defines three services: web (Nginx), app (your PHP-FPM service), and db (PostgreSQL).
You will need to create a directory structure for this setup to work correctly. A common practice is to have:
./.docker/ containing your Dockerfile and custom configuration files.
./.docker/nginx/ containing your default.conf file.
./src/ for your application code (e.g., index.php).
- docker-compose.yml This file defines the services, volumes, and networks for your application stack.
version: '3.8'
services:
web:
image: nginx:alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www/html
- ./.docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- app
networks:
- app-network
app:
build:
context: .
dockerfile: ./.docker/Dockerfile
container_name: php-fpm
volumes:
- ./src:/var/www/html
networks:
- app-network
db:
image: postgres:16-alpine # Use a specific version for stability
container_name: postgres
restart: always
environment:
POSTGRES_DB: your_database_name
POSTGRES_USER: your_database_user
POSTGRES_PASSWORD: your_strong_password
volumes:
- postgres-data:/var/lib/postgresql/data
networks:
- app-network
volumes:
postgres-data:
networks:
app-network:
driver: bridge- nginx/default.conf
This file tells Nginx how to handle requests, particularly how to forward PHP requests to the PHP-FPM container. Create this file at ./.docker/nginx/default.conf.
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
# Pass PHP scripts to the PHP-FPM service
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
web service (Nginx):
Uses the official nginx:alpine image.
The ports mapping 8080:80 exposes port 80 of the container to port 8080 on your host machine. You can access your application at http://localhost:8080.
The volumes mount your local application code from ./src to /var/www/html inside the container and mounts your custom Nginx configuration.
depends_on ensures the app service starts before Nginx.
It communicates with the app service via the shared app-network.
The build directive points to your Dockerfile at ./.docker/Dockerfile to build your custom PHP 8.4 image.
The volumes mapping shares your local code with this container, allowing PHP-FPM to access your application files.
Uses a specific version of the official PostgreSQL image, which is a best practice.
16-alpine is a good choice for a lightweight image.
environment variables are used to configure the database name, user, and password. Be sure to replace the placeholder values with your own.
A volume named postgres-data is created to persist your database data, so it won't be lost when you stop or restart the container.
Make sure you have your Dockerfile at ./.docker/Dockerfile.
Make sure your default.conf file is at ./.docker/nginx/default.conf.
Place your PHP application files inside a ./src/ directory. For a simple test, create a file named index.php with
phpinfo();Open your terminal in the same directory as your docker-compose.yml file.
Run the following command:
docker-compose up -d --buildOnce the containers are up, open your web browser and navigate to http://localhost:8080. You should see the PHP info page.
This setup provides a complete, isolated development environment for your application, where each service is self-contained and communicates over a private network.