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.

How to Dockerize PHP Applications

Updated on June 4, 2021

3 Min Read
dockerize php application

Among the many challenges in the modern software development is to ensure that the application performs consistently across all environment.

This is where Docker come to the rescue! It provides a uniform way of building and running containers for any required services. The platform makes sure that your application deployed on any Web Hosting for PHP 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 5.4

For this, make sure that you have Docker setup on the machine. I will work with docker-compose, that makes it easy to deploy multi-container application by defining them in a single file and executing a simple command.

Related: How To Host PHP On Kyup Container

In the root of the project, create a file and name it docker-compose.yml. The code in the file would look like:

Docker-compose.yml Explained

In this code, two services with the names php and db are defined these two will connect to run the final application.

‘build’ defines the location of the Dockerfile of the respected service.

‘volume’ mounts the project directory as a volume on the container at /var/www/html. A log directory is also mounted in the respective directory.

The php container exposes the port 8000 of the machine and binds to the port 80 of the container. This will allow access to the application

environment’ is used to inject environment variables.

Remember that I could force Docker to build a container before another by specifying the depends on option.  However, in this case, the db container would be built first as the php container depends on it.

Once the docker-compose file has been created, I will next create the respective dockerfiles that   contain all the instructions for building the container images.

Go to the Docker directory and create two folders for php and db containers.

The two dockerfiles would look like:  

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

php Dockerfile

The file begins with a definition of the base image (in this case, a PHP 5.6 base image). You could easily set up your desired base image depending upon the needs of your application. Next, I copied several files to the container.

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

db Dockerfile

In this dockerfile, I have used MySQL 5.6 as the base image. When a container is initiated, a new database with the specified name is created. I have also included the database SQL dump file that will be executed as soon as the server is created (because of its location in /docker-entrypoint-initdb.d).

Once all the files have been created, go to the root of the application and run the following command:

docker-compose up -d

This command will start the containers in the background. The launch could be slow the first time because all the base images have to be downloaded. However, all subsequent launches are much quicker.

Note that I have also copied the hosts file (with the following text) in the container.

Now, you can access the application by visiting http://localhost:8000. As I also copied my hosts file in the container, I can also access the application by visiting the URL,

dockerized php blog

You can view your active containers through this command:

docker ps

Here is a sample output:

You can also access the container through

docker exec -it <container_name> bash

See! How simple is to Dockerize PHP application. Due to its light weight, it can be easily destroyed and created again in just a couple of moments.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Customer Review at

“Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

Ali Azmi

Ali Azmi is Associate DevOps Engineer at Cloudways - A Managed PHP Cloud Hosting Platform. He is skilled in developing PHP applications particularly in Laravel. He is passionate about contributing to open source projects.


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!

Unleash The Cloud.
Not Budgets.

For 4 Months +
Up To 30 Free Migrations

Cyber Week

Time Left In Offer
  • 0


  • 0


  • 0


  • 0


40% OFF

On All Plans