Cyber Month Deal - up to 36% OFF

Where Docker Stores Images & Containers: A Quick Guide

Published on Oct 22, 2024 Updated on Oct 22, 2024

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.

Cloud VPS - Cheaper Each Month

Start with $9.99 and pay $0.5 less until your price reaches $6 / month.

Share this article

Related Articles

Published on Apr 10, 2024 Updated on May 15, 2024

How to Install Nextcloud AIO on Docker | Step-by-Step

This tutorial will show you how to install Nextcloud using Nextcloud AIO Docker image to help significantly reduce configuration steps.

Read More
Published on May 15, 2024 Updated on May 15, 2024

Portainer Tutorial: How to Update [and Restart] Portainer

This tutorial demonstrates how to manage Portainer: how to update Portainer, how to restart Portainer, and update existing containers.

Read More
Published on Apr 14, 2020 Updated on Feb 6, 2024

Docker Swarm vs. Kubernetes – What are the Subtle Differences?

Kubernetes and Docker Swarms are essential tools used to deploy containers inside a cluster. Learn how they differ and when to use them

Read More
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: 06ac5732e.831