Hereโs a complete tutorial on the docker diff command, explaining how it works, its purpose, and providing a comprehensive list of examples.
What is docker diff?
docker diff is a Docker command used to inspect changes made to the filesystem of a container. It shows the differences between the container’s current state and its original image.
Key Features:
- Lists changes to the container’s filesystem.
- Helps in debugging and auditing what files have been added, modified, or deleted in the container.
- Useful for verifying the impact of commands executed inside a running container.
Basic Syntax
docker diff [OPTIONS] CONTAINER
Code language: CSS (css)
Options:
There are no specific options for docker diff. You simply provide the container name or ID.
Output Explanation
docker diff provides three types of changes:
- A โ File or directory was added to the container.
- C โ File or directory was changed (modified).
- D โ File or directory was deleted from the container.
Example Output:
C /etc/hostname
A /new_file.txt
D /tmp/old_log.txt
C /etc/hostnameโ/etc/hostnamewas modified.A /new_file.txtโnew_file.txtwas added to the container.D /tmp/old_log.txtโold_log.txtwas deleted from/tmp.
Examples of docker diff
1. Check Changes in a Running Container
docker diff my_container
This command shows all changes to the my_container filesystem.
2. Check Changes After Modifying a File
- Start a container:
docker run -dit --name my_ubuntu ubuntu - Modify or create a file in the container:
docker exec my_ubuntu bash -c "echo 'Hello Docker' > /tmp/hello.txt" - Run
docker diffto check the change:docker diff my_ubuntuOutput:A /tmp/hello.txt
3. Detect Deleted Files
- Start a container:
docker run -dit --name my_nginx nginx - Delete a file inside the container:
docker exec my_nginx rm /etc/nginx/conf.d/default.conf - Run
docker diff:docker diff my_nginxOutput:D /etc/nginx/conf.d/default.conf
4. Monitor Changes in a Container’s Temporary Files
Containers often generate temporary files in /tmp or /var during operation. You can use docker diff to track these changes:
docker diff my_temp_container
This helps identify logs or cache files added during runtime.
5. Check Changes in a Stopped Container
You can run docker diff on both running and stopped containers:
docker diff stopped_container
6. Debugging File Changes in a Database Container
If you’re running a MySQL container and suspect that files in /var/lib/mysql have been modified:
docker diff my_mysql
This will list all changes in the /var/lib/mysql directory, helping you verify data persistence or accidental modifications.
7. Combine docker diff with Other Commands
Using docker diff with docker exec
To confirm a change detected by docker diff, use docker exec:
docker exec my_container cat /path/to/file
Using docker diff with docker commit
If you want to save a modified container after detecting changes:
docker commit my_container my_new_image
List of Common docker diff Commands
| Command | Description |
|---|---|
docker diff my_container | Show all changes in the containerโs filesystem |
docker diff stopped_container | Inspect changes in a stopped container |
docker diff my_nginx | Check changes in an NGINX container |
docker diff my_database | Monitor changes in a database container |
docker diff container_id | Use container ID to check for filesystem changes |
Best Practices for docker diff:
- Use
docker difffor debugging unexpected behavior in containers. - Combine with
docker committo create a new image from a modified container. - Regularly monitor changes in critical containers (e.g., databases, web servers).
- Avoid leaving unnecessary files in containersโ
docker diffhelps track them.
Common Errors and Solutions
- “Error response from daemon: No such container”
โ Ensure the container name or ID is correct and the container exists. - “No changes detected”
โ This means no changes have been made since the container was created. - Permission Denied Errors
โ Ensure you have the necessary permissions to inspect the container.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals