Creating a Docker-Compose Stack for PHP and MySQL: A Comprehensive Guide

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to package your application’s components, such as PHP and MySQL database, into a single configuration file. This simplifies the deployment and management of your application, ensuring that all components are running and communicating correctly.

In this article, we’ll walk you through the process of creating a Docker-Compose stack for a PHP and MySQL application for database.

Prerequisites

Before getting started, make sure you have Docker and Docker Compose installed on your system. You can download and install Docker from the official Docker website (https://www.docker.com/) and Docker Compose from the Docker Compose documentation (https://docs.docker.com/compose/install/). Once you have both installed, you’re ready to proceed.

Creating the Docker-Compose File

The first step is to create a Docker-Compose file, which will define the structure and dependencies of your application. Here’s an example Docker-Compose file for a PHP and MySQL application for database:

version: '3'
services:
  php:
    image: php:apache
    ports:
      - 80:80
    volumes:
      - ./src:/var/www/html/
  mysql:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypassword
    volumes:
      - mysql_data:/var/lib/mysql
volumes:
  mysql_data:
YAML

Explanation of the Docker-Compose File:

Building and Running the Application

Once you’ve saved the Docker-Compose file, you can build and run the application using the following command:

docker-compose up -d


Creating a Docker-Compose Stack for PHP and MySQL: A Comprehensive Guide

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to package your application’s components, such as the PHP and MySQL application for database, into a single configuration file. This simplifies the deployment and management of your application, ensuring that all components are running and communicating correctly.

In this article, we’ll walk you through the process of creating a Docker-Compose stack for a PHP and MySQL application for database.

Prerequisites

Before you begin, ensure you have the following installed:

sudo apt-get install docker-compose

Creating the Docker-Compose File

The first step is to create a Docker-Compose file, which will define the structure and dependencies of your application. Here’s an example Docker-Compose file for a PHP application with a MySQL database:

YAML

version: "3.9"

services:
  php-app:
    build: .
    ports:
      - "80:80"
    volumes:
      - ./app:/var/www/html
    depends_on:
      - mysql

  mysql:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypassword
    volumes:
      - mysql_data:/var/lib/mysql
    ports:
      - "3306:3306"

volumes:
    mysql_data:

Use code with caution. Learn morecontent_copy

Explanation of the Docker-Compose File:

version: “3.9”

This line specifies the version of the Docker-Compose file. This is important for ensuring compatibility with different versions of Docker-Compose.

services:

This section defines the services in your application. A service is a self-contained unit of an application that can be run, stopped, and managed independently.

php-app:

This service represents your PHP application. The following properties are defined for this service:

  • build: This option instructs Docker Compose to build the Docker image for the PHP application from the . directory, which contains the application’s source code. This will create a custom image with the PHP application installed and configured.
  • ports: This option maps port 80 on the host machine to port 80 inside the container. This will allow you to access your application from a web browser.
  • volumes: This option mounts the ./app directory on the host machine to the /var/www/html directory inside the container. This means that your application’s code and data will be shared between the host and the container. This is useful for development, as it allows you to make changes to your code and see them reflected in the container without having to rebuild the image.
  • depends_on: This option specifies that the php-app service depends on the mysql service. This means that the php-app service will not start until the mysql service is up and running. This is because the PHP application needs to connect to the MySQL database to store data.

mysql:

This service represents your MySQL database. The following properties are defined for this service:

  • image: This option specifies the Docker image to use for the MySQL database. We’re using the official mysql:latest image, which will pull the latest version of MySQL from Docker Hub.
  • restart: This option tells Docker to restart the mysql service automatically if it stops. This is important for ensuring that your database is always available.
  • environment: This option sets environment variables for the mysql service. These variables are used to configure the MySQL database. The following environment variables are defined:
  • MYSQL_ROOT_PASSWORD: This variable sets the password for the MySQL root user. It is important to keep this password secret to protect your database from unauthorized access.
  • MYSQL_DATABASE: This variable sets the name of the database that will be created for your application. You can change this to a different name if you want.
  • MYSQL_USER: This variable sets the name of the user that will be used to connect to the database from your PHP application. You can change this to a different name if you want.
  • MYSQL_PASSWORD: This variable sets the password for the database user. It is important to keep this password secret to protect your database from unauthorized access.
  • volumes: This option mounts the mysql_data volume to the /var/lib/mysql directory inside the container. This volume will be used to store the data for your MySQL database. This is important for persisting your data between container restarts.
  • ports: This option maps port 3306 on the host machine to port 3306 inside the container. This is the default port for MySQL, and it allows other applications to connect to your database.

volumes:

This section defines the volumes for your application. A volume is a directory that is shared between the host machine and the container. This can be useful for storing data that you want to persist between container restarts.

mysql_data:

This volume is used to store the data for your MySQL database. This is important for persisting your data between container restarts.

Building and Running the Application

Once you’ve saved the Docker-Compose file, you can build and run the application using the following command:

docker-compose up -d

This command will build the Docker images for both the PHP application and the MySQL database, and then start the two containers in detached mode. You can then access your PHP application by opening a web browser and navigating to localhost.

Testing the Application

To test your application, create a simple PHP file, such as index.php, in the app directory. Save the following code to the index.php file:

<?php
$host = 'mysql';
$user = 'myuser';
$pass = 'mypassword';
$db = 'mydatabase';
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully to MySQL database";
?>
PHP

Reload your web browser and you should see the message “Connected successfully to MySQL database”. This confirms that your PHP application is running correctly.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top