Docker stores its images on Linux in `/var/lib/docker`, a hidden directory that manages layers and metadata for every container you build. If you have ever wondered where are docker images stored linux, the answer is right there in that default location, but there is more to it than just a single folder. Understanding this storage system helps you manage disk space, troubleshoot issues, and optimize your Docker workflow.
When you pull or build a Docker image, it does not just sit as one big file. Instead, Docker breaks it into layers, each representing a set of changes. These layers are stored under `/var/lib/docker` using a storage driver like overlay2, aufs, or devicemapper. The exact structure depends on your Linux distribution and Docker version.
Let us walk through the default storage path, how layers work, and what you can do to locate or move your images. This guide is for anyone using Docker on Linux, whether you are a beginner or a seasoned developer.
Where Are Docker Images Stored Linux: The Default Location
The primary directory for Docker images on Linux is `/var/lib/docker`. This is where Docker keeps all its data, including images, containers, volumes, and networks. When you run `docker images`, you see a list of images, but the actual files live in subdirectories under this path.
Inside `/var/lib/docker`, you will find several key folders:
- image/ – Contains metadata about images, including layer relationships and configuration files.
- overlay2/ – Holds the actual layer data when using the overlay2 storage driver, which is the default on modern Linux systems.
- containers/ – Stores data for running and stopped containers.
- volumes/ – Persistent data volumes you create with `docker volume create`.
To see this for yourself, open a terminal and run:
sudo ls -la /var/lib/docker
You will notice that most files are owned by root, so you need sudo to view them. This is a security measure to prevent accidental changes.
How Docker Organizes Image Layers
Docker images are not monolithic. They consist of read-only layers stacked on top of each other. Each layer corresponds to a command in your Dockerfile, like `RUN apt-get update` or `COPY . /app`. When you pull an image, Docker downloads only the layers you do not already have locally.
These layers are stored as tar archives or directories inside the storage driver folder. For overlay2, each layer is a subdirectory under `/var/lib/docker/overlay2/`. The directory name is a hash that uniquely identifies the layer.
You can inspect the layers of an image using:
docker image inspect <image-name>
Look for the “RootFS” section in the JSON output. It lists all layer hashes that make up the image.
Checking Your Current Storage Driver
Different Linux distributions may use different storage drivers. To find out which one your Docker daemon is using, run:
docker info | grep "Storage Driver"
Common outputs include overlay2, aufs, devicemapper, or btrfs. The storage driver determines exactly how layers are stored on disk. For example, overlay2 uses a union filesystem to merge layers, while devicemapper uses block devices.
If you are using overlay2, your images are in `/var/lib/docker/overlay2/`. For aufs, they are in `/var/lib/docker/aufs/`. The structure varies, but the principle is the same: layers are stored as separate directories or files.
Why Knowing The Storage Location Matters
Understanding where your Docker images live helps you manage disk space. Over time, unused images, dangling layers, and old containers can eat up gigabytes of storage. You might run out of space on your root partition if you do not clean up regularly.
Another reason is backup and migration. If you need to move Docker to a new server, you can copy the entire `/var/lib/docker` directory. However, it is often better to use `docker save` and `docker load` for individual images, as they create portable tar files.
Security is also a factor. The Docker directory contains sensitive data, including environment variables and application code. Ensure that only root and the docker group have access to `/var/lib/docker`.
How To View The Size Of Your Docker Images
To see how much space your images are using, use:
docker system df
This command shows the total disk usage for images, containers, volumes, and build cache. It breaks down the size by type, helping you identify what is consuming space.
For a more detailed view of individual images, run:
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
This lists all images with their sizes in a readable table. You can then decide which ones to remove.
Cleaning Up Unused Images And Layers
Docker provides a simple command to remove unused data:
docker system prune -a
This removes all stopped containers, dangling images, and unused networks. Add the `–volumes` flag to also delete unused volumes. Be careful, as this action is irreversible.
If you want to remove only unused images (not containers), use:
docker image prune -a
This keeps images that are tagged or used by at least one container. Dangling images, which have no tag and are not referenced, are removed by default.
Changing The Default Storage Location
Sometimes you need to move Docker images to a different partition or disk. For example, if your root partition is small, you can configure Docker to use a larger drive. This involves editing the Docker daemon configuration file.
Here is how to change the storage location:
- Stop the Docker service:
sudo systemctl stop docker - Create a new directory for Docker data, e.g.,
sudo mkdir -p /mnt/docker-data - Edit the daemon configuration file:
sudo nano /etc/docker/daemon.json - Add the following line:
{ "data-root": "/mnt/docker-data" } - Save the file and start Docker:
sudo systemctl start docker
If you already have images in the old location, you need to copy them over. Use rsync to transfer the data:
sudo rsync -aP /var/lib/docker/ /mnt/docker-data/
Then restart Docker. Verify the change with:
docker info | grep "Docker Root Dir"
This should now show your new path.
Potential Issues When Moving Docker Data
Moving the Docker directory can cause problems if not done carefully. Permissions must be preserved, and the new location must have enough space. Also, if you are using SELinux or AppArmor, you may need to update security policies.
Another issue is that some storage drivers, like devicemapper, rely on loopback devices that are tied to the original path. In such cases, it is safer to export images with `docker save` and re-import them after changing the data root.
Always back up your images before making changes. Use `docker save` to create tar files of important images:
docker save -o my-image.tar my-image:latest
You can later load them on the new setup with `docker load -i my-image.tar`.
Where Are Docker Images Stored Linux: Advanced Scenarios
For production environments, you might use a dedicated storage driver like overlay2 with a separate filesystem. Some setups use LVM or ZFS for better performance and snapshots. In these cases, the storage location might still be `/var/lib/docker`, but the underlying filesystem is different.
Docker also supports remote storage backends, such as NFS or Ceph. If you configure Docker to use a remote filesystem, images are stored on the network drive instead of the local disk. This is useful for clusters where multiple nodes need access to the same images.
To check if your Docker is using a remote filesystem, look at the “Docker Root Dir” in `docker info`. If it points to an NFS mount, that is where images are stored.
Inspecting Individual Image Layers On Disk
If you want to dig deeper into the actual files of an image, you can explore the overlay2 directory. First, find the layer IDs of an image:
docker image inspect <image-name> | jq '.[].RootFS.Layers'
Each layer ID corresponds to a directory under `/var/lib/docker/overlay2/`. You can list the contents of a layer:
sudo ls -la /var/lib/docker/overlay2/<layer-id>/diff
The `diff` folder contains the actual files added or changed in that layer. This is useful for debugging or verifying that your image contains the expected files.
Note that layers are read-only. Any changes made in a container are stored in a writable layer on top, which is created when the container starts.
Using Docker Export And Import For Portability
If you need to move images between machines without copying the entire Docker directory, use `docker save` and `docker load`. This creates a single tar file that includes all layers and metadata.
Example:
docker save -o ubuntu.tar ubuntu:latest
scp ubuntu.tar user@remote-server:/tmp/
ssh user@remote-server
docker load -i /tmp/ubuntu.tar
This method is more portable than copying `/var/lib/docker` because it works across different storage drivers and Docker versions.
Common Misconceptions About Docker Image Storage
Some people think Docker images are stored in the home directory or in a user-specific folder. That is not true. Docker runs as a system service and stores data system-wide under `/var/lib/docker`. Only root or users in the docker group can access it.
Another myth is that removing an image from `docker images` frees up all its disk space immediately. In reality, if a container is still using that image, the layers remain. You must remove all dependent containers first.
Also, do not confuse image layers with container layers. Container layers are writable and exist only while the container is running. They are stored separately under `/var/lib/docker/containers/`.
How To Find The Exact Path Of A Specific Image
To locate the on-disk path of a particular image, you can combine `docker image inspect` with the layer hashes. But there is no direct command that gives you the full path. Instead, you can use a script to map image IDs to overlay2 directories.
Here is a simple approach:
- Get the image ID:
docker images --no-trunc - Find the layer hashes from the image inspect output.
- Search for those hashes under `/var/lib/docker/overlay2/`.
This manual method works, but for automation, tools like `docker system df –verbose` provide a summary of disk usage per image.
Frequently Asked Questions
Where Are Docker Images Stored Linux By Default?
Docker images are stored in `/var/lib/docker` by default. The exact subdirectory depends on the storage driver, typically `overlay2` on modern systems.
Can I Change Where Docker Images Are Stored On Linux?
Yes, you can change the storage location by editing `/etc/docker/daemon.json` and setting the `data-root` parameter to a new directory. Then restart Docker.
How Do I Check The Size Of Docker Images On Linux?
Use `docker system df` to see total disk usage, or `docker images` with the `–format` option to view individual image sizes.
Is It Safe To Delete Files In /Var/lib/docker?
No, do not manually delete files in `/var/lib/docker`. Use Docker commands like `docker system prune` to safely remove unused data.
What Happens If /Var/lib/docker Runs Out Of Space?
Docker will fail to pull new images or start containers. You need to free up space by pruning unused data or moving the Docker directory to a larger partition.
Now you have a solid understanding of where are docker images stored linux and how to manage them. The default location is `/var/lib/docker`, but you can customize it to fit your needs. Keep an eye on disk usage, clean up regularly, and use portable methods like `docker save` for backups. This knowledge will help you run Docker more efficiently on any Linux system.