- Random Access Musings
- Posts
- Diving into Docker: Crafting an Nginx Container (Part 1)
Diving into Docker: Crafting an Nginx Container (Part 1)
From Ansible Playbooks to Dockerfiles: The Next Step in Configuration Management
Hello Muser!

In our last series, we talked about Ansible and how it helps manage IT setups. We saw how Ansible playbooks can set up and standardize IT systems, making sure everything is consistent no matter where it's running.
Today, we're moving on to Docker. Docker is kind of like Ansible, but instead of setting up servers, it packs everything an app needs into a container. It's like putting your whole app and its tools into a box you can run anywhere. I would have loved to use pihole as an example for this as well but containerizing an application can have some subtle gotchas that I'll go over in my next newsletter.
Instead, we'll be creating a simple nginx web container that displays a Hello Message!
Also, as a reminder, you can always leave a comment with any questions or thoughts!

Getting Started with Docker
Before we dive into making our Dockerfile, let's make sure you have Docker installed:
1. Installing Docker CLI:
For Windows: The official Docker documentation for Windows
For macOS (using Homebrew):
brew install --cask docker
Once installed, you can start Docker Desktop from your Applications folder.
2. Setting up our Git Repository on GitHub:
Go to GitHub and create a new repository named
docker-nginx
.Once created, clone the repository to your local machine:
git clone https://github.com/[your-username]/docker-nginx.git
cd docker-nginx
3. Crafting Our Web Page:
Let's make a simple
index.html
. Here's what you can put in:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello from Docker!</title>
</head>
<body>
<h1>Hello from Docker!</h1>
</body>
</html>
Save this as
index.html
in yourdocker-nginx
folder. Then, commit this file to our Git repository:
git add index.html
git commit -m "Add simple web page for Nginx"
4. Starting with Ubuntu:
We'll use the official Ubuntu Docker image. Create a new file named
Dockerfile
and open it in an editor. The following steps should all be modifications to yourDockerfile
.
FROM ubuntu:latest
5. Updating the System:
Like we did with Ansible, we'll update the system first.
RUN apt-get update && apt-get upgrade -y
6. Getting What We Need:
Nginx needs some tools. Let's get them.
RUN apt-get install -y nginx
7. Adding Our Web Page to Nginx:
Now, let's add our
index.html
to our Docker container.
COPY index.html /var/www/html/
8. Opening Ports:
Nginx needs some ports open to serve our web page.
EXPOSE 80
9. Running Nginx:
We'll tell Docker to run Nginx when the container starts.
CMD ["nginx", "-g", "daemon off;"]
10. Committing Our Dockerfile to Git:
If you've followed along all the steps, your
Dockerfile
should now look like this:
# Start with the official Ubuntu image
FROM ubuntu:latest
# Update the system
RUN apt-get update && apt-get upgrade -y
# Install Nginx
RUN apt-get install -y nginx
# Copy our custom web page
COPY index.html /var/www/html/
# Open the port Nginx needs
EXPOSE 80
# Run Nginx when the container starts
CMD ["nginx", "-g", "daemon off;"]
Let's commit to our Git repository.
git add Dockerfile
git commit -m "Initial Dockerfile for Nginx setup"

Building, Running, and Testing Our Nginx Container
Now that we have our Dockerfile ready, it's time to bring our Nginx web server to life inside a Docker container. Here's how we do it:
1. Building the Docker Image:
In your terminal, navigate to the
docker-nginx
directory where your Dockerfile is located. Then, build the Docker image:
docker build -t my-nginx-image .
This command tells Docker to build an image using the Dockerfile in the current directory and name it "my-nginx-image".
2. Running the Docker Container:
With our image built, let's run it:
docker run -d --name my-nginx-container -p 8080:80 my-nginx-image
This command tells Docker to run a container named "my-nginx-container" using the "my-nginx-image" image. We're also mapping port 8080 on our machine to port 80 on the container.
3. Testing Our Web Page:
Open your favorite web browser and navigate to
http://localhost:8080
. You should see our "Hello from Docker!" message. Pretty cool, right?
4. Committing Our Changes to Git:
It's always a good practice to keep track of our progress:
git add .
git commit -m "Added instructions to build, run, and test the Nginx container"
git push

Connecting The Dots
There’s a lot going on here so let’s try and make sense of everything we just did. There are 4 points I think are important to understand:
Docker is Infrastructure-as-Code (just like Ansible)
We use git to manage that code (just like Ansible)
You create a Docker image from the
Dockerfile
You run one (or more) instances of that image
Just like with Ansible, we've taken a step-by-step approach to set up a service, but this time inside a Docker container. The beauty of Docker is that this container can now be run on any machine with Docker installed, ensuring the same environment and behavior everywhere.
Remember how we talked about Ansible ensuring every server is set up right? Docker takes that a step further by ensuring every app runs right, no matter where it is. It's like having a portable environment for your app, packed with everything it needs.
In my next newsletter, we'll dive deeper into Docker's features, explore the differences between a typical VM configuration vs containers, and talk about WHY we might want to run containers over VMs!
Remember, I’m happy to answer any questions or hear your thoughts in the comments!

Enjoyed this post? Subscribe now and share with your colleagues who might find this useful!
Keep learning and keep growing,
Reply