What is the difference between container layer and volume in Docker? What is the difference between container layer and volume in Docker? docker docker

What is the difference between container layer and volume in Docker?


Volumes and image layers are separate concepts in Docker. To your first question, no, no volumes are created without -v being used (ignoring for the moment the fact that the Dockerfile format does have a VOLUME verb).

If you specify a volume that is provided by any volume driver (the default being a local directory which will bind-mount into the target location in your container filesystem), this volume is unrelated to the image layers, including the 'writable' topmost layer provided by the back-end storage driver in use in your Docker engine.

Specifically, the storage driver in use is what controls how the layer stack, and that top "r/w" layer is managed. For example, the overlay or devicemapper or btrfs driver handles the mount of the image layers and creating and managing the top layer.

Volumes come into play outside of this by being bind-mounted in to existing (or created) paths within the filesystem image. When you exit the container, these volumes are preserved in their source location (e.g. for a local/default volume driver, in /var/lib/docker/volumes/<name>) and the unmounted layer stack will have, under management of the storage driver in use, the modified "upper layer" of the layer stack, unless you had your container removed on exit (--rm). These are two separately managed concepts and the volume system has no interplay with the storage backend driver.


Volume is for mounting data between container and host

If I run docker container without any -v arguments will any volumes get created?

-v is not for creating the volume, just to mount the data from and to

-v [host_path]:[container_path],

here host_path, your server directory path where you want to mout the data to

container_path, container directory path mount date from

If I do specify a volume (which is not a mount to some location in host) does the volume become this container's writable layer?

When the path doesn't exist in the host. But you have mountaed it to container, there are two cases here.

CASE 1: If you want to mount some config files from your machine to container if the container expects that file while running then it throws error in the logs

CASE 2: If you want to mount the volume from container to host machine. When cotainer starts running it creates a folder in the host machine and writes the data to the host machine mounted directory.