Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker diff command

The docker diff command allows you to inspect the changes made to a container's filesystem compared to its base image. This can be useful for understanding what modifications have occurred within the container, such as added, deleted, or modified files and directories. In this tutorial, we'll cover the basics of using the docker diff command.

Syntax:

docker diff CONTAINER
  • CONTAINER: The name or ID of the container you want to inspect.

Example:

  1. Run a container and make some changes:

    docker run -it --name my-container ubuntu /bin/bash
    

    Inside the container, create a new file, modify an existing file, and delete a file:

    touch new-file.txt
    echo "modified" > /etc/hostname
    rm /etc/issue
    exit
    
  2. Inspect the changes made to the container's filesystem:

    docker diff my-container
    

    This command will display a list of changes made to the container's filesystem. The output will show added (A), deleted (D), or modified (C) files and directories with their respective paths:

    C /etc
    A /new-file.txt
    C /etc/hostname
    D /etc/issue
    

In this example, we can see that a new file (new-file.txt) has been added, an existing file (/etc/hostname) has been modified, and a file (/etc/issue) has been deleted.

This tutorial should give you a basic understanding of how to use the docker diff command to inspect changes made to a container's filesystem compared to its base image. This can be helpful for understanding what modifications have occurred within the container.

  1. How to Use Docker Diff Command:

    • Description: docker diff displays changes to files in the container's filesystem.
    • Code: Syntax of docker diff:
      docker diff [OPTIONS] CONTAINER
      
  2. Inspecting Changes with Docker Diff:

    • Description: Use docker diff to examine filesystem changes in a running or stopped container.
    • Code: Example of inspecting changes in a container:
      docker diff my_container
      
  3. Viewing File Changes in Docker Containers:

    • Description: docker diff provides a list of added, modified, or deleted files in the container filesystem.
    • Code: Example of viewing file changes:
      docker diff my_container
      
  4. Comparing Container Filesystem Before and After Changes:

    • Description: Use docker diff to compare filesystem states before and after specific operations.
    • Code: Example of comparing changes:
      docker run -d --name my_container nginx:latest
      # Make changes inside the container
      docker diff my_container
      
  5. Monitoring Container Changes with Docker Diff:

    • Description: Regularly use docker diff to monitor container filesystem changes for security or auditing purposes.
    • Code: Example of monitoring changes:
      watch docker diff my_container
      
  6. Automating Docker Container Change Detection:

    • Description: Integrate docker diff into scripts or monitoring tools for automated change detection.
    • Code: Example of using docker diff in a script:
      changes=$(docker diff my_container)
      
  7. Troubleshooting with Docker Diff:

    • Description: docker diff can assist in identifying issues by revealing unexpected filesystem modifications.
    • Code: Example of troubleshooting with docker diff:
      docker diff my_container