How to Create a PHP Docker Container ?
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.
What is PHP ?
Php is popular server-side scripting language that is designed primarily for web development. It can also be used for general purpose programming languages. It is embedded within the HTML and known for its simplicity and flexibility. It is used for building dynamic and interactive web pages including content management systems like WordPress due to its extensive libraries and ease of integration with databases.
What is Docker Container ?
Docker container is a lightweight, standalone executable software package that includes everything that needed to run an application such as the code, runtime, libraries and dependencies. Containers helps the operating systems in enhancing the OS-level virtualization for running multiple isolated applications on a single host ensuring the consistency across different environments. Docker containers are the preferable solution for developing, testing and deployingthe applications efficiently and reliably.
How to Install PHP using Docker ? A Step-By-Step Guide
The following steps guide you on how to install the PHP based docker container:
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.
<?php
echo ?Welcome to GeeksForGeeks?;
?>
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
How to Update PHP in Docker Container ?
The following steps guide you on how to update the PHP in docker container:
Step 1: Pull the Latest PHP Image
Firstly pull the latest version of PHP image from the dockerhub. The following command helps in pulling the latest image:
docker pull php:latest
Step 2: Stop the Running Container
Use the following command and stop the currently running PHP container:
docker stop <container_name>
Example
docker stop my-php-app
Step 3: Remove the Existing Container
Remove the old containers to avoid the conflicts
docker rm <container_name>
Step 4: Run a New Container with the latest Image
Use the following command to run a new container with the latest PHP image:
docker run -d --name <new_container_name> php:latest
Step 5: Verify the latest PHP Version
Check the PHP version of the new container to confirm the update with the following command:
docker exec -it <new_container_name> php -v
Why to Create a PHP Docker Container ?
The following are the reasons to create a PHP docker container:
-
Consistent Development Environment: Docker containers facilitates with ensuring the PHP environment across all the environments such as development, testing and production stages with eliminating the system issues.
-
Simplified Dependency Management: Docker encapsulates all the PHP dependencies, libraries and configurations in the container making easier to manage and replicate the environments.
-
Improved Deployment Efficiency: It provides the streamlined deployment process by packaging the PHP application and its environments into as single, portable containers that can run on any docker supported platform.
-
Resource Isolation and Scalability: Docker with containers ensures in providing the isolation to the PHP application from the other services. Through this provides the stable performance with allowing the easy scaling by deploying multiple container instances.