How to List All Changed Files Inside Docker Containers

Discover how to use the "docker container diff" command to effortlessly identify added, changed, and deleted files within your Docker containers' file systems.

2 min read
How to List All Changed Files Inside Docker Containers
Photo by David Dibert on Pexels.

Did you know that you can easily identify the files that have been changed within a Docker container's file system? This information can be valuable, especially when you want to ensure that all necessary files are backed up and aren't lost when the container restarts. 🙄

In this article, we will explore how to use the command made available by Docker effectively and even automate the process for multiple containers.

Listing changed files and directories

To list all the files and directories that have been added, changed, or deleted within a Docker container, you can use the "docker container diff" command. Here is the syntax:

docker container diff <CONTAINER_NAME_OR_ID>
Make sure to replace <CONTAINER_NAME_OR_ID> with the container's name or ID!

Here is an example of its output for a MariaDB container that I use:

C /run
C /run/mysqld
A /run/mysqld/mysqld.pid
A /run/mysqld/mysqld.sock
Example of the result of a docker container diff command.

This command will output a list of files and directories that fall into the following categories:

  • A: added
  • C: changed
  • D: deleted

Automating the process

If you need to perform this check for multiple containers, manually finding and pasting each container's name or ID can be time-consuming. However, you can automate this process using the following script:

#!/bin/bash

# Get the names of all containers
container_names=$(docker container ls --format '{{.Names}}')

# Iterate over all container names
for container_name in $container_names
do
    # Execute `docker container diff` on the container
    docker container diff ${container_name}

    # Check if the command was successful or not 
    if [ $? -eq 0 ]
    then
        echo "The command 'docker container diff' has been executed with success on container ${container_name}."
    else
        echo "The command 'docker container diff' has failed on container ${container_name}."
    fi
done
The Bash script that you can use to list all changed files and directories for all your containers.

By running this script, you will get the results for all your containers. The output will appear on the standard output, but you can easily modify the script to redirect the results to a file or multiple files according to your needs. 😉

Conclusion

By utilizing the docker container diff command and the provided script, you now have the ability to identify all the changed files within your Docker containers. This knowledge is crucial for ensuring that all necessary files are appropriately preserved, especially when working with container volumes. 🤗