Easily Run PHP 7.4 with Docker Compose, Apache and MySQL

Posted April 16, 2024
Easily Run PHP 7.4 with Docker Compose, Apache and MySQL

PHP comes in different versions. You choose what version you need. In this guide, you will use PHP 7.4. You will use Docker, Apache, and MySQL to spin up a PHP 7.4 Docker Container with Docker Compose.

If you are running PHP 7.4 locally, you will need a webserver. This way, you will be able to access your server and run what you need. The same works with Docker. However, Docker is your PHP 7.4 environment. You will need to create a server. In this case, the Apache server will work perfectly to run PHP 7.4 within Docker.

On the other hand, you need a database, and MySQL is a good choice, along with PHP 7.4 and Apache, Docker will use Docker Compose and spin a MySQL container to server your PHP data store. Now let’s dive in and Effortlessly Run PHP 7.4 with Docker Compose, Apache, and MySQL.

Prerequisites

To run PHP 7.4 with Docker, Compose, Apache, and MySQL, ensure:

Creating PHP 7.4 Dockerfile

To run a container, PHP 7.4 will need a Dockerfile. A Dockerfile takes your instructions and packages your app in a Docker container.

A Dockerfile will let you add Apache to run PHP 7.4. At the same time, it is the Dockerfile that will let you specifically pick a PHP 7.4 image from Docker Hub.

To get started, first create the following directory tree:

php7.4-with-docker/
β”‚ β”œβ”€β”€ Dockerfile # create a Dockerfile
β”‚ └── docker-compose.yml # Make sure you have docker-compose.yml file
└── src/ # This is your PHP code directory
    └── index.php

Now, Once you have the Dockerfile ready, dive in and add PHP 7.4 Dockerfile instructions as follows:

# Add 7.4 as your base PHP version
FROM php:7.4-apache
# Add Apache modules Docker
RUN a2enmod rewrite
# Install the MySQL PHP 7.4 library
RUN docker-php-ext-install mysqli pdo pdo_mysql
# Set working directory
WORKDIR /var/www/html
# Copy PHP code to Docker
COPY ../src .

You will Note that you have specifically added PHP 7.4 as your base Docker image. Docker will use it to create the container.

Using Docker Compose with PHP 7.4, Apache, and MySQL

To create this container, you will need some test scripts. For now, you just the Docker file to run a PHP 7.4 Docker image. Now, you need a docker-compose.yml file to spin up your container

Docker Compose will create three services here:

  • PHP to build PHP 7.4 from the Dockerfile. It will mount the current directory to /var/www/html inside the container.
  • Apache exposes port 8000 on the host machine and runs PHP.
  • MySQL service to set up environment variables for MySQL configuration that connect to PHP.

Creating PHP 7.4, Apache, and MySQL with docker-compose.yml

Once you understand the service you need, go to your docker-compose.yml file and set up these services as follows:

First, you will Apache and expose your app on 8000. This way, you will let Apache copy the src directory and mount it into docker on /var/www/html and expose the application:

version: "3.9"
services:
  php_app:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./src:/var/www/html
    networks:
      - app-network
    depends_on:
      - mysql_database
    ports:
      - 8000:80

Note that, A PHP will depend on mysql_database. Now you will go ahead and add a new service to create a MySQL database. Your database will have the environment variables for connecting to as follows:

  mysql_database:
    image: mysql:8.3
    environment:
      MYSQL_ROOT_PASSWORD: mysql_root_password
      MYSQL_DATABASE: test_database
      MYSQL_USER: php_user
      MYSQL_PASSWORD: php_user_password
    networks:
      - app-network
    ports:
      - "3306:3306"
networks:
  app-network:
    driver: bridge

Note here: You have environment variables such as:

  • MYSQL_ROOT_PASSWORD: mysql_root_password
  • MYSQL_DATABASE: test_database
  • MYSQL_USER: php_user
  • MYSQL_PASSWORD: php_user_password

In this case, these elements will be used for PHP to connect to a MySQL database.

Creating a PHP 7.4 MySQL Docker App

Because this Docker app is using PHP and MySQL, You will use a simple application. This is where you will add the PHP 7.4 code example to Docker.

In this case, you create an src and add an index.php. You will create a MySQL connection script using PHP 7.4 as follows:

<?php

$servername = "mysql_database";
$username = "php_user_password";
$password = "php_user_password";
$database = "test_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully";

// Close connection
$conn->close();

?>

The main key points here are the connection details. These details are based on the MySQL service environment variables: In such a case:

  • The servername must be the MySQL service name, which in this case is mysql_database.
  • The connection strings username, database, and password are also taken based on MySQL variables.
  • Your Dockerfile has RUN docker-php-ext-install mysqli pdo pdo_mysql. It will install MySQL libraries PHP 7.4 to connect to the MySQL database.

Creating a PHP 7.4 Container

Once you have the above ready, you now use Docker commands to let Docker spin up a PHP 7.4 Container.

In your root directory, use the following command to run the Container:

docker-compose up --build -d

Easily Run PHP 7.4 with Docker Compose, Apache and MySQL

Once the containers are running, confirm so in your Docker Engine:

Easily Run PHP 7.4 with Docker Compose, Apache and MySQL

To test the PHP 7.4 Container is working, open http://localhost:8000/. At this point, you should have a successful connection to MySQL:

Easily Run PHP 7.4 with Docker Compose, Apache and MySQL

This confirms you have successfully used Docker to run PHP 7.4 with Apache and MySQL.

The next step is you go ahead and add phpMyAdmin to access your MySQL database and manage it. This Apache PHP with Docker Compose, MySQL, and PhpMyAdmin guide has all you need

Conclusion

I hope you can now Easily Run PHP 7.4 with Docker Compose, Apache, and MySQL. This Guide was created to let you use a specific version that you need on Docker.

Easily Run PHP 7.4 with Docker Compose, Apache and MySQL

Written By:

Joseph Chege