Day 18: Docker for DevOps Engineers pt 2

Day 18: Docker for DevOps Engineers pt 2

Publish Date: Aug 8
1 0

What is Docker Compose

Docker Compose is basically Docker’s multi-container manager — like a project coordinator for your containers.

Instead of you running 5 to 10 docker run commands manually for different containers, Compose lets you define everything (services, networks, volumes) in one YAML file (usually docker-compose.yml), and then start them all with one command:

docker-compose up

Enter fullscreen mode Exit fullscreen mode

Key Points

  • One file to rule them all → You describe your whole app’s setup in a docker-compose.yml file.

  • Multi-container orchestration → Runs several containers together (e.g., your app + database + cache).

  • Easy to share → The YAML file is portable; anyone can run your stack with docker-compose up.

  • Built-in networking → Containers can talk to each other by service name.

What is YAML?

YAML (pronounced “yah-mull”) is a human-friendly way to write structured data — think of it as a prettier, less noisy alternative to JSON or XML.

The name stands for "Yet Another Markup Language" or "YAML Ain’t Markup Language" (yes, they made the acronym recursive on purpose — tech folks love that) which emphasizes that YAML is for data, not documents.

Why YAML is popular

  • Easy to read → No curly braces {} or quotation marks unless needed.

  • Indentation-based → Uses spaces to show hierarchy (like Python).

  • Supports comments → You can write notes with #.

  • Widely used in DevOps → Docker Compose, Kubernetes, GitHub Actions, Ansible, etc., all use YAML

For the purpose of this task, you will need to have the pre-requisites to ensure you can follow along with the task.

PRE-REQUISITES

Make sure:

  • Docker is installed and running
  • Docker Compose is installed (docker-compose --version) - to check that docker-compose is installed
  • You're using a Linux machine or WSL/Ubuntu

TASK 1: Learn to Use docker-compose.yml

🔹 Step 1: Create a Project Directory

mkdir flask-docker-compose
cd flask-docker-compose
Enter fullscreen mode Exit fullscreen mode

🔹 Step 2: Create Your Flask App

mkdir app
cd app
touch app.py requirements.txt
Enter fullscreen mode Exit fullscreen mode

app.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return "Hello from Flask with Docker Compose!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
Enter fullscreen mode Exit fullscreen mode

🔹 Step 3: Create a Dockerfile (in flask-docker-compose/ directory)

# Dockerfile
FROM python:3.12-slim

WORKDIR /app

COPY app/ /app/

RUN pip install -r requirements.txt

EXPOSE 5000

CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

🔹 Step 4: Create docker-compose.yml in the root directory

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    environment:
      - FLASK_ENV=development
Enter fullscreen mode Exit fullscreen mode

🔹 Step 5: Run the App with Docker Compose

docker-compose up
Enter fullscreen mode Exit fullscreen mode

Open your browser and visit: http://localhost:5000
You should see: Hello from Flask with Docker Compose!

TASK 2: Pull a Public Docker Image and Run It Locally

🔹 Step 1: Pull a Pre-Built Image (e.g., Nginx)

docker pull nginx
Enter fullscreen mode Exit fullscreen mode

🔹 Step 2: Run Nginx Container

docker run -d --name mynginx -p 8080:80 nginx
Enter fullscreen mode Exit fullscreen mode

Visit: http://localhost:8080

🔹 Step 3: Run as Non-root User (optional)

If you're getting permission errors:

sudo usermod -aG docker $USER
reboot
Enter fullscreen mode Exit fullscreen mode

🔹 Step 4: Inspect Container

docker inspect mynginx
Enter fullscreen mode Exit fullscreen mode

Look for:

  • "ExposedPorts"
  • "Mounts"
  • "State"

🔹 Step 5: View Logs

docker logs mynginx
Enter fullscreen mode Exit fullscreen mode

🔹 Step 6: Stop & Start Container

docker stop mynginx
docker start mynginx
Enter fullscreen mode Exit fullscreen mode

🔹 Step 7: Remove Container When Done

docker rm -f mynginx
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment