Docker Series — Part 11: Automating Apache Containers with CMD, Save/Load & Runtime Control

Nitin DhimanNitin Dhiman
3 min read

We’ve already built and deployed custom containers. But what if we want full control over when they start, what they execute, and even transfer them across systems?
In this session, we explore how to automate Docker services using CMD, use docker save/load, and build a fully self-sufficient Apache web server container.

Starting Docker Automatically on System Boot

To ensure Docker starts every time your system does:

systemctl enable docker
systemctl status docker

This is useful on servers, VMs, or persistent environments where Docker must be running at boot.


2. Running a Container for One-Time Tasks

When you run a command like:

docker run -it centos:7 date

Docker spins up a container, runs the date command, and then exits.

Same with:

docker run -it centos:7 sleep 5

The container’s lifecycle = the lifecycle of the command.


3. Automating Runtime with CMD in Dockerfile

Instead of passing commands at runtime, you can define them in a Dockerfile:

FROM centos:7
CMD date

Build and Run:

docker build -t myd:v1 .
docker run -it myd:v1

Now, every time you run this container, it will execute date by default. You can also override this:

docker run -it myd:v1 cal

4. Creating a Custom Apache Web Server Image

Let’s automate the launch of a full web server container.

Dockerfile:

FROM centos:7
RUN yum install httpd -y
RUN yum install net-tools -y
RUN echo "welcome" > /var/www/html/index.html
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

Build & Run:

docker build -t myh:v1 .
docker run -it myh:v1

Now the Apache server starts automatically and serves a page at http://<container-ip>/index.html.

Validate:

curl http://172.17.0.2/index.html
# Output: welcome

5. Saving & Sharing Docker Images

Save the image as a tar file:

docker save myh:v1 -o myweb.tar

Load the image on another system:

docker load -i myweb.tar

This makes your Docker images portable — ready to ship to teammates, clients, or other servers.


Recap

FeaturePurpose
CMDAutomates container behavior at runtime
systemctl enable dockerAuto-start Docker on boot
docker save/loadShare or migrate Docker images
Apache via DockerfileLaunch full server with one command

Why This Matters

As your Docker usage scales, you’ll often need:

  • Containers that run scripts, cron jobs, or services

  • Image backups and portability

  • Default behaviors without manual intervention

Understanding CMD, ENTRYPOINT, and image saving/loading is essential to becoming infrastructure-ready.


Have any questions or ideas about Docker automation, saving images, or web servers in containers?
Let’s discuss — I’m always happy to help the DevOps fam!

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 🚀