Where Docker Stores Images & Containers: A Quick Guide

October 22nd, 2024
Where Docker Stores Images & Containers: A Quick Guide

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:

  1. Stop Docker:
sudo systemctl stop docker
  1. 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"
}
  1. 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.

With over 20 years in IT, Didier has been creating technical documentation for companies, catering to both technical and non-technical audiences. Didier is an expert in Linux system administration, DevOps, cloud computing, cybersecurity, IT consulting, management consulting, technical writing, Diataxis framework, Doc-as-Code, UX Writing, Jamstack, MkDocs, Docker, containers, open-source, SDLC, and Python programming. His hands-on technical expertise, coupled with his strong communication skills, enables him to bridge the gap between developers and end-users. Didier creates user guides, API References, end-user documentation, how-tos, and tutorials. He is an expert in authoring using modern technologies such as Markdown, Mermaid, and static-site generators. Didier also utilizes frameworks and methodologies such as Diaxiatis and Doc-as-code, applying structured writing techniques. Currently, Didier works as a freelance technical writer and documentation consultant, assisting organizations in creating comprehensive and easy-to-understand documentation for their software and web applications. In his previous roles, Didier worked as a system and applications engineer and implemented style guides, tone and voice best practices, and documentation processes that streamline their release cycles. Didier resides in Vacoas, Mauritius.

Start Building Now

Deploy your new Cloud VPS server in 5 minutes starting from $5.83 / month.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: b0376f5bd.760