1

I need to mount host directory /data to 6 containers h1,h2,h3,h4,h5,h6

/data is an external hard disk mounted on the host. The 6 containers can be opened and closed easily.

The 6 containers will go into their own sub-directories of /data to analyze data independently and produce new data locally. All sub-directories have nothing to do with each other.

A relevant question is here, but no preferred answer is given.

How to do that? Below are the containers and images I have now.

$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9bd9334a1e7 ubuntu "/usr/bin/bash" 19 hours ago Up 18 hours h6 23679fe7252b ubuntu "/usr/bin/bash" 19 hours ago Up 18 hours h5 e2864e38e746 ubuntu "/usr/bin/bash" 19 hours ago Up 18 hours h4 c8996a304638 ubuntu "/usr/bin/bash" 19 hours ago Up 18 hours h3 9acd2a223d86 ubuntu "/usr/bin/bash" 19 hours ago Up 18 hours h2 5690b8c7b6da ubuntu "/usr/bin/bash" 2 days ago Up 12 hours h1

$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/hello-world latest f2a91732366c 2 months ago 1.85 kB docker.io/ubuntu 27 422dc563ca32 2 months ago 252 MB docker.io/ubuntu latest 422dc563ca32 2 months ago 252 MB

questionhang
  • 735
  • 2
  • 12
  • 31

1 Answers1

0

IF those containers are already running, you cannot easily add /data to them.
Except maybe with docker cp.

But the best practice remains either:

  • make images with /data already in it (Dockerfile ADD)
  • or use the existing image and launch your container, but with the -v (volume) option. See Use volumes.

https://docs.docker.com/engine/admin/volumes/images/types-of-mounts-volume.png

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I added a bold sentence in the post. – questionhang Jan 29 '18 at 06:30
  • It does not change my answer – VonC Jan 29 '18 at 06:33
  • The last method is the best choice for me. It seems I should use `docker run -t -i -v /home/test --name test itbilu/test /bin/bash` , but how to specify names of containers? – questionhang Jan 29 '18 at 07:02
  • 1
    @questionhang docker run is for launching a *new* container, not for modifying an existing running one. – VonC Jan 29 '18 at 07:04
  • @questionhang You can specify the name of the new container you are launching with -name: https://docs.docker.com/engine/reference/run/#container-identification – VonC Jan 29 '18 at 07:04
  • I do not understand `docker run -t -i -v /home/test --name test itbilu/test /bin/bash`. Whose name? What is the container that will be generated? What is itbilu/test? BTW, there is no easy way to add `data` to known containers, like `h1 and h2`? – questionhang Jan 29 '18 at 07:14
  • 1
    @questionhang That is what my all answer is saying: "there is no easy way to add data to known containers, like h1 and h2": you can try docker cp, but again, that is not the best practice – VonC Jan 29 '18 at 07:18
  • `docker run -t -i -v /home/test --name test itbilu/test /bin/bash` would run a container named `test`, based on the image `itbilu/test`, but overriding its `CMD` with a `/bin/bash` process: as long as that bash remains opened, the container will run. – VonC Jan 29 '18 at 07:19
  • That command will generate a directory with the same path in the new container, but the directory is empty. – questionhang Jan 29 '18 at 07:47
  • 1
    @questionhang yes, you need to specify the path on the host machine that you want to mount: https://docs.docker.com/engine/admin/volumes/bind-mounts/#choosing-the--v-or-mount-flag. `-v /data:/home/test` – VonC Jan 29 '18 at 07:51