How to build and run an Apache container with PHP in a docker
ENVIRONMENT
- Docker: 18.03.1-ce
- Host: Windows 10 Pro
INSTRUCTIONS
- Create a Dockerfile in your project with the following content:
FROM php:7.0-apache COPY src/ /var/www/html/ EXPOSE 80
- Create an src folder to store your website files and folders
- Create an index.php file inside the src folder as below :
<?php echo "Hello World !";
The folder structure at this point would look like this :
- Build Docker Image using command:
docker build -t phpapache .
docker build command downloads the referenced images and creates a new build as per the instructions in the Dockerfile in your local repository.
- Run docker container using command :
docker run -d --name phpapache-runtime-3 -p 8080:80 -v "$(pwd)/src:/var/www/html/" phpapache
Here we are doing the following :
running the container in the detached mode
naming the container as phpapache-runtime,
mapping the host port 8080 to the container port 80
mounting the src folder in the present working directory in host machine to the /var/www/html/ inside the container. This will allow a real-time change of the website hosted by Apache.
You can list all running containers using command: docker ps
TESTING :
- Access the website using url : http://localhost:8080/index.php
Next, modify the index.php file in the src directory as below and refresh the page and test again :
[text]
<?php
echo "Hello World Changed!";
[/text]
Note:
To mount the host machine folder as a volume in the container, you will first need to enable share drive.
You can do this in docker settings window:
REFERENCES :
- Official PHP docker hub repo : https://hub.docker.com/_/php/