Douglas Su
Douglas Su

Reputation: 3364

Contents in the container's bind-mounted dir keep unchanged after mounting/umounting removable drive from the host

I want to dynamically change the contents in the container's directory according to the mounted removable USB disks. To fulfill this, I do the following steps.

  1. Run the container with -v option, which mount the host directory (/mnt) into container (/share). Assume the name of the new container is test. The command should look like docker run --name test -d -v /mnt:/share ubuntu:latest.

  2. Inspect the contents via docker exec -it test /usr/bin/bash. For now, the /share is empty.

  3. mount the USB disk to host. Execute mount /dev/sdxY /mntcommand. the /mnt directory on the host now contains files and directories which are stored on the removable USB disk.

  4. Inspect the contents in the containers again. The /share directory in the container is still empty. Nothing has been changed at all.

If I do this reversely: 1) first mount the USB disk to host, 2) run the container, 3) umount the USB disk. The contents in the container keep remained, but the /mnt directory on the host is swept.

Do docker has some mechanism to keep the contents synchronous across the container and the host after I mount/umount the disk.


docker info:

Containers: 2
 Running: 2
 Paused: 0
 Stopped: 0
Images: 1
Server Version: 17.03.1-ce
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 14
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc
runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe
init version: 949e6fa
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.8.0-46-generic
Operating System: Ubuntu 16.04.2 LTS
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.684 GiB
Name: tri-xps
ID: LMPY:EGYU:QUAF:DPUF:GZNR:AHFS:URFD:EFW3:5DFV:WHR3:NAYJ:PKQV
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

Upvotes: 1

Views: 809

Answers (2)

Douglas Su
Douglas Su

Reputation: 3364

Sorry for my late post. After creating an issue on docker's official github page. @cpuguy83 gave me the answer. https://github.com/moby/moby/issues/32512.

To make the the mount operations propagate to the container, append slave flag to -v options. e.g:

-v media/usb:/smb_share:slave

For more information, check HERE.

Upvotes: 2

Girdhar Sojitra
Girdhar Sojitra

Reputation: 678

You can use --device option for accessing usb device directly within container.

docker run -t -i --device=/dev/ttyUSB0 ubuntu bash

More documentation available at https://docs.docker.com/engine/reference/commandline/run/#add-host-device-to-container---device

Upvotes: 1

Related Questions