Skip to content

Instantly share code, notes, and snippets.

@yoctozepto
Created January 20, 2022 12:47
Show Gist options
  • Save yoctozepto/e6fdc2789297fdfdff4fd45fe64c9cb9 to your computer and use it in GitHub Desktop.
Save yoctozepto/e6fdc2789297fdfdff4fd45fe64c9cb9 to your computer and use it in GitHub Desktop.
Docker shared bind mounts bug reproducer
#!/bin/bash
# possible Docker bug when doing nested shared bind mounts
# happens since at least Docker 17.10 (could be earlier)
# currently tested with Docker 20.10.5
# see also https://bugs.launchpad.net/kolla-ansible/+bug/1783978
# the formula below is built this way: (origin mounts)+(submounts)=(total)
# the behaviour described below is observed both on container recreation
# (as given here) and container restart
# in certain cases mounts are being preserved and happen to grow exponentially
set -x
mkdir -p $HOME/bombastic-host
mkdir -p $HOME/bombastic-host-sub
# (1) - bind in shared-bind - BAD behaviour (possibly expected)
docker run --rm \
-v $HOME/bombastic-host:/bombastic-container:shared \
-v $HOME/bombastic-host-sub:/bombastic-container/sub \
busybox /bin/true
mount | grep bombastic | sort | cut -f 1-3 -d' '
# after 1: 0+1=1
# /dev/sdb on /home/radek/bombastic-host/sub
docker run --rm \
-v $HOME/bombastic-host:/bombastic-container:shared \
-v $HOME/bombastic-host-sub:/bombastic-container/sub \
busybox /bin/true
mount | grep bombastic | sort | cut -f 1-3 -d' '
# after 2: 1+2=3
# /dev/sdb on /home/radek/bombastic-host-sub
# /dev/sdb on /home/radek/bombastic-host/sub
# /dev/sdb on /home/radek/bombastic-host/sub
docker run --rm \
-v $HOME/bombastic-host:/bombastic-container:shared \
-v $HOME/bombastic-host-sub:/bombastic-container/sub \
busybox /bin/true
mount | grep bombastic | sort | cut -f 1-3 -d' '
# after 3: 3+4=7
# /dev/sdb on /home/radek/bombastic-host-sub
# /dev/sdb on /home/radek/bombastic-host-sub
# /dev/sdb on /home/radek/bombastic-host-sub
# /dev/sdb on /home/radek/bombastic-host/sub
# /dev/sdb on /home/radek/bombastic-host/sub
# /dev/sdb on /home/radek/bombastic-host/sub
# /dev/sdb on /home/radek/bombastic-host/sub
# and on it goes...
# cleanup (must be in this order)
sudo umount $HOME/bombastic-host-sub
sudo umount $HOME/bombastic-host/sub
# (2) - shared-bind in bind - OK
docker run --rm \
-v $HOME/bombastic-host:/bombastic-container \
-v $HOME/bombastic-host-sub:/bombastic-container/sub:shared \
busybox /bin/true
mount | grep bombastic | sort | cut -f 1-3 -d' '
# (3) - volume in shared-bind - BAD (analogously to 1)
docker run --rm \
-v bombastic-sub-volume:/bombastic-container/sub \
-v $HOME/bombastic-host:/bombastic-container:shared \
busybox /bin/true
mount | grep bombastic | sort | cut -f 1-3 -d' '
# after 1: 0+1=1
# /dev/sdb on /home/radek/bombastic-host/sub
docker run --rm \
-v bombastic-sub-volume:/bombastic-container/sub \
-v $HOME/bombastic-host:/bombastic-container:shared \
busybox /bin/true
mount | grep bombastic | sort | cut -f 1-3 -d' '
# after 2: 1+2=3
# /dev/sdb on /home/radek/bombastic-host/sub
# /dev/sdb on /home/radek/bombastic-host/sub
# /dev/sdb on /var/lib/docker/volumes/bombastic-sub-volume/_data
docker run --rm \
-v bombastic-sub-volume:/bombastic-container/sub \
-v $HOME/bombastic-host:/bombastic-container:shared \
busybox /bin/true
mount | grep bombastic | sort | cut -f 1-3 -d' '
# after 3: 3+4=7
# /dev/sdb on /home/radek/bombastic-host/sub
# /dev/sdb on /home/radek/bombastic-host/sub
# /dev/sdb on /home/radek/bombastic-host/sub
# /dev/sdb on /home/radek/bombastic-host/sub
# /dev/sdb on /var/lib/docker/volumes/bombastic-sub-volume/_data
# /dev/sdb on /var/lib/docker/volumes/bombastic-sub-volume/_data
# /dev/sdb on /var/lib/docker/volumes/bombastic-sub-volume/_data
# and on it goes...
# cleanup (must be in this order)
sudo umount /var/lib/docker/volumes/bombastic-sub-volume/_data
sudo umount /home/radek/bombastic-host/sub
# (4) - shared-bind in volume - BAD (unlike analogous 2, I expect this case to be OK)
# could be related to https://github.com/moby/moby/issues/35323
docker run --rm \
-v bombastic-volume:/bombastic-container \
-v $HOME/bombastic-host-sub:/bombastic-container/sub:shared \
busybox /bin/true
mount | grep bombastic | sort | cut -f 1-3 -d' '
# after 1: 0+1=1
# /dev/sdb on /var/lib/docker/volumes/bombastic-volume/_data/sub
docker run --rm \
-v bombastic-volume:/bombastic-container \
-v $HOME/bombastic-host-sub:/bombastic-container/sub:shared \
busybox /bin/true
mount | grep bombastic | sort | cut -f 1-3 -d' '
# after 2: 1+2=3
# /dev/sdb on /home/radek/bombastic-host-sub
# /dev/sdb on /var/lib/docker/volumes/bombastic-volume/_data/sub
# /dev/sdb on /var/lib/docker/volumes/bombastic-volume/_data/sub
docker run --rm \
-v bombastic-volume:/bombastic-container \
-v $HOME/bombastic-host-sub:/bombastic-container/sub:shared \
busybox /bin/true
mount | grep bombastic | sort | cut -f 1-3 -d' '
# after 3: 3+4=7
# /dev/sdb on /home/radek/bombastic-host-sub
# /dev/sdb on /home/radek/bombastic-host-sub
# /dev/sdb on /home/radek/bombastic-host-sub
# /dev/sdb on /var/lib/docker/volumes/bombastic-volume/_data/sub
# /dev/sdb on /var/lib/docker/volumes/bombastic-volume/_data/sub
# /dev/sdb on /var/lib/docker/volumes/bombastic-volume/_data/sub
# /dev/sdb on /var/lib/docker/volumes/bombastic-volume/_data/sub
# and on it goes...
# cleanup (must be in this order)
sudo umount $HOME/bombastic-host-sub
sudo umount /var/lib/docker/volumes/bombastic-volume/_data/sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment