Backend Development 3 min read

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

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

After saving the Dockerfile, build the image with:

<code>docker build -t malina_php_project .</code>

When the build finishes you will see a success message indicating the image was created.

3. Run the container

<code>docker run -it -d --name malina_php_project malina_php_project</code>

Check that the container is running:

<code>docker ps</code>

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

<code>docker exec -it 18b429b2ceac /bin/bash</code>

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)

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

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

DockerBackend DevelopmentDevOpsContainerizationPHPApache
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

login 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.