Challenge 1: Create Your First Docker Container

In this challenge, you'll create a simple Docker container that runs a "Hello, World!" Node.js app. This is a great way to get familiar with Docker's basic concepts like images, containers, and Dockerfiles.

Step 1: Create a Simple Node.js App Create a file called 'app.js' with the following code:

// app.js
const http = require('http');
const port = 3000;
const requestHandler = (request, response) => {
  response.end('Hello, World! This is your first Docker container.');
};
const server = http.createServer(requestHandler);
server.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

Step 2: Write a Dockerfile Create a file named 'Dockerfile' in the same directory:

# Use an official Node.js runtime as a base image
FROM node:14
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json (if available)
COPY package*.json ./
# Install dependencies (none in this simple example, but it's good practice)
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Define the command to run the app
CMD ["node", "app.js"]

Step 3: Build and Run the Docker Image Open your terminal, navigate to the project directory, and run the following commands:

# Build the Docker image and tag it as "node-hello"
docker build -t node-hello .
# Run the Docker container from the image
docker run -p 3000:3000 node-hello

When you open your browser and navigate to http://localhost:3000, you should see the message "Hello, World! This is your first Docker container." This confirms that your container is running successfully.

Challenge 2: Containerizing a Python Flask Application

This challenge walks you through containerizing a basic Flask web app. Flask is perfect for small projects and prototypes.

Step 1: Write a Simple Flask App Create a file named 'app.py' with the following content:

# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return "Hello from Flask running in Docker!"
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Also, create a requirements.txt file using pip freeze > requirements.txt The file contains the required dependency. You should see this in the file:

Flask==2.0.1

Step 2: Create the Dockerfile Create a Dockerfile with the following code:

# Use the official Python image from the Docker Hub
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Copy requirements.txt and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code
COPY . .
# Expose the port the app runs on
EXPOSE 5000
# Define the command to run the app
CMD ["python", "app.py"]

Step 3: Build and Run the Docker Image Build and run your Flask container using these commands:

# Build the Docker image and tag it as "flask-app"
docker build -t flask-app .
# Run the Docker container, mapping port 5000
docker run -p 5000:5000 flask-app

Now, visiting http://localhost:5000 in your browser should display the message from your Flask app.

Challenge 3: Docker Compose – Running Multiple Containers

Docker Compose allows you to define and manage multi-container Docker apps. In this challenge, you'll set up a simple web service that includes a web server and a Redis instance.

Step 1: Create a Web Application We'll use a Python Flask app that connects to Redis. Create an 'app.py' file:

# app.py
from flask import Flask
import redis
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
@app.route('/')
def hello():
    # Increment a counter in Redis
    count = cache.incr('hits')
    return f"Hello! This page has been visited {count} times."
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Also, add a requirements.txt file. Use:

pip freeze > requirements.txt

To get:

Flask==2.0.1
redis==3.5.3

Step 2: Create a Dockerfile for the Flask App

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]

Step 3: Define Services with Docker Compose Create a 'docker-compose.yml' file to define the web app and Redis services:

version: '3.8'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - redis
  redis:
    image: redis:alpine
    ports:
      - "6379:6379"

Step 4: Build and Run the Application with Docker Compose Run the following command in your terminal:

docker-compose up --build

The web app should now be accessible at http://localhost:5000, and it will display the number of visits using Redis as a backend.

Challenge 4: Implementing Health Checks and Logging in Docker

In this challenge, you'll add a health check to a Docker container and configure basic logging. It is very important that your containerized application is healthy and logs information appropriately for it to be production ready.

Step 1: Create an Application with a Health Endpoint Let's use and extend our flask app from Challenge 2 to include a health check endpoint. Update your app.py like this:

# app.py
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def hello():
    return "Hello from Flask with Health Checks!"
@app.route('/health')
def health():
    # Return a JSON response indicating the service is healthy
    return jsonify(status="healthy"), 200
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Step 2: Update the Dockerfile with a Health Check Modify your Dockerfile to include a health check directive:

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
# Health check: Test the /health endpoint every 30 seconds
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
  CMD curl -f http://localhost:5000/health || exit 1
CMD ["python", "app.py"]

Step 3: Running the Container and Viewing Logs Build and run your Docker container:

docker build -t flask-health .
docker run -p 5000:5000 flask-health

To view the logs and verify the health check, use:

docker logs <container_id>

You should see periodic messages confirming that your container is healthy. In a production setting, you can integrate logging drivers or centralized logging systems for better monitoring.