How to Changing the Docker Storage Path in Ubuntu

Docker Disk Space Usage

Docker can potentially use a significant amount of disk space. This usage primarily comes from the following:

  1. 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.
  2. 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.
  3. 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.
  4. 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:

  1. Stop the Docker daemon:

    sudo systemctl stop docker
    
  2. 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
    
  3. 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 with ExecStart 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"
    }
    
  4. Reload the systemd daemon and restart Docker:

    sudo systemctl daemon-reload
    sudo systemctl start docker
    
  5. 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

  1. 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.

  2. 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.

  3. 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:

  1. Stop Docker Daemon: To avoid any potential conflicts and ensure that no files are in use, stop the Docker daemon:

    sudo systemctl stop docker
    
  2. 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.

  3. Restart Docker Daemon: After the cleanup, you can start Docker again:

    sudo systemctl start docker
    
  4. 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.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值