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
Dockerfileis 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 singledocker buildcommand. - 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
FROMinstruction 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
COPYinstruction do? It takesindex.htmlfrom 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
httpdimage documentation, Apache is pre-configured to serve web files from/usr/local/apache2/htdocs/, so placingindex.htmlthere 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 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 psshowedmy-web-containerwith statusUp. - Does my image exist? Yes—
docker imageslistedmeshacks-webpagelocally. - Did I map the ports correctly? Yes—
-p 8080:80forwarded traffic fromlocalhost:8080on my machine to port80inside 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) confirmedindex.htmlwas packaged properly.
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 Reflection
- What happens if you change your HTML file after building the image? (Practical Test): I edited
index.htmllocally to add a new line of text and refreshedhttp://localhost:8080without rebuilding. The browser still displayed the original version because the image had already locked in a snapshot ofindex.htmlwhen I randocker 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.


