How to serve local PHP file on docker container
In this article, we will discuss How to create a docker image for PHP. PHP is a server scripting language and a powerful tool for making dynamic Web pages. we can run PHP applications using docker.
Make a directory
Make a directory to organize files by using the below command.
$ mkdir php-docker
Create a PHP File
We will create a PHP file inside the php-docker directory.
#index.php <?php echo "Hello world!"; ?>
Dockerfile file create
We will create a Dockerfile and pull the PHP Image and Copy the index.php files to the following directory.
#Dockerfile FROM php:7.4-apache COPY . /var/www/html RUN chown -R www-data:www-data /var/www
Your directory structure should look like this:
Create The Docker Image
Now build the Docker Image, you can use the Docker Build Command.
sudo docker build -t php-example .
Now verify available images in the docker container.
sudo docker images
Run the Docker image
You can use the following command to run the Docker image.
$ docker run php-example
You can see the result our PHP application is being served at IP address 172.17.0.2.