Bobcares

Docker Overlay2: How to Setup and Use?

by | Sep 20, 2022

 Let us take a closer look at the docker overlay2 and the configuration necessary to set it up with the support of Docker hosting support services at Bobcares.

Use the OverlayFS Storage Driver

OverlayFS is a modern union filesystem similar to AUFS, but faster and simpler to create. Docker supports two OverlayFS storage drivers: the old overlay and the updated and more stable overlay2.

docker overlay2

Please keep in mind that in this article we will call the Linux kernel driver OverlayFS and the Docker storage driver overlay or overlay2.

Requirements For Docker Overlay2 

The OverlayFS is the most popular storage driver, and it will support if we meet the following conditions:

  • Linux kernel version 4.0 or higher, or RHEL or CentOS with kernel version 3.10.0-514 or higher. If we use an earlier kernel, we must utilize the overlay driver, which is not the best practice.
  • On xfs backing filesystems, the overlay and overlay2 drivers are supported, but only with d type=true enabled. Verify that the ftype option is set to 1 with xfs info. To properly format an xfs filesystem, use the parameter -n ftype=1.

    Docker now skips attempts to use the overlay or overlay2 driver when running on XFS without d-type support. Existing installations will continue to function but will generate an error.

    Users will be able to move their data as a result of this. This will be a fatal error in a future version, preventing Docker from the beginning.

  • When the storage driver is changed, existing containers and images on the local system become unreachable. Before replacing the storage driver, we can use docker saves to store any images we’ve created or push them to Docker Hub or a private registry so we don’t have to recreate them.

Configure Docker with the Overlay or Overlay2 storage driver

It is better to use the overlay2 driver rather than the overlay driver if possible. Docker EE does not support the overlay driver.

The Docker host must be running version 3.18 of the Linux kernel with the overlay kernel module loaded in order for Docker to use the overlay storage driver. The overlay2 driver requires a kernel version of 4.0 or higher.

We must first meet all of the conditions before proceeding with this method for docker overlay2 configuration.

The methods below describe how to set up the overlay2 storage driver. If the legacy overlay driver is necessary, mention it instead.

Stop Docker.

sudo systemctl stop docker

Make a backup of the contents of /var/lib/docker.

cp -au /var/lib/docker /var/lib/docker.bk

If we want to utilize a different backup filesystem than the one used by /var/lib/, we must format it and mount it in /var/lib/docker. To make this mount permanent, add it to /etc/fstab.

Change the file /etc/docker/daemon.json. If it is not present create it. Assuming the file was empty, enter the following information.

{
"storage-driver": "overlay2"
}

Docker will not start if the daemon.json file includes an invalid JSON.

Start Docker.

sudo systemctl start docker

Check that the daemon uses the overlay2 storage driver. Look for Storage Driver and Backing filesystem using the docker info command.

docker info

Docker is now using the overlay2 storage driver, and the overlay mount has been automatically constructed with the necessary lowerdir, upperdir, merged, and workdir constructions.

Continue reading for more information on how OverlayFS works within Docker containers, as well as performance tips and information on the constraints of its compatibility with various backup filesystems.

How the overlay2 driver works

OverlayFS combines two directories on a single Linux host and shows them as one. These directories refer to tiers, and the process of unification refers to union mount.

The lower directory is referred to as lowerdir, and the upper directory is referred to as upperdir. The unified view is accessible via its own directory called merged.

The overlay2 driver supports up to 128 lower OverlayFS layers natively. This feature improves the efficiency of layer-related Docker tasks like docker build and docker commit while using fewer inodes on the supporting filesystem.

Image and container layers on-disk

We may see six directories under /var/lib/docker/overlay2 after downloading a five-layer image using docker pull ubuntu.

Caution: Do not directly modify any of the files or directories in /var/lib/docker/. Docker manages these files and folders.

ls -l /var/lib/docker/overlay2

As symbolic links, the new l (lowercase L) directory contains abbreviated layer identifiers. These identifiers can avoid exceeding the page size restriction on mount command arguments.

ls -l /var/lib/docker/overlay2/l
../3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b/diff
../4e9fa83caff3e8f4cc83693fa407a4a9fac9573deaf481506c102d484dd1e6a1/diff
../eca1e4e1694283e001f200a667bb3cb40853cf2d1b12c29feda7422fed78afed/diff
../223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7/diff
../e8876a226237217ec61c4baf238a32992291d059fdac95ed6303bdff3f59cff5/diff

The lowest layer includes a file called a link that carries the name of the shortened identifier and a directory named diff that contains the contents of the layer.

ls /var/lib/docker/overlay2/3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b/
cat /var/lib/docker overlay2/3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b/link
ls /var/lib/docker/overlay2/3a36935c9df35472229c57f4a27105a136f5e4dbef0f87905b2e506e494e348b/diff

The second-lowest layer, as well as each higher layer, contains a file named lower that represents its parent, as well as a directory called diff that holds its contents.

It also has a merged directory that holds the unified contents of its parent layer and itself, as well as a working directory that OverlayFS uses internally.

ls /var/lib/docker/overlay2/223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7
cat /var/lib/docker/overlay2/223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7/lower
ls /var/lib/docker/overlay2/223c2864175491657d238e2664251df13b63adb8d050924fd1bfcdb278b866f7/diff/

Utilize the mount command to see the mounts that exist when we use the overlay storage driver with Docker.

mount | grep overlay

The second line’s rw indicates that the overlay mount is read-write.

[Need assistance with similar queries? We are here to help]

Conclusion

To conclude we have now learned more about the docker overlay2 configuration and the various steps necessary with the support of our Docker hosting support services.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

2 Comments

  1. Mazhar Haq

    I have overlay2 driver running in docker. I have a postgress container running. Any data stored in postgress database is lost after a reboot or power off and on! How do I prevent data loss and data persistent.

    Reply
    • Hiba Razak

      Hi,
      Please contact our support through live chat(click on the icon at right-bottom).

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.