Skip to content

Practice Exercise 2

Practice Exercise 2: Docker Container Monitoring Exercise

Objective: This exercise aims to equip interns with practical knowledge in monitoring Docker containers using various commands and tools. Upon completion, interns should be able to answer the question: "How do you monitor containers?"

Prerequisites:

  1. Basic understanding/review of Docker commands.
  2. Access to a Linux host with Docker installed.

Exercise Steps:

Part 1: Basic Docker Container Monitoring

Step 1: List Running Containers

  1. Open a terminal on the Linux host.
  2. Use the docker ps command to list running containers.
  3. Observe container IDs, names, and other relevant information.

Step 2: Container Resource Monitoring

  1. Use the docker stats command to display live container resource usage.
  2. Observe CPU usage, memory usage, and network activity.

Step 3: Container Logs

  1. Use the docker logs <container_id> command to access container logs.
  2. Identify log entries related to container events and activities.

Part 2: Inspecting Docker Containers

Step 1: Container Inspection

  1. Use the docker inspect <container_id> command to obtain detailed information about a specific container.
  2. Examine the JSON output and identify key container attributes.

Step 2: Analyzing Container Metrics

  1. Extract specific metrics (e.g., CPU, memory) from the docker inspect output.
  2. Understand how to use this information for monitoring.

Part 3: Docker Compose Monitoring

Step 1: Compose Services

  1. If not already done, create a Docker Compose file with multiple services.
  2. Use docker-compose up -d to start the services.

Step 2: Monitoring Compose Services

  1. Use the docker-compose ps command to list running services.
  2. Explore monitoring options for the entire Compose environment.

Part 4: Creating a Docker Monitoring Script

Step 1: Write a Monitoring Script

  1. Write a shell script that incorporates Docker commands used in the exercise.
  2. The script should provide an overview of running containers and their resource usage.
  3. The script will do the following:

    • It will monitor a specific host resource.

    • Will have an exit status.

    • Will notify through email.

    • Will have to log every time the script runs.

    • The script will run every 5 minutes.