DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Concatly
Concatly

Posted on

How to Dockerize a Basic PHP Application?

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.

Steps for Dockerizing a Basic PHP Application

Files required for setup

Create a folder in your working directory.

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 dockerhub.
  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 .
  1. Run the image in a container using 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
  1. 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.

Top comments (1)

Collapse
 
galgaldas profile image
Galgaldas

I would also recommend checking out this. All you need to do is download couple of files and you can already write docker-compose up and it will work - github.com/kasteckis/symfony-docke...

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠