DEV Community


Posted on

Move docker folder to external storage

Quick note to me and the world:

Docker by design stores everything (except some certs and the daemon config) in one folder. By default, that's '/var/lib/docker'.

That folder can grow quite big over time. Yes, one could do some housekeeping. But especially when using Docker Volumes for persistent data, this folder quickly grows. Aside from symlinks or mount binds, it is also possible to directly tell the Docker Daemon where to store all the files.

For instance, I have the folder /mnt/srv mounted to an external disk /dev/sdb1.

To make Docker use, e.g. /mnt/srv/docker, I put the following in /etc/docker/daemon.json. If the file does not exist, simply create it!

  "data-root": "/mnt/srv/docker"
Enter fullscreen mode Exit fullscreen mode

You do not have to create the docker folder. If /mnt/srv/docker does not exist, Docker will create it. But normally, you do not want to have a blank start, so you could simply mv /var/lib/docker /mnt/srv/.

Make sure you first stop the Docker Dameon/Service. This can be done by executing sudo systemctl stop docker.socket.

When done, simply start the service: sudo systemctl start docker.service.

The first command makes sure docker really shuts down. If you only stop the service, the socket will still be active! On the other hand, if you start the service, the socket will be activated, too.


If you mount a disk partition, like I did, it might happen that some containers fail. For instance, I tried to run sonatype/nexus3 and it always failed. Here's an excerpt from the logs:

Caused by: java.lang.UnsatisfiedLinkError: /nexus-data/tmp/jna4777440640643979514.tmp: /nexus-data/tmp/jna4777440640643979514.tmp: failed to map segment from shared object
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(
        at java.lang.ClassLoader.loadLibrary(
        at java.lang.Runtime.load0(
        at java.lang.System.load(
        at com.sun.jna.Native.loadNativeDispatchLibraryFromClasspath(
        at com.sun.jna.Native.loadNativeDispatchLibrary(
        at com.sun.jna.Native.<clinit>(
        at com.orientechnologies.common.jna.ONative.instance(
        at com.orientechnologies.orient.core.engine.OMemoryAndLocalPaginatedEnginesInitializer.configureDefaultDiskCacheSize(
        at com.orientechnologies.orient.core.engine.OMemoryAndLocalPaginatedEnginesInitializer.configureDefaults(
        at com.orientechnologies.orient.core.engine.OMemoryAndLocalPaginatedEnginesInitializer.initialize(
        at com.orientechnologies.orient.core.engine.local.OEngineLocalPaginated.startup(
        at com.orientechnologies.orient.core.Orient.startEngine(
        at com.orientechnologies.orient.core.Orient.loadStorage(
        at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.<init>(
        at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.<init>(
        at com.orientechnologies.orient.server.OSystemDatabase.init(
        at com.orientechnologies.orient.server.OSystemDatabase.<init>(
        at com.orientechnologies.orient.server.OServer.initSystemDatabase(
        at com.orientechnologies.orient.server.OServer.activate(
        ... 7 common frames omitted

Enter fullscreen mode Exit fullscreen mode

The reason was, I had the following in my /etc/fstab:

UUID=a3d2ba5a-dbbd-40e9-b6d7-a4349987e6f8   /mnt/srv        ext4    defaults,nofail,user  0       2
Enter fullscreen mode Exit fullscreen mode

The user argument implies the options noexec, nosuid, and nodev. With one or more of these implied options in place, I get the above error. Removing the user argument from my mount resolved it!

Top comments (0)