DEV Community

Cover image for Automating My Docker Apache Server with Python
Jude Wakim
Jude Wakim

Posted on

Automating My Docker Apache Server with Python

After building my first Docker project, I knew I couldn’t stop there. The Dockerfile worked perfectly, the image deployed, and Apache ran inside its own containerized environment. But there was still one essential piece missing:

🚀 Automation

So I built it.


The Project (Recap + Link)

In my previous article, I walked through setting up a full Dockerized Apache server workflow—building a Docker image, pushing it to Docker Hub, and deploying it in a custom Docker network.

You can check out that full breakdown here:

👉 End-to-End Docker Apache Server: Build, Push, and Networked Deploy


Why Python for Automation?

Docker CLI is great, but typing in the same sequence of commands repeatedly isn’t efficient—especially when you’re building and deploying containers frequently.

Using Python (and the official Docker SDK for Python) allows us to:

  • Automate the build → push → deploy cycle
  • Control deployments via CLI flags (e.g. --build)
  • Manage Docker networks and containers programmatically
  • Practice Python in a real-world DevOps workflow

Let’s dive into the updated project and see how everything fits together.


The Dockerfile 🐳

The Dockerfile sets up an Ubuntu-based container with Apache pre-installed and ready to serve:

FROM ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y apache2 && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

EXPOSE 80

CMD ["apachectl", "-D", "FOREGROUND"]
Enter fullscreen mode Exit fullscreen mode

This file is responsible for building a clean, self-contained environment that runs Apache in the foreground—perfect for containerized deployment.


The deploy.py Script 🐍

This is the automation magic.

The Python script does the following:

✅ Builds the Docker image locally (if the --build flag is passed)
✅ Tags and pushes the image to Docker Hub
✅ Pulls the image (even if already local, for consistency)
✅ Creates a custom Docker network (if it doesn't exist)
✅ Deploys the container inside that network
✅ Automatically removes any conflicting containers

Here’s a snippet of the logic behind the --build flag:

if args.build:
    print(f"Building image '{image_name}'...")
    image, logs = client.images.build(path=".", tag=image_name)
    # Push to DockerHub
    dockerhub_tag = f"{dockerhub_username}/{image_name}"
    image.tag(dockerhub_tag)
    client.images.push(dockerhub_tag)
    print(f"Image pushed to {dockerhub_tag}")
else:
    # Pull from DockerHub
    client.images.pull(dockerhub_tag)
Enter fullscreen mode Exit fullscreen mode

The full script handles everything from authentication to error catching and cleanup. You can test and run the automation with:

python3 deploy.py --build

Or just:

python3 deploy.py

To skip the build and pull the latest image from DockerHub.


Obstacles + Solutions 🧱

Docker SDK permissions – Needed to make sure Docker was properly installed and the Python SDK had access. Solved by ensuring Docker Desktop was running and permissions were configured.

Image caching – Docker caches builds, so when testing changes, the image might not reflect updates. Resolved by using the --build flag to force fresh builds.

Custom network errors – If a network already existed, the script would fail. Handled by checking for the network and reusing it if needed.

Local environment confusion – Mixing Docker CLI and Python SDK sometimes caused conflicts. Staying consistent with the SDK helped.


Final Thoughts 💭

This updated project now covers the full lifecycle of a Docker container—from image build to deployment—with no manual CLI steps required.

If you're learning Docker, automating your workflow with Python is a great way to cement your understanding while leveling up your DevOps toolkit.

You can check out the updated GitHub repo here:
👉 GitHub: judeWakim/apache-server-on-docker

Let me know if you try it or fork it—I'd love to see your improvements.

Happy coding! 🐳🚀


📬 Follow My Work

Want more DevOps and cloud projects?

📝 Medium

💼 LinkedIn

🐙 GitHub

💻 Dev.to

Like and comment if you liked this one — more hands-on AWS, Docker, and automation content coming soon. 🛠️💡

Top comments (0)