Dockerize PHP Project

Blog > php

If you work in the software industry, you've probably heard of the concepts of container and Docker. Docker and containers are a breakthrough in software development and delivery, a new way of running software.

Docker is a new technology that allows development teams to build, manage and secure applications anywhere.

Today we will look at how we can run our PHP project with Docker.

Our requirements;
  • A text editor to create PHP & Dockerfile files
  • Docker installed server

Let say we have a simple Hello World project and we aim to dockerize that. The simple index file that we use for this example is;

First of all, we create a file named DockerFile in the root directory (same directory with index.php) of our PHP project and save it by typing the following lines.

The lines in this file explained as follow;

  • The first line specifies our Container image. Here we got the official php7.4 Apache image from Docker Hub.
  • In the second line, we copied our index file to Apache's server root directory.
  • Later, we configured the server to broadcast over port 80.
  • Finally we set Apache to run in the background.
Let's get our project published. 
Now Docker enters the scene

At this stage, we throw our project on our server and run the following command first. 

We compiled our image named phpDockerDemo with v1 tag in the directory we are in. The tag is optional.

Then write the following command to run

With this command, our phpDockerDemo v1 tag image (-d) will be broadcasting in the background (-p) to 80 external port and 80 internal port.

By opening your browser, you can now access our demo project with your server url.

Conclusion

This is the simplest workflow for containerization of a PHP project. You can build up more complex projects with containerization

Tags