How docker volume mounts differ
Please look at the following output.
docker run -it --rm ireshmm/jenkins:lts-jdk11 ls -la /var/jenkins_home
Output:
total 40
drwxr-xr-x 3 jenkins jenkins 4096 Jan 22 16:20 .
drwxr-xr-x 1 root root 4096 Jan 12 15:46 ..
-rw-r--r-- 1 jenkins jenkins 4683 Jan 22 16:20 copy_reference_file.log
drwxr-xr-x 2 jenkins jenkins 16384 Jan 22 16:20 plugins
-rw-rw-r-- 1 jenkins root 7152 Jan 12 15:42 tini_pub.gpg
When we mount a fresh new volume at /var/jenkins_home,
docker run -v random_volume:/var/jenkins_home -it --rm ireshmm/jenkins:lts-jdk11 ls -la /var/jenkins_home
Output:
total 40
drwxr-xr-x 3 jenkins jenkins 4096 Jan 22 16:24 .
drwxr-xr-x 1 root root 4096 Jan 12 15:46 ..
-rw-r--r-- 1 jenkins jenkins 4683 Jan 22 16:24 copy_reference_file.log
drwxr-xr-x 2 jenkins jenkins 16384 Jan 22 16:24 plugins
-rw-rw-r-- 1 jenkins root 7152 Jan 12 15:42 tini_pub.gpg
Question: How do the files still exist? Shouldn't they be gone?
When we mount a empty directory on host,
mkdir /tmp/empty_dir && chmod 777 /tmp/empty_dir
docker run -v /tmp/empty_dir:/var/jenkins_home -it --rm ireshmm/jenkins:lts-jdk11 ls -la /var/jenkins_home
Output:
total 32
drwxrwxrwx 3 1001 1001 4096 Jan 22 16:28 .
drwxr-xr-x 1 root root 4096 Jan 12 15:46 ..
-rw-r--r-- 1 jenkins jenkins 4683 Jan 22 16:28 copy_reference_file.log
drwxr-xr-x 2 jenkins jenkins 16384 Jan 22 16:28 plugins
Question: Why do the output differ from the previous output? What could be the reason?
Solution 1:
This is described in the documentation:
If you start a container which creates a new volume, as above, and the container has files or directories in the directory to be mounted (such as /app/ above), the directory’s contents are copied into the volume. The container then mounts and uses the volume, and other containers which use the volume also have access to the pre-populated content.
Bind mounts don't share this behavior.