Debug School

rakesh kumar
rakesh kumar

Posted on

Explain the real time application of docker tools

what is docker ?
How docker tool plays important role into monitoring
write shell scripting for docker monitoring
How docker tool plays important role in builds
write shell scripting for build the source code

Docker is a tool that allows developers, sys-admins etc. to easily deploy their applications in a sandbox (called containers) to run on the host operating system i.e. Linux. The key benefit of Docker is that it allows users to package an application with all of its dependencies into a standardized unit for software development. Unlike virtual machines, containers do not have high overhead and hence enable more efficient usage of the underlying system and resources.

Image description

Image description

Image description

How docker tool plays important role into monitoring

Docker tools play a crucial role in monitoring containerized applications and infrastructure, providing insights into performance, resource utilization, and health. Here are some key Docker tools and examples of how they contribute to monitoring:

Docker Stats:

Role: Docker provides a built-in command called docker stats that allows you to monitor resource usage of running containers.

docker stats <container_id>
Enter fullscreen mode Exit fullscreen mode

This command provides real-time information about CPU usage, memory usage, network I/O, and block I/O of the specified container.
Docker Health Checks:

Role: Docker allows you to define health checks in the Dockerfile or during container creation. Health checks periodically assess the container's health, and you can use this information for monitoring.

HEALTHCHECK --interval=5s --timeout=3s CMD curl -f http://localhost/ || exit 1
Enter fullscreen mode Exit fullscreen mode

In this example, the container checks the health of an HTTP service every 5 seconds.
Docker Events:

Role: Docker daemon generates events for various Docker-related activities, such as container creation, start, stop, and more. Monitoring these events can help track changes in the environment.

docker events
Enter fullscreen mode Exit fullscreen mode

This command shows a real-time stream of events from the Docker daemon.
Docker Logging:

Role: Docker containers generate logs that can be collected and analyzed for monitoring purposes. Docker supports various logging drivers, and logs can be sent to external logging systems.

docker logs <container_id>
Enter fullscreen mode Exit fullscreen mode

Retrieve the logs of a specific container. Additionally, you can configure Docker to use logging drivers like JSON File, Syslog, or external logging services.
Docker Swarm and Kubernetes Monitoring:

Role: For orchestrating containers, Docker Swarm and Kubernetes provide monitoring features to track the health and performance of services and nodes.
Docker Swarm uses docker node ls to list nodes and their status.
Kubernetes has tools like Prometheus and Grafana for comprehensive monitoring and visualization.
Third-Party Monitoring Tools:

Role: Several third-party tools specialize in Docker monitoring, offering extended features, visualization, and alerting capabilities.
Example: Tools like Prometheus, Grafana, Datadog, and Sysdig can be integrated with Docker to provide advanced monitoring features.
Docker System Prune:

Role: Docker provides a command to remove unused data, such as stopped containers, dangling images, and volumes, which helps in maintaining a clean environment and can indirectly contribute to efficient monitoring.

docker system prune
This command removes stopped containers, dangling images, and unused networks.
Docker Bench Security:

Role: While not primarily a monitoring tool, Docker Bench Security checks the security aspects of a Docker setup. Regular checks with tools like this contribute to the overall health and security monitoring.

docker run -it --net host --pid host --cap-add audit_control -e DOCKER_CONTENT_TRUST=$DOCKER_CONTENT_TRUST -v /var/lib:/var/lib -v /var/run/docker.sock:/var/run/docker.sock -v /usr/lib/systemd:/usr/lib/systemd -v /etc:/etc --label docker_bench_security docker/docker-bench-security
Enter fullscreen mode Exit fullscreen mode

This command runs Docker Bench Security to check the security configuration of the Docker daemon.
These examples illustrate how Docker tools and commands can be utilized for monitoring containerized applications and infrastructure. Depending on the complexity and requirements of your setup, you might choose a combination of built-in Docker tools and third-party solutions to ensure effective monitoring and observability.

Write shell scripting for docker monitoring

One common approach for monitoring Docker containers is to use a shell script that collects information about container health, resource utilization, and other relevant metrics. Below is a simple example of a shell script that you can use for basic monitoring.

Docker Monitoring Shell Script:

Create a shell script named

# Function to check the health of a Docker container
check_container_health() {
    health_status=$(docker inspect --format='{{json .State.Health.Status}}' $container_id)

    if [ "$health_status" == "\"healthy\"" ]; then
        echo "Container $container_id is healthy."
        echo "Container $container_id is unhealthy."

# Function to display resource usage of a Docker container
display_resource_usage() {
    echo "Resource usage for container $container_id:"
    docker stats --no-stream $container_id

# Function to show the logs of a Docker container
show_container_logs() {
    echo "Logs for container $container_id:"
    docker logs $container_id

# Main function
main() {
    echo "Docker Monitoring Script"

    # Specify the container ID you want to monitor

    # Check container health
    check_container_health $target_container_id

    # Display resource usage
    display_resource_usage $target_container_id

    # Show container logs
    show_container_logs $target_container_id

# Execute the main function
Enter fullscreen mode Exit fullscreen mode


The script defines three functions:

check_container_health: Checks the health status of a specified Docker container.
display_resource_usage: Displays resource usage statistics for a specified Docker container.
show_container_logs: Shows the logs of a specified Docker container.
The main function:

Specifies the container ID you want to monitor (target_container_id).
Calls the three functions to check health, display resource usage, and show logs for the specified container.

Make the script executable:

chmod +x
Enter fullscreen mode Exit fullscreen mode

Run the script:

Enter fullscreen mode Exit fullscreen mode

Replace with the actual ID of the Docker container you want to monitor. The script will provide information about the health, resource usage, and logs of the specified container.

This is a basic example, and depending on your monitoring needs, you might want to enhance the script or use more specialized monitoring tools and frameworks for a comprehensive monitoring solution.

How docker tool plays important role in builds

Docker plays a crucial role in the build process of applications, providing a standardized and reproducible environment for building and packaging software. Here's how Docker tools contribute to the build process:

Environment Standardization:

Docker enables developers to create a consistent and reproducible build environment. The dependencies, libraries, and tools required for building an application can be encapsulated within a Docker image.

The Dockerfile is a script that defines the steps to create a Docker image. It includes instructions for installing dependencies, copying source code, and configuring the environment.
Docker images created from Dockerfiles serve as a portable and immutable unit that can be used across different stages of the development lifecycle.
Isolation and Dependency Management:

Docker containers provide isolation, allowing developers to package applications with their dependencies without affecting the host system.
Dependency conflicts are minimized as the build environment is encapsulated within the Docker image.
Build Cache:

Docker leverages a build cache to optimize the build process. If a Docker image has been built before and the Dockerfile hasn't changed, Docker reuses the intermediate layers from the cache.
This significantly speeds up subsequent builds, especially when dealing with large dependencies.
Multi-Stage Builds:

Docker supports multi-stage builds, allowing developers to create a streamlined final image without including unnecessary build artifacts and dependencies.
This reduces the size of the final Docker image, making it more lightweight and efficient for deployment.
Cross-Platform Builds:

Docker facilitates cross-platform builds by allowing developers to build an application in a Docker container on their local machine and then deploy the same image on different platforms without modification.
Integration with CI/CD:

Docker is seamlessly integrated into Continuous Integration/Continuous Deployment (CI/CD) pipelines. CI tools, such as Jenkins, GitLab CI, and Travis CI, can use Docker to build, test, and deploy applications.
Docker images serve as artifacts that can be versioned and deployed consistently across different environments.

Docker images encapsulate the build environment and application code, ensuring that the build process is reproducible across different environments.
This helps eliminate the "it works on my machine" problem and ensures consistency from development to production.
Docker Build Command:

The docker build command is used to build a Docker image from a Dockerfile. Developers can specify build arguments, tags, and other options during the build process.

docker build -t my-app:latest
Enter fullscreen mode Exit fullscreen mode

Community and Official Images:

Docker Hub and other container registries provide a vast collection of pre-built base images and official images for popular programming languages and frameworks.
Developers can use these images as a starting point for building their applications, saving time and ensuring a solid foundation.

write shell scripting for build the source code

Project Structure:
Assume you have a Node.js application with the following structure:

|-- Dockerfile
|-- src/
|   |-- app.js
|-- package.json
Enter fullscreen mode Exit fullscreen mode

Create a Dockerfile in the project root to define the build environment.

# Dockerfile
FROM node:14

WORKDIR /usr/src/app

# Copy package.json and package-lock.json separately to leverage Docker cache
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy application source code
COPY src/ ./src/

# Build the application
RUN npm run build

CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode
Create a shell script named to automate the build process.


# Specify the image name and tag

# Build the Docker image
docker build -t $IMAGE_NAME:$TAG .

# Run the Docker container
docker run -p 3000:3000 $IMAGE_NAME:$TAG
Enter fullscreen mode Exit fullscreen mode


The Dockerfile uses the official Node.js image as the base image.
It sets the working directory, copies the package.json and package-lock.json files, installs dependencies, copies the application source code, and then runs the build command (npm run build).
The script specifies the image name and tag, builds the Docker image using docker build, and then runs a container based on the built image using docker run.

Make the script executable:

chmod +x
Enter fullscreen mode Exit fullscreen mode

Run the script to build and run the Docker image:

Enter fullscreen mode Exit fullscreen mode

This example demonstrates how Docker plays a crucial role in the build process. It provides a consistent and isolated environment for building the Node.js application, encapsulating dependencies and ensuring reproducibility. The Dockerfile defines the steps for creating the image, and the shell script automates the build and run process.

Top comments (0)