How to Dockerize a Basic PHP Application?

Vishesh Ahuja
2 min readJul 17, 2019

--

What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

Dockerize a Basic PHP Application — Concatly

Steps for Dockerizing a Basic PHP Application

Files required for setup

Create a folder in your working directory and add the files given below in the directory.

index.php

This is a basic PHP file which only prints a text on the screen.

<?php

echo "Hello - This is my first docker application";

?>

Dockerfile

FROM php:7.2-apache
COPY index.php /var/www/html/index.php
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
  1. The base image php:7.2-apache is used. The base image is pulled from the docker hub.
  2. The file index.php is copied to /var/www/html/ in the image.
  3. The port 80 is exposed for apache.
  4. Apache is started inside the container.

Docker Commands

  1. Build an image using docker build command. We build the image with the name phpdemo and tag as v1.
docker build -t phpdemo:v1 .

2. Run the image in a container using the docker run command. We run one container from the image phpdemo:v1. Notice the -p tag. It is used to publish the host port 9090 to the container port 80. In short, if any request will come on port 9090, it will redirect that request to our container in port 80.

docker run -d -p9090:80 phpdemo:v1

3. Open your browser and go to http://localhost:9090/index.php You should see the output from the PHP file.

Conclusion

In this article, we discussed how to dockerize a basic PHP application. You can also read more articles on PHP on Concatly.

--

--

Vishesh Ahuja

Vishesh is an Alumni of Delhi College of Engineering. Currently working as a Lead Software Engineer, he is also the Founder of Concatly.com.