Introduction

Yesterday I ran pre-built containers from Docker Hub. Today, I stepped up from user to creator by packaging my own HTML webpage into a custom Docker image using a Dockerfile, running it as a web server, and testing how container immutability works in practice.

1. Research

  • What is the difference between a Docker Image and a Docker Container? A Docker Image is the read-only blueprint or template containing the app and its dependencies, while a Container is the live, running instance created from that image.
  • What is a Dockerfile, and why do we need one? A Dockerfile is a plain-text recipe with step-by-step instructions telling Docker how to build a custom image automatically. We need it so anyone can reproduce the exact same environment consistently with a single docker build command.
  • Why can't Docker build an image without a Dockerfile? Without a Dockerfile (or an existing container commit), Docker doesn't know which base OS/software to start from, which files to copy in, or what commands to run inside the image.
  • What happens if you change your HTML file after building the image? Nothing changes in the existing image or running container. Docker images are read-only snapshots taken at build time, so you must rebuild the image and start a new container for code edits to show up.

2. Creating the Workspace & Webpage

I created a Day2 project directory and built a simple index.html page with a welcome heading, a short introduction, and an emoji:

mkdir Day2 && cd Day2 nano index.html 

3. Writing the Dockerfile

Next, I created a Dockerfile to package my webpage inside an Apache web server:

FROM httpd:2.4 COPY index.html /usr/local/apache2/htdocs/index.html 
  • What does the FROM instruction do? It sets the starting foundation for my image by pulling the official Apache web server image (httpd:2.4) from Docker Hub.
  • What does the COPY instruction do? It takes index.html from my local project folder and copies it inside the image's filesystem.
  • How does Docker know where Apache serves website files from? According to the official httpd image documentation, Apache is pre-configured to serve web files from /usr/local/apache2/htdocs/, so placing index.html there lets Apache find and serve it automatically.

4. Building the Image & Running the Container

I built my custom image and launched it in detached mode (-d), mapping port 8080 on my host machine to port 80 inside the container:

docker build -t meshacks-webpage . docker run -d -p 8080:80 --name my-web-container meshacks-webpage docker images docker ps 

Running the Container

5. Viewing & Inspecting the Live Website

Navigating to http://localhost:8080 in my browser confirmed Apache was serving my custom page:

  • Is my container actually running? Yes—docker ps showed my-web-container with status Up.
  • Does my image exist? Yes—docker images listed meshacks-webpage locally.
  • Did I map the ports correctly? Yes—-p 8080:80 forwarded traffic from localhost:8080 on my machine to port 80 inside the container.
  • Is my HTML file inside the image? Yes—seeing my custom welcome text in the browser (and inspecting /usr/local/apache2/htdocs/ inside the container) confirmed index.html was packaged properly.

Inspecting the Live Website

6. Leaving No Zombies (Cleanup)

After verifying everything worked, I stopped and removed the container and deleted my custom image:

docker stop my-web-container docker rm my-web-container docker rmi meshacks-webpage docker ps -a docker images 

Cleanup

Reflection

  • What happens if you change your HTML file after building the image? (Practical Test): I edited index.html locally to add a new line of text and refreshed http://localhost:8080 without rebuilding. The browser still displayed the original version because the image had already locked in a snapshot of index.html when I ran docker build. To see the update, I had to rebuild the image and run a fresh container.
  • Which part of today's challenge was the most difficult? Understanding how host-to-container port mapping (-p 8080:80) bridges the isolated container network to my local browser—and realizing why local file edits don't automatically appear inside an already-built image.