🚀 Day 17 of My #90DaysOfDevOps Challenge: Docker Project for DevOps Engineers 🚀

🚀 Day 17 of My #90DaysOfDevOps Challenge: Docker Project for DevOps Engineers 🚀

Welcome back to my #90DaysOfDevOps journey! Today's task is particularly exciting as it involves Docker, a powerful tool for containerization that simplifies application development, deployment, and scaling. This blog dives into what I learned, how I approached the task, and why Docker is essential for DevOps engineers.


🔹 What is Docker?

Docker is an open-source containerization platform that allows developers to package applications with all their dependencies into containers. These containers are lightweight, portable, and ensure consistency across environments—whether you're running on your local machine, staging servers, or production.


🔹 What is a Dockerfile?

A Dockerfile is a simple text file that contains instructions for building a Docker image.

  • It defines the base image (e.g., Python, Node.js, Ubuntu).

  • Specifies the dependencies and commands required for the application.

  • Bundles the application code and other necessary files into the container.

  • Configures the startup command to run the application.

Think of a Dockerfile as a recipe that Docker follows to create a fully functional container.


🔹 Task Overview

Here’s what the Day 17 task involved:

  1. Create a Dockerfile for a simple web application.

  2. Build a Docker image using the Dockerfile.

  3. Run the container and verify the application.

  4. Push the image to a repository like Docker Hub.


🔹 Step-by-Step Solution

1️⃣ Create a Simple Web Application

For this task, I chose Python and Flask to build a basic web application. Here’s the application code:

app.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, Docker! Welcome to my DevOps journey."

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

requirements.txt

Flask==2.1.2

2️⃣ Write the Dockerfile

The Dockerfile provides instructions to package the Flask application into a Docker container. Here’s the content:

Dockerfile

# Use an official Python runtime as the base image
FROM python:3.9-slim

# Set the working directory inside the container
WORKDIR /app

# Copy application files to the container
COPY . /app

# Install dependencies
RUN pip install -r requirements.txt

# Expose the application port
EXPOSE 5000

# Run the application
CMD ["python", "app.py"]

3️⃣ Build the Docker Image

To build the Docker image, I used the docker build command:

docker build -t my-flask-app .
  • -t: Tags the image with a name (my-flask-app).

  • .: Refers to the current directory containing the Dockerfile.


4️⃣ Run the Container

I ran the container to test the application using the docker run command:

docker run -p 5000:5000 my-flask-app
  • -p 5000:5000: Maps port 5000 of the container to port 5000 on the host.

Accessing http://localhost:5000 in a browser displayed the message:
“Hello, Docker! Welcome to my DevOps journey.”


5️⃣ Push the Image to Docker Hub

To share the image, I pushed it to Docker Hub:

  1. Log in to Docker Hub:

     docker login
    
  2. Tag the image with my Docker Hub username:

     docker tag my-flask-app <username>/my-flask-app
    
  3. Push the image to the repository:

     docker push <username>/my-flask-app
    

🔹 Key Concepts in Docker

  1. Images vs. Containers:

    • Image: A blueprint for creating containers.

    • Container: A running instance of an image.

  2. Base Images: Docker images can be based on official distributions like Python, Node.js, or Ubuntu.

  3. Port Mapping: Ensures the containerized application is accessible from the host.

  4. Docker Hub: A public repository for sharing Docker images.


🔹 Why is Docker Essential for DevOps?

  1. Consistency Across Environments: Docker eliminates the “it works on my machine” problem by ensuring consistent environments.

  2. Lightweight and Fast: Containers are lightweight compared to traditional virtual machines.

  3. Portability: Docker containers can run anywhere Docker is installed.

  4. Simplifies CI/CD: Docker integrates seamlessly with CI/CD pipelines for automated deployments.


🔹 Conclusion

Docker is a must-have tool for modern DevOps practices, enabling efficient application development, deployment, and scaling. This project gave me hands-on experience with creating Docker images, running containers, and pushing images to Docker Hub.