Docker Diff: Track Changes in Docker Containers

Docker diff to track changes in container

Docker containers provide a lightweight and consistent environment to run applications. But when working inside a container, it’s important to track changes made during its runtime. Whether you’re debugging, testing, or auditing, tracking filesystem changes can help. The docker diff command lets you see what has changed inside a container.

In this article, we’ll show you how to use docker diff to track changes in Docker containers.

What is docker diff?

The docker diff command tracks changes made to a container’s filesystem. It shows you which files have been Added (A), Modified (C), or Deleted(D) inside a running or stopped container. This command is handy when reviewing changes made during testing or after installing software in a container.

Here’s what docker diff can detect:

  • A: A new file or directory is added to the container.
  • C: An existing file or directory that has been changed.
  • D: A file or directory that has been deleted.

Basic Syntax

The basic syntax of the docker diff command is simple:

 # docker diff [OPTIONS] CONTAINER
  • CONTAINER: The ID or name of the container you want to track.

Note: Docker diff only works with running or stopped containers, not images.

Using Docker Diff to Track Changes

Let’s walk through an example to see how to use docker diff to track changes with example.

1. Start a container

First, we’ll start a container using the official Ubuntu image.

 # docker run -dit --name my_container ubuntu

This command starts a container named my_container using the Ubuntu image.

2. Make some changes

Next, we’ll make a few changes inside the container.

# docker exec my_container touch /newfile 
# docker exec my_container rm /etc/hostname 
# docker exec my_container apt-get update

Here, we’ve created a new file (/newfile), deleted a file (/etc/hostname), and updated the package list.

3. Track the changes

Now, let’s use docker diff to see what’s changed.

 # docker diff my_container

Output.

A /newfile
C /etc
D /etc/hostname
C /var/lib/apt

As you can see, Docker shows the added, changed, and deleted files.

Filtering Output using Grep

The docker diff command often produces a lot of output. You can use grep to filter the results based on the type of change (Added, Changed, or Deleted).

1. Show Only Added Files

To see only the files or directories that were added inside the container, use grep with the filter for “A”:

 # docker diff my-ubuntu-container | grep '^A'

Output.

A /tmp/my-new-file
A /var/lib/apt/lists/lock
A /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_focal_InRelease

2. Show Only Changed Files

To list only the files or directories that were modified inside the container, use this command:

 # docker diff my-ubuntu-container | grep '^C'

Output.

C /var/lib/apt/lists/partial\

3. Show Only Deleted Files

To display only the files or directories that were deleted, use grep to filter for “D”:

 # docker diff my-ubuntu-container | grep '^D'

Alternatives to docker diff for Tracking Changes

While docker diff is helpful in tracking filesystem changes, other Docker commands offer different insights. Let’s explore some alternatives.

1. Docker Logs

The docker logs command lets you track the output of processes running inside the container. It displays the logs from the container’s standard output (stdout) and standard error (stderr). This helps monitor application behavior and errors.

For example, run the docker logs command to track changes related to processes inside the container in real time.

 # docker logs -f example-container

Output.

[INFO] Server started on port 8080

2. Docker History

The docker history command shows the history of an image’s creation. It lists the commands used to create each image layer, giving you insight into the build process and any modifications made.

For example, Run the following command to display the history of the layers that make up the Ubuntu:latest Docker image.

 # docker history ubuntu:latest

Output.

IMAGE        CREATED          CREATED BY                                      SIZE
c6f1abc2c60c 3 days ago       /bin/sh -c #(nop)  CMD ["bash"]                 0B
f5b35c7c19e2 3 days ago       /bin/sh -c #(nop) ADD file:abc123 in /           27.5MB

Limitations of docker diff

While docker diff is a great tool, it has its limitations:

  • Only tracks changes in the filesystem. It won’t show changes to the container’s metadata, processes, or environment variables.
  • Doesn’t show network or configuration changes, so it’s useful mainly for tracking file and directory modifications.

Conclusion

Tracking changes in Docker containers is essential for managing, debugging, and auditing your applications. Docker provides several built-in commands like docker diff, docker logs, and docker history to help you track these changes efficiently. Additionally, external tools can offer more advanced features if needed.

FAQs

1. How do I track changes in real-time with docker diff?

docker diff is not designed for real-time tracking, but you can run it periodically or after a significant operation to see changes.

2. Does docker diff show changes to temporary files created during container runtime?

Yes, if files are created, modified, or deleted in the container's file system during runtime, docker diff will show these changes.

3. Can docker diff track environment variable changes?

No, docker diff only tracks file system changes. Environment variables are not tracked by this command.

4. Can I use docker diff on a stopped container?

Yes, docker diff works on both running and stopped containers.

About Hitesh Jethva

I am Hitesh Jethva, Founder and Author at Code2DevOps.com. With over 15 years of experience in DevOps and open source technologies, I am passionate about empowering teams through automation, continuous integration, and scalable solutions.

View all posts by Hitesh Jethva