Docker has become an essential tool in the world of software development and deployment, enabling developers to package applications into containers for consistency across various environments. However, understanding how Docker works requires knowing where Docker stores its images and containers on your system. In this article, we’ll break down the storage of Docker images, making it easy to understand where these files are stored and how to manage them.
Docker images overview
A Docker image is a lightweight, standalone, executable package that includes everything needed to run software, service, runtime, libraries, and settings. Images are fundamental in Docker because they provide a repeatable and consistent environment for your applications.
Images and containers
Docker images are different from containers. A Docker image is essentially a blueprint that defines how a container should be created, while a container is the runtime instance of that image.
Now, let’s explore the storage locations of Docker images.
Ready to supercharge your Docker infrastructure? Scale effortlessly and enjoy flexible storage with Cherry Servers bare metal or virtual servers. Eliminate infrastructure headaches with free 24/7 technical support, pay-as-you-go pricing, and global availability.
Default location on Linux
When you install Docker on Linux, the default location for storing Docker images is under /var/lib/docker
. This directory holds not only your images but also your containers, volumes, and other data Docker uses to function.
Within this directory, you'll find subdirectories like overlay2 (or sometimes aufs, depending on your Docker setup). This is where Docker stores image layers, and each layer is a read-only file system.
Here’s an example of how you can view the images Docker is storing:
List docker images
Use the following command to list all Docker images currently stored on your system:
docker images
View storage backend
The storage backend is important for understanding the layout of the Docker image files. Common backend drivers are overlay2, zfs, and btrfs. Run the following command to see which storage driver Docker is using:
$ docker info | grep Storage
Storage Driver: btrfs
Docker organizes images in layers, where each layer represents a different file system change (like installing a package or adding a file). These layers are stacked on top of each other using the storage driver to form a complete image.
Default location on windows and macOS
On Windows and macOS, Docker runs inside a virtual machine due to the way Docker utilizes Linux features. This means the image storage is a bit different from Linux, but the general concept remains the same.
For Windows, Docker Desktop stores images in the following path:
C:\ProgramData\DockerDesktop\
For macOS, Docker Desktop stores images within the internal VM under the directory:
/Users/<your-username>/Library/Containers/com.docker.docker/Data/vms/
In both cases, the actual image files reside within the Docker virtual machine, and you can interact with them via Docker’s command-line interface.
Managing docker storage
As you work with Docker, your system can accumulate a large number of images, containers, volumes, and other data. This can lead to disk space issues over time. Managing storage effectively is critical to keeping your development environment healthy.
Here are a few common practices for managing Docker storage:
Clean up unused images
To remove unused or untagged (dangling) images, you can run:
docker image prune
Remove all stopped containers
If you want to remove containers that are no longer running, use:
docker container prune
Check disk usage
Docker has a built-in command to check how much disk space is being used by images, containers, and volumes. Use this command:
$ docker system df
This will give you a breakdown of the storage consumption and help you decide if it’s time to prune unused images or containers.
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 1 1 13.26kB 0B (0%)
Containers 1 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
Also read: How to Build a Docker Image
Change docker’s storage location
If your system's default storage location runs low on space, you can configure Docker to use a different directory. To do this, you'll need to modify the docker.service file and change the --data-root
parameter. Here’s a basic guide on how to do this on Linux:
- Stop Docker:
sudo systemctl stop docker
- Create or Modify the Docker service file located at
/etc/docker/daemon.json
and add the new storage location:
{
"data-root": "/new/path/to/docker"
}
- Restart Docker:
sudo systemctl start docker
By default, Docker uses the system’s main disk for storage, but by changing the data-root parameter, you can configure it to use an external disk or any other location that better suits your needs.
Conclusion
Understanding where Docker stores its images is an important part of mastering containerized applications. On Linux, this data is typically stored in /var/lib/docker
, while on Windows and macOS, Docker uses a virtual machine to manage storage. Keeping track of your image storage and cleaning up unused images can help you optimize disk usage and maintain a smooth-running development environment.
By regularly managing Docker’s storage through pruning and moving the storage location when necessary, you can prevent your system from becoming overwhelmed with unused containers and images. Docker’s flexibility allows you to adjust these settings according to your project’s needs, ensuring your workflow remains efficient.