1

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
Douglas Su
  • 3,214
  • 7
  • 29
  • 58

2 Answers2

1

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

Girdhar Sojitra
  • 648
  • 4
  • 14
  • I tried to used `--device` option but failed. Before plugging the USB cable into host computer, there has no proper device file in `/dev`, e.g `/dev/sdb`. If I first connect USB cable, then running container with `--device=/dev/sdb1`, the device is still there event after I disconnect the USB cable from host. – Douglas Su Apr 17 '17 at 04:17
  • This post will help to you http://stackoverflow.com/questions/24225647/docker-any-way-to-give-access-to-host-usb-or-serial-device – Girdhar Sojitra Apr 17 '17 at 05:16
  • I tested with `docker run -it --rm --privileged -v /dev/bus/usb:/dev/bus/usb ubuntu bash`, the usb flash driver can't be recognized if I plugged it before container runs. – Douglas Su Apr 17 '17 at 12:39
1

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.

Douglas Su
  • 3,214
  • 7
  • 29
  • 58