Current Location: Home> Latest Articles> Docker Compose Guide: Efficient PHP Framework Deployment Tutorial

Docker Compose Guide: Efficient PHP Framework Deployment Tutorial

M66 2025-09-11

Introduction to Docker Compose

Docker Compose is a tool for managing multi-container applications, significantly simplifying PHP framework deployment. By configuring version, services, image building, port mapping, dependencies, and volumes, developers can efficiently manage multiple containers and quickly deploy applications.

Practical Example: Deploying a Laravel Application

Below is an example Docker Compose configuration for a Laravel application:

version: '3'

services:
    app:
        build: .
        volumes:
            - .:/var/www/html
        ports:
            - "80:80"
        depends_on:
            - db
    db:
        image: mysql:5.7
        volumes:
            - db-data:/var/lib/mysql
        environment:
            - MYSQL_ROOT_PASSWORD=password

volumes:
    db-data:

Configuration Analysis

Version Declaration

The version field specifies the Docker Compose file version, with version 3 used in this example.

Service Definition

The services block defines container services, including app (for deploying the Laravel application) and db (for deploying the MySQL database).

Image Building and Directory Mounting

The app container is built using the Dockerfile specified by build, and volumes mount the host directory into the container, enabling real-time code synchronization.

Port Mapping

ports maps the container's port 80 to the host port 80, allowing the Laravel application to be accessed via a browser.

Dependencies

depends_on indicates that the app container depends on the db container, ensuring the database container starts first to prevent application startup failures.

Volume Configuration

volumes define persistent storage, with db-data used to store MySQL data, mounted at /var/lib/mysql inside the container.

Environment Variables

The environment block sets environment variables for the MySQL container, where MYSQL_ROOT_PASSWORD defines the root user's password to ensure database security.

Conclusion

Mastering Docker Compose configuration makes PHP framework deployment more efficient and convenient. This example demonstrates the complete process from service definition to volume management, providing a clear reference for developers.