Step-by-Step Guide to Building and Running a PHP Project with Docker

This tutorial explains how to create a Dockerfile for a PHP‑Apache project, build the image, run the container, verify its status, and use volume mounting and exec commands to manage the application inside the container.

php Courses
php Courses
php Courses
Step-by-Step Guide to Building and Running a PHP Project with Docker

When you only have a Linux system and do not want to install Nginx, PHP, MySQL, etc., you can package your project into a Docker image to run it inside a container.

1. Create the project – prepare your PHP files in a directory (e.g., project1).

2. Write the Dockerfile

FROM php:5.6-apache
RUN docker-php-ext-install mysqli
ADD project1 /var/www/html

After saving the Dockerfile, build the image with: docker build -t malina_php_project . When the build finishes you will see a success message indicating the image was created.

3. Run the container

docker run -it -d --name malina_php_project malina_php_project

Check that the container is running: docker ps You can view container logs (e.g., docker logs 18b429b2ceac) to see the assigned IP address.

Access the application in a browser via the container’s IP, for example http://x.x.0.2/phpinfo.php, and you should see the PHP info page.

4. Verify files inside the container docker exec -it 18b429b2ceac /bin/bash This opens a shell inside the container where you can inspect /var/www/html and confirm that the project files were copied (the ADD instruction copies the contents of project1 but not the directory itself).

5. Volume mounting (optional)

docker run -it -v /host/directory:/container/directory image_name:tag
docker run -it -d -v /home/malina/project/project1:/var/www/html malina_php_project:latest

Mounting a host directory allows you to edit files locally and have changes reflected instantly inside the container.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentcontainerizationPHPApache
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.