This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

🔊 Web Growth Summit is here! Learn from industry experts on July 17-18, 2024. REGISTER NOW→

How to Dockerize PHP Applications

Updated on June 21, 2023

6 Min Read
docker php

One of the biggest challenges in modern software development is ensuring an application performs consistently across all environments. And that’s exactly where Docker comes to the rescue!

Docker offers a standardized way of building and running containers for any required services. The platform ensures your application deployed on any PHP web hosting performs the same regardless of the target environment.

To illustrate how to Dockerize PHP Application, I will dockerize a blogging website originally developed in Laravel 5.4.

You might also like: Getting Started With Laravel 8

Ensure you have Docker set up on the machine before getting started. I will work with docker-compose, which makes it easy to deploy a multi-container application by defining them in a single file and executing a simple command.

But before delving into the technicalities, let’s get our basics right first and have a quick overview of Docker and PHP containers.

What Is Docker?

Docker is an open-source platform that enables developers to automate an application’s deployment and management within software containers. Docker uses a client-server architecture, where the client communicates with the daemon responsible for building, running, and distributing containers.

Also, Docker provides a way to package an application and its dependencies into a container image to be easily deployed on any system running Docker. This containerization approach allows applications to run consistently across different environments.

By using Docker, developers can streamline the development and deployment processes, increase productivity, and create a consistent and reproducible environment for their applications. It has gained significant popularity and is widely used in the software development and DevOps communities.

What Is a PHP Container?

A PHP container in Docker refers to an isolated and lightweight runtime environment created using Docker technology, specifically for running PHP applications.

In Docker, a container is a standalone unit that encapsulates an application and all its dependencies, including the operating system, libraries, and runtime environment. And a PHP container is configured to provide the necessary components for running the PHP code.

Using Docker, you can package your PHP application and its dependencies into a container, ensuring consistency and reproducibility across different environments. Doing so lets you run PHP applications on any system with Docker installed, regardless of the underlying host operating system.

Docker containers also offer benefits such as scalability, portability, and ease of deployment, making them an efficient choice for running PHP applications in various deployment scenarios.

Requirements to Install Docker

Here are the prerequisites listed to install Docker:

  • PHP version 8.x or above.
  • MariaDB or MySQL Database
  • Docker requires a 64-bit version of Windows
  • Docker requires a 64-bit processor with support for hardware virtualization.
  • 2 GB RAM is recommended
  • Docker recommends at least 20 GB of free space.
  • Docker requires a compatible version of the Linux kernel.

How to Setup and Run a Local PHP Apache Server Instance

Here’s how you can set up and run a local PHP Apache server instance using Docker:

  1. Specify the following environments in your docker-compose.yml file:
    version: '3.8'
    container_name: php-apache
    image: php:8.0-apache
    - ./php/src:/var/www/html/
    - 8000:80
  • container_name: Choose a random name for your PHP container, e.g., php-apache.
  • image: Specify the official PHP image and the version of PHP Apache you want to use, e.g., php:8.0-apache.
  • volumes: Set up the source directory for your code/files. Place your PHP script files in the ./php/src directory.
  • ports: Define the port mapping for the Apache server. In this example, we expose port 80 of the container, which can be accessed through port 8000 on your local computer.

Let’s test it out. Go ahead and run docker-compose to pull all the information. Download the Apache server, build the image, and run the container.


Working With PHP Dockerfile

  1. Create a Dockerfile with the following content:
COPY php.ini /usr/local/etc/php/
RUN apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng12-dev libmcrypt-dev mysql-client \
&& docker-php-ext-install pdo mysql mysql1 gd iconv \ && docker-php-ext-install mbstring \ 
&& docker-php-ext-install mcrypt


RUN a2enmod rewrite
#RUN a2enmod mcrypt
RUN service apache2 restart
WORKDIR /etc/apache2/sites-available/
RUN a2ensite


You might also like: How To Upgrade From PHP 5.X To PHP 7

  • Specify the base image and dependencies required for your PHP application.
  • Copy the necessary configuration files (php.ini,, hosts) to the appropriate locations inside the container.
  • Enable the rewrite module, restart the Apache service, and configure the Apache site.
  • Expose port 80 for accessing the PHP application.
  1. Build the Docker image using the “docker build -t php-app . ” command in the directory containing the “Dockerfile.”

Setup a MySQL Database Container

Add the following configuration to your docker-compose.yml file:

    container_name: db
    image: mysql
    restart: always
        - "9906:3306"
  • Choose a container name for the MySQL database container, e.g., “db”.
  • Specify the MySQL image.
  • Set the necessary environment variables for authentication and database access.
  • Define the port mapping to access the MySQL service.

Password authentication: To use and access a MySQL server, you need to set authentication environments that will allow you to access the defined MySQL server and its services, such as a database. We have used MYSQL_USER: MYSQL_USER and MYSQL_PASSWORD in the code above.

Add MySQL Support Tools to the PHP Container

  • In your project directory, navigate to the “/php” folder and create a “Dockerfile” file.
  • Add the following content to the “Dockerfile”:
FROM php:8.0-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN apt-get update && apt-get upgrade -y

  • Specify the base image and install the “mysqli” extension.
  • Update and upgrade the system packages.

Accessing the Application:

  • Visit http://localhost:8000 in your browser to access the PHP application.
  • If you copied the host’s file to the container, you can access the application using the URL
  • You can view your active containers using the following command:

docker ps

Here is a sample output:

  • You can also access the container using the command below:
    docker exec -it <container_name> bash

See how easily you can Dockerize a PHP application. Due to its lightweight, it can be easily destroyed and created again in minutes.

Final Words

So now you know how to dockerize a PHP application. With its magnificent powers of containerization, Docker offers a portal to a world of PHP application enchantment.

Docker whisks away the deployment headaches and transforms them into a breeze to help you dedicate your precious time and energy to what truly matters – crafting extraordinary PHP applications that leave a lasting impression.

Don’t wait a moment longer – venture into the captivating realm of Docker, where streamlined workflows, simplified deployments, and PHP perfection await. Your applications are ready for the spotlight, and Docker is the stage that will elevate them to greatness. Step into this world of wonder, and let your PHP creations shine brighter than ever before.

Q) Can PHP run on Docker?

A) Yes. PHP can run on Docker seamlessly, allowing developers to utilize its platform-independent and isolated environment for running PHP applications efficiently.

Q) How to check PHP running on a Docker container?

A) Here’s how you can check PHP is running on a Docker container:

  • Retrieve the container ID or name using the command:
docker ps
  • Access the shell of the running container with the following command:
docker exec -it <container_id_or_name> sh
  • Once inside the container’s shell, execute the command php -v to check the PHP version. If the version is displayed, it confirms that PHP is running successfully.
  • This command will display the PHP version installed in the container, confirming that PHP is running successfully.

Alternatively, suppose your PHP application has a web interface. In that case, you can access it by opening a web browser and navigating to the IP address or hostname associated with the Docker container on the specified port (e.g., http://localhost:80). If the PHP application loads successfully, it indicates that PHP is running within the Docker container.

Q) What is the size of Docker PHP?

A) The initial size of a Docker PHP image can vary depending on the included extensions and dependencies. However, with optimization techniques like multi-stage builds, minimizing unnecessary dependencies, and compressing image layers, the size of a Docker PHP image can be significantly reduced. It’s possible to achieve a smaller footprint and optimize the image size based on your specific requirements.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Abdur Rahman

Abdur Rahman is the Magento whizz at Cloudways. He is growth ambitious, and aims to learn & share information about Ecommerce & Magento Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]


Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!


Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour


  • 0


  • 0


  • 0


  • 0



For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now