Docker Disk Space Usage
Docker can potentially use a significant amount of disk space. This usage primarily comes from the following:
- Images: Docker images can be quite large, especially for complex applications. Each image contains all the necessary binaries and libraries required to run the application.
- Containers: Every time a container is run from an image, Docker creates a writable layer over the immutable image. This layer can accumulate changes, including new or altered files.
- Volumes: If you’re using Docker volumes to persist data, these can also take up space, depending on how much data your application is handling.
- Build cache: When building Docker images, Docker keeps a cache of intermediate images to speed up subsequent builds. This cache can grow over time.
Managing Disk Space
If disk space becomes an issue, you can manage it in several ways:
-
Prune unused Docker objects: Docker provides a pruning feature to clean up resources that are not in use, such as containers, images, networks, and build cache.
docker system prune
To also remove unused volumes, you can add the
-a
flag:docker system prune -a
-
Remove specific images or containers: If you know specific containers or images are no longer needed, you can remove them manually:
docker rmi ImageID docker rm ContainerID
Changing the Docker Storage Path
By default, Docker stores its data (images, containers, volumes, etc.) in /var/lib/docker/
on Linux. If you’re running out of space on your primary disk, you might consider moving Docker’s storage to a different disk with more space.
Here’s how you can change the default storage location:
-
Stop the Docker daemon:
sudo systemctl stop docker
-
Copy the Docker data to the new directory:
Suppose you want to move it to a new drive mounted at
/mnt/newdrive/
. You would do the following:sudo rsync -avxP /var/lib/docker/ /mnt/newdrive/docker
-
Update the Docker daemon configuration:
Edit the Docker service file. This file might be located at
/lib/systemd/system/docker.service
. You need to find the line starting withExecStart
and add the-g
option pointing to the new directory:ExecStart=/usr/bin/dockerd -g /mnt/newdrive/docker -H fd:// --containerd=/run/containerd/containerd.sock
Or, alternatively, create or modify the
/etc/docker/daemon.json
file with the new data-root configuration:{ "data-root": "/mnt/newdrive/docker" }
-
Reload the systemd daemon and restart Docker:
sudo systemctl daemon-reload sudo systemctl start docker
-
Verify that Docker is using the new directory:
docker info | grep "Docker Root Dir"
After successfully changing the Docker storage path and verifying that Docker is properly using the new directory, you might consider what to do with the data left in the original location (typically /var/lib/docker/
). Here are some considerations and steps you can take regarding the data in the original Docker directory:
Considerations Before Cleaning
-
Ensure Everything Works: Before you delete anything, ensure that your Docker containers and images are working correctly from the new location. Run some containers, pull new images, and ensure there are no errors related to storage.
-
Backup Important Data: If you haven’t already, make sure to back up important volumes or data from your Docker containers. This is crucial in case you need to revert or recover specific data.
-
Check for Active Data: Ensure that no services on your system are mistakenly still using the old Docker directory. You can check this by temporarily renaming the old directory and seeing if any operations fail.
Cleaning the Old Docker Data
Once you are confident that your Docker setup is stable and correctly configured with the new path, you can proceed to clean up the old data:
-
Stop Docker Daemon: To avoid any potential conflicts and ensure that no files are in use, stop the Docker daemon:
sudo systemctl stop docker
-
Remove Old Docker Directory: Now you can safely remove the old Docker directory. This will free up space on your system:
sudo rm -rf /var/lib/docker/
This command deletes all files and directories within the Docker default directory and is irreversible, so be absolutely sure that you’ve backed up everything you need before executing it.
-
Restart Docker Daemon: After the cleanup, you can start Docker again:
sudo systemctl start docker
-
Monitor Logs: Check the Docker logs to ensure there are no errors related to missing files or directories:
journalctl -u docker.service
Look for any critical errors that could indicate problems.
Additional Checks
-
Disk Space: Verify that the disk space has been reclaimed by checking the disk usage:
df -h
-
Docker Functionality: Test the Docker functionality one more time by running containers or pulling images, just to be certain everything is operating smoothly.