Day 16: Docker for DevOps Engineers

Photo by Ian Taylor on Unsplash

Day 16: Docker for DevOps Engineers

DevOps Learning

What Is Docker?

Docker is like a lunchbox for software applications. Instead of carrying each food item separately (like a burger, fries, and a drink), you put them all in one lunchbox. Similarly, Docker lets you package an entire application, including its code, libraries, and settings, into a single container.

Use Cases of Docker:

  1. Web Servers:

    • Imagine you’re hosting a website. Instead of setting up the server manually (which can be tricky), you create a Docker container with the web server software, your website code, and any other necessary tools. It’s like putting the entire website into a lunchbox!

    • Benefits: Easy to move, share, and deploy. Consistent across different computers.

  2. Microservices:

    • Microservices are like building blocks for big applications. Each microservice does a specific job (e.g., handling payments or sending emails).

    • Docker allows you to create separate containers for each microservice. It’s like having a lunchbox for each job—neat and organized!

  3. Testing Environments:

    • Developers need to test their code before releasing it. Docker containers provide a clean and identical environment for testing.

    • It’s like having a lunchbox with all the tools needed for testing—no messy desks!

How to Run and Configure Docker:

  1. Install Docker:

    • Just like getting a lunchbox, you need to install Docker on your computer. Here’s how:

      • For Windows: Download and install Docker Desktop.

      • For macOS: Install Docker Desktop for Mac.

      • For Linux: Follow the instructions for your specific Linux distribution.

  2. Create a Dockerfile:

    • Think of a Dockerfile as a recipe for your lunchbox (container). It tells Docker what ingredients (software, code, etc.) to put inside.

    • Create a file named Dockerfile (no extension) in your project directory.

    • Open the Dockerfile in a text editor and write instructions. Here’s a simple example:

        # Use an official Python image as the base
        FROM python:3.9
      
        # Set the working directory inside the container
        WORKDIR /app
      
        # Copy your website code into the container
        COPY . /app
      
        # Install any dependencies (if needed)
        RUN pip install -r requirements.txt
      
        # Set up a web server (e.g., Flask or Django)
        CMD ["python3", "app.py"]
      
  3. Build the Container:

    • Run the following command in your terminal (make sure you’re in the same directory as your Dockerfile):

        docker build -t my-web-app .
      
      • -t my-web-app tags your container with the name “my-web-app.”

      • The . at the end specifies the current directory as the build context.

  4. Run the Container:

    • Now that your container is built, let’s run it:

        docker run -p 8080:80 my-web-app
      
      • -p 8080:80 maps port 8080 on your host machine to port 80 inside the container.

      • my-web-app is the name of your container.

  5. Voilà! Your app is up and running:

    • Open your web browser and visit http://localhost:8080.

    • You’ll see your website running inside the Docker container!

Thank you for reading😉.