PHP is one of the widely used programming languages across several organizations mostly used for creating web architectures and back-end applications. Most of the big tech giants still rely on PHP for their back-end applications and are also increasingly adopting developer tools such as Docker. Thus, it becomes very important to learn how to access and use PHP inside Docker Containers. Docker provides regularly updated PHP Images that can be pulled straight from the Dockerhub and can be customized using Dockerfiles. The Docker PHP Image allows you to build and run PHP applications and to access such applications on your localhost, you can use the -p flag to connect and publish ports.
In this article, we will discuss how to create a PHP Docker Container with the help of the Apache server.
Step 1: Creating a PHP File
Create a simple PHP file that will be served once we run the container. Let’s name the file app.php.
Step 2: Creating the Dockerfile
We will pull the PHP Image and Copy the files to the following directory.
FROM php:7.0-apache COPY . /var/www/php
Your directory structure should look like this.
Step 3: Creating the Docker Image
To build the Docker Image, you can use the Docker Build Command.
sudo docker build -t php-demo .
To verify that the image has been built, you can list all the Images.
sudo docker images
Step 4: Running the Container
You can use the following command to run the Docker Container.
sudo docker run -p 80:80 php-demo
You can see that our PHP application is being served at IP address 172.17.0.4.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.