In today’s fast-paced tech world, the ability to quickly and efficiently deploy web applications is essential. Docker Compose, a tool for defining and running multi-container Docker applications, offers a streamlined and scalable approach. This article will guide you through the process of setting up a web environment with PHP, MySQL, and Nginx using Docker Compose, ensuring a robust, repeatable, and scalable deployment.
.
Prerequisites
Before diving in, ensure you have the following installed:
- Docker: The engine running containers.
- Docker Compose: The tool for defining and running multi-container Docker applications.
Step 1: Your PHP Application
Place your PHP code in a directory named code in your project directory. Docker Compose will mount this directory into both the Nginx and PHP containers.
For this example, I have created a sample PHP script in product directory:
mkdir my-php-app && cd my-php-app
echo "<?php phpinfo(); ?>" > index.php
Step 2: Setting Up the Docker Compose File
The heart of Docker Compose is the docker-compose.yml
file. This file defines the services, networks, and volumes for your application. Start by creating a docker-compose.yml
file in your project directory:
version: '3'
services:
web:
image: nginx:latest
ports:
- "8000:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/site.conf
depends_on:
- php
- db
php:
image: php:fpm
volumes:
- ./code:/code
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mydb
MYSQL_USER: user
MYSQL_PASSWORD: userpass
volumes:
db_data:
This file defines three services: web (Nginx), php (PHP-FPM), and db (MySQL). It also specifies a volume for MySQL data persistence.
Step 3: Configuring Nginx
Create an Nginx configuration file named site.conf
in your project directory. This file will direct Nginx to handle web requests and pass PHP requests to the PHP-FPM service.
server {
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /code;
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Step 4: Running Your Application
With your docker-compose.yml
file and Nginx configuration in place, you can start your application by running:
docker-compose up -d
This command will download the required images, create the defined volumes, and start the services as configured.
Step 5: Accessing Your Application
Once the containers are up and running, you can access your PHP application by navigating to http://localhost:8000
in your web browser.
Conclusion
Docker Compose offers a convenient and efficient way to deploy complex web applications. By following the steps outlined in this guide, you have set up a PHP application with Nginx and MySQL, all running in separate containers but working seamlessly together. This approach not only simplifies the deployment process but also ensures consistency and scalability across different environments.
Remember, Docker and Docker Compose are powerful tools, and this is just the tip of the iceberg. As you become more familiar with these technologie you’ll discover even more ways to optimize your deployment workflows.