Building and Running a Dockerized PHP "Hello World" Application

Sheryar SherSheryar Sher
4 min read

As a Computer Science student exploring web development and DevOps, I recently built and containerized a simple PHP application using Docker. Although the application simply prints "Hello, World!", this exercise provided practical experience in Docker image creation, containerization, and publishing on Docker Hub.

This post documents the process, including building the image, running it locally, and publishing it for others to pull and use.

Project Overview

Goal

  • Create a minimal PHP web app

  • Dockerize the application using a custom Dockerfile

  • Run the containerized app locally

  • Push the image to Docker Hub

  • Pull and run the image on any system with Docker installed

Application Code

The PHP application consists of a single file:

index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Php hello world</title>
</head>
<body>
    <h1><?php echo "Hello, world!;" ?></h1>
    <p>The current date and time is: <?php echo date('Y-m-d H:i:s'); ?></p>
</body>
</html>

Dockerfile

Dockerfile

# official page of the php
FROM php:8.2-apache

# setting working directory
WORKDIR /var/www/html

# copy applicatoin source code to the working directory
COPY . /var/www/html/

Directory Structure

dockerized-php-app /
├── Dockerfile
└── index.php

Building and Running the Container Locally

1. Build the Docker Image

docker build -t dockerized-php-app  .

2. Run the Container

docker run -d -p 5000:80 dockerized-php-app

Visit http://localhost:5000 in a browser to view the output:

Hello, world!;
The current date and time is: 2025-04-14 22:05:01

Managing Docker Containers

After running your container, it’s important to understand how to manage it efficiently. Below are some essential Docker commands for interacting with containers.

Run a Container in Detached Mode

To run a container in the background (detached mode), use the -d flag:

docker run -d --name dockerized-php-app dockerized-php-app

This starts the container without tying up your terminal.

Run a Container in Interactive Mode

To interact with a container directly (helpful for debugging or testing):

docker run -it --name dockerized-php-app dockerized-php-app /bin/bash

This opens a shell inside the container where you can run commands interactively.

Enter a Running Detached Container

If you’ve started a container in detached mode and want to enter it later:

docker exec -it dockerized-php-app /bin/bash

This opens an interactive shell session inside the running container.

Stop a Running Container

To gracefully stop a running container:

docker stop dockerized-php-app

This sends a SIGTERM signal to the container to shut it down.

Remove a Stopped Container

Once a container is stopped, you can remove it using:

docker rm dockerized-php-app

To remove a container forcefully (even if it's running):

docker rm -f dockerized-php-app

Remove All Containers at Once

To remove all containers, whether running or stopped:

docker rm -f $(docker ps -aq)

This command lists all container IDs and removes them forcefully.

Docker Storage Management

Sharing Data Between Host and Container (Volume Binding)

Running the Container with Volume Binding

docker run -d -p 5000:80 \
  --name dockerized-php-app \
  -v ~/projects/dockerized-php-app:/var/www/html \
  dockerized-php-app

Publishing the Image to Docker Hub

To make the image accessible to others or across devices, you can push it to Docker Hub.

Step 1: Log in to Docker Hub

docker login

Step 2: Tag the Image

Format:

docker tag local-image-name dockerhub-username/repository-name

For example:

docker tag dockerized-php-app  yourdockerhubusername/dockerized-php-app

Step 3: Push the Image

docker push yourdockerhubusername/dockerized-php-app

After pushing, the image will be available on your Docker Hub profile under the specified repository name.

Pulling and Running the Image From Docker Hub

Once the image is pushed, it can be pulled and run from any machine with Docker:

docker pull yourdockerhubusername/dockerized-php-app

Run the container:

docker run -d -p 5000:80 yourdockerhubusername/dockerized-php-app

Visit http://localhost:5000 to confirm it's working.

Key Learnings

This project helped me understand:

  • How to build and run Docker containers

  • The structure of a basic Dockerfile

  • The process of publishing Docker images to Docker Hub

  • How Docker supports reproducible environments across machines

Conclusion

This project may be small, but the workflow it introduces is highly relevant in modern software engineering. Containerization, even for simple applications, promotes portability, consistency, and ease of deployment — all vital in professional development environments.

If you're new to Docker, I highly recommend trying a similar project to get familiar with the core concepts.

Thanks for reading. Feel free to reach out with any questions or feedback.

0
Subscribe to my newsletter

Read articles from Sheryar Sher directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sheryar Sher
Sheryar Sher

Full-stack developer | React, Django, Python | Passionate about building ML-powered web apps | Final year CS student