How to Change Docker Storage Location
@ rushi | Wednesday, Nov 11, 2020 | 3 minutes read | Update at Wednesday, Nov 11, 2020

You can easily change the Docker default storage location by creating the daemon.json file and pointing to another location in that file.

It happened to me several times that I didn’t have enough space in my root partition to store Docker containers and I had to move the Docker default storage location to another partition. In this post, I wrote down how to do that for my readership and future myself :)

Docker containers are relatively large (> 1G) and by default Docker stores all containers in /var/lib/docker, which is located in the root partition of your Linux system. I usually have separate root and home partitions, and given that Linux doesn’t take much space, I allocate 15-30G for my root partition. This happened not to be enough to work with Docker and I had to move the Docker storage location to another larger partition. However, it turned out not to be easy.

Do NOT do this to move Docker storage location

These two solutions could have worked in the past as you may often find them online, but neither of them worked for me with Ubuntu-based Linux distros in 2018-2019 (Docker version > 17).

The first obvious idea was to symlink the default storage location to another partition:

sudo ln -s /mnt/newlocation /var/lib/docker

2. DOCKER_OPTS

Another often posted solution is to stop Docker:

sudo systemctl stop docker

Edit the /etc/default/docker file by adding the new location with the -g in the DOCKER_OPTS line:

DOCKER_OPTS="-dns 8.8.8.8 -dns 8.8.4.4 -g /mnt/newlocation"

Then start Docker again:

sudo systemctl start docker

After that Docker should use /mnt/newlocation as a new storage location.

UPDATE: It seems DOCKER_OPTS solution may work if you add the $DOCKER_OPTS variable to the systemd script /lib/systemd/system/docker.service:

ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS

However, I have not tested it because the solution I describe below is simpler and probably more correct.

Change Docker storage location: THE RIGHT WAY!

Luckily, the right way to change Docker storage location was not more complicated than the two non-working options I have described above.

You need to create a JSON file /etc/docker/daemon.json with the content pointing to the new storage location:

{
"data-root": "/mnt/newlocation"
}

You can read more about daemon.json in Docker docs.

Then, restart Docker or reboot the system:

sudo systemctl restart docker

If you get any error during the restart, pay attention to spaces in daemon.json. JSON files are sensitive to indentation and an extra or lacking space may cause an error. If Docker restarts fine, this new setting will make Docker place all new containers to the new location. However, old containers will stay in /etc/default/docker. I recommend removing all old containers:

And downloaded them again:

docker pull <container-name>

Final thoughts

It is unfortunate that this simple solution with daemon.json was not the first I found when I tried to fix the “not enough space” issue due to Docker images taking too much space. So, I hope this blog post will save time some users who need to change their Docker storage location.

Source https://evodify.com/change-docker-storage-location/

关于我

g1eny0ung 的 ❤️ 博客

记录一些 🌈 生活上,技术上的事

一名大四学生

马上(已经)毕业于 🏫 大连东软信息学院

职业是前端工程师

业余时间会做开源和 Apple App (OSX & iOS)

主要的技术栈是:

  • JavaScript & TypeScript
  • React.js
  • Electron
  • Rust

写着玩(写过):

  • Java & Clojure & CLJS
  • OCaml & Reason & ReScript
  • Dart & Swift

目前在 PingCAP 工作

– 2020 年 09 月 09 日更新

其他

如果你喜欢我的开源项目或者它们可以给你带来帮助,可以赏一杯咖啡 ☕ 给我。~

If you like my open source projects or they can help you. You can buy me a coffee ☕.~

PayPal

https://paypal.me/g1eny0ung

Patreon:

Become a Patron!

微信赞赏码

wechat

最好附加一下信息或者留言,方便我可以将捐助记录 📝 下来,十分感谢 🙏。

It is better to attach some information or leave a message so that I can record the donation 📝, thank you very much 🙏.