Skip to content

Instantly share code, notes, and snippets.

@jpalala
Last active September 9, 2025 09:45
Show Gist options
  • Save jpalala/67f3dcc4b5ee4b8d0d357462293dfd9e to your computer and use it in GitHub Desktop.
Save jpalala/67f3dcc4b5ee4b8d0d357462293dfd9e to your computer and use it in GitHub Desktop.
docker files for laravel

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).

  1. 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
  1. 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;
    }
}

How It Works

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.

app service (PHP-FPM):

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.

db service (PostgreSQL):

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.

To Run the Stack

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 --build

Once 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.

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;
}
}
# Use the official PHP 8.4 FPM Alpine image as the base
FROM php:8.4-fpm-alpine
# Update and upgrade packages, then install basic dependencies
# The --no-cache option keeps the image size smaller
RUN apk update && apk upgrade --no-cache \
&& apk add --no-cache bash git
# Install PHP extensions
# This part assumes you have a script named install-php.sh in your .docker directory
# that handles installing PHP extensions.
# If your install-php.sh script is designed for PHP 7.4, you might need to update it
# to be compatible with PHP 8.4, especially regarding extension names or dependencies.
# For example, some extension installation commands might differ.
COPY ./.docker/install-php.sh /usr/sbin/install-php.sh
RUN chmod +x /usr/sbin/install-php.sh
# Run the extension installation script
# Ensure your install-php.sh script is updated for PHP 8.4 compatibility.
# Common extensions like gd, intl, pdo_mysql, zip might require specific build dependencies.
# Example: if your script installs gd, ensure libpng-dev, libjpeg-turbo-dev, freetype-dev are installed.
RUN /usr/sbin/install-php.sh
# Copy PHP configuration files
COPY ./.docker/*.ini /usr/local/etc/php/conf.d/
# Copy application code
# It's often better to copy only necessary files for production builds.
# For development, you might copy everything.
COPY . .
# Change current user to www-data
# This is a good security practice for running web applications.
USER www-data
# Expose port 9000 for PHP-FPM
EXPOSE 9000
# Start the PHP-FPM server
CMD ["php-fpm"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment