
Php Example in Docker
Running a PHP application in Docker is a common use case for containerizing web applications. I'll guide you through the process of setting up a simple PHP application in Docker.
Steps to Dockerize a PHP Application
We will create a PHP application with a basic index.php
file and serve it using Apache (which includes PHP support).
1. Prepare Your PHP Application
Let’s start by creating a simple PHP script that we’ll run inside the Docker container. This will be the index.php
file.
index.php
:
<?phpecho "Hello from Docker and PHP!";?>
This is a basic PHP script that will display a message when accessed via a browser.
2. Create a Dockerfile
Now, we’ll create a Dockerfile that defines how the container will run the PHP application.
Example Dockerfile:
# Step 1: Use an official PHP image with Apache support from Docker HubFROM php:8.0-apache# Step 2: Set the working directory inside the containerWORKDIR /var/www/html# Step 3: Copy the PHP application files into the containerCOPY index.php /var/www/html/# Step 4: Expose port 80 to allow access to the web serverEXPOSE 80
Explanation of the Dockerfile:
- FROM php:8.0-apache: This line uses the official PHP 8.0 image with Apache pre-configured. Apache is the web server, and PHP is installed along with it.
- WORKDIR /var/www/html: This sets the working directory inside the container to
/var/www/html
, which is the default directory where Apache serves files. - COPY index.php /var/www/html/: This copies your
index.php
file from your local machine to the container’s/var/www/html
directory. - EXPOSE 80: This exposes port 80 so you can access the web server running inside the container.
3. Build the Docker Image
Now that you have the Dockerfile
and index.php
ready, it’s time to build the Docker image.
In the directory where your Dockerfile
and index.php
are located, run the following command:
docker build -t php-docker-app .
This command will build a Docker image named php-docker-app
.
4. Run the Docker Container
After building the image, you can run the container and serve your PHP application with the following command:
docker run -p 8080:80 php-docker-app
-p 8080:80
tells Docker to map port 8080 on your local machine to port 80 on the container, which is where Apache is serving the PHP app.
5. Test the PHP Application
Once the container is running, open your web browser and navigate to:
http://localhost:8080
You should see the message:
Hello from Docker and PHP!
This confirms that the PHP application is being served inside the Docker container.
6. (Optional) Share the Image
If you want to share your Docker image, you can push it to Docker Hub.
Log in to Docker Hub:
docker login
Tag the image for Docker Hub:
docker tag php-docker-app yourusername/php-docker-app
Push the image to Docker Hub:
docker push yourusername/php-docker-app
This will upload the image to Docker Hub, making it available for others to pull and run.
7. Cleanup (Optional)
After you're done testing, you can stop and remove the container using:
docker stop <container_id>docker rm <container_id>
To list all running containers, use:
docker ps
To remove the image (if you no longer need it):
docker rmi php-docker-app
Conclusion
You have successfully Dockerized a basic PHP application using Apache in just a few simple steps. The Dockerfile uses an official PHP image with Apache support to serve your PHP script inside a container. You can now run this PHP app anywhere Docker is available, ensuring portability and consistency across different environments.