Docker Series — Part 10: Building Custom Docker Images for Apache Web Servers

Nitin DhimanNitin Dhiman
3 min read

In this post, we move beyond just running containers. We explore how to build your own custom Docker image, step by step — using both docker commit and the more powerful, structured approach: Dockerfile.
You’ll also learn the difference between build-time and run-time instructions — and how to automate container behavior from the start.

Why Build Custom Docker Images?

We’ve all used docker run with public images. But what if you want:

  • Your own Apache web server setup?

  • Pre-configured files inside the container?

  • Automation during container startup?

That’s where building custom images comes in — and you have two ways to do it:

  1. Using docker commit (manual snapshot)

  2. Using a Dockerfile (declarative and reusable)


Method 1: Using docker commit

This method is like taking a snapshot of a container after configuring it manually.

Step-by-step:

  1. Run a container from CentOS:

     docker run -it centos:7
    
  2. Install Apache inside the container:

     yum install httpd -y
    
  3. Create your webpage:

     cd /var/www/html
     echo "vimal daga" > vimal.html
    
  4. Start the Apache server:

     httpd
    
  5. Exit the container, then:

     docker ps -a
     docker commit <container-id> myweb:v1
    

Now you’ve created a custom image from a live container. But this is manual — and not ideal for scaling or CI/CD.


A Dockerfile lets you define everything in code — like infrastructure-as-code for containers. Here’s how:

🧱 Sample Dockerfile:

#Dockerfile
FROM centos:7

RUN yum install httpd -y

WORKDIR /var/www/html

RUN echo "vimal daga" > index.html

CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

FROM: base image
RUN: install packages or run commands (build-time)
WORKDIR: set working directory
CMD: default command when container starts (run-time)

Build your image:

docker build -t myweb:v1 .

Run your container:

docker run -it myweb:v1

Now when the container launches:

  • Apache is already installed

  • HTML file is in place

  • Web server auto-starts


COPY vs docker cp

  • docker cp is used manually:

      docker cp index.html container_id:/var/www/html/
    
  • COPY in Dockerfile is better for automation:

      #Dockerfile
      COPY index.html /var/www/html/
    

Build-Time vs Run-Time Commands

TypeKeywordRuns when...
Build-timeRUNYou build the image
Run-timeCMD, ENTRYPOINTYou run a container from the image

This distinction is key when optimizing and automating Docker workflows.


Bonus Tip: Validate the Apache Process

Use ps aux inside your container to confirm:

ps aux | grep httpd

You’ll see the root + multiple Apache worker processes. That’s your web server in action


Recap

In this blog, you learned:

  • How to build custom Docker images in 2 ways

  • How to create and start an Apache web server in a container

  • Difference between RUN and CMD

  • Dockerfile structure and key instructions

  • Real-world automation for web hosting inside Docker


Still confused about image builds or when to use which command?
Ping me anytime — I’d love to help you level up!

#Docker #DevOps #Dockerfile #Apache #WebServer #Containerization #Linux #InfrastructureAsCode #DockerSeries #LearningInPublic #OpenSource #CloudNative #DockerBuild

0
Subscribe to my newsletter

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

Written by

Nitin Dhiman
Nitin Dhiman

Self-taught DevOps enthusiast on a journey from beginner to pro. Passionate about demystifying complex tools like Docker, AWS, CI/CD & Kubernetes into clear, actionable insights. Fueled by curiosity, driven by hands-on learning, and committed to sharing the journey. Always building, always growing 🚀