Mounting multiple volumes on a docker container?

Docker

Docker Problem Overview


I know I can mount a directory in my host on my container using something like

docker run -t -i -v '/on/my/host:/on/the/container' ubuntu /bin/bash

Is there a way to create more than one host-container pair? e.g. a comma-separated list, or pass in an array?

Docker Solutions


Solution 1 - Docker

Pass multiple -v arguments.

For instance:

docker -v /on/my/host/1:/on/the/container/1 \
       -v /on/my/host/2:/on/the/container/2 \
       ...

Solution 2 - Docker

Docker now recommends migrating towards using --mount.

Multiple volume mounts are also explained in detail in the current Docker documentation.

From: https://docs.docker.com/storage/bind-mounts/

$ docker run -d \
  -it \
  --name devtest \
  --mount type=bind,source="$(pwd)"/target,target=/app \
  --mount type=bind,source="$(pwd)"/target,target=/app2,readonly,bind-propagation=rslave \
  nginx:latest

Original older answer should still work; just trying to keep the answer aligned to current best known method.

Solution 3 - Docker

You can use -v option multiple times in docker run command to mount multiple directory in container:

docker run -t -i \
  -v '/on/my/host/test1:/on/the/container/test1' \
  -v '/on/my/host/test2:/on/the/container/test2' \
  ubuntu /bin/bash

Solution 4 - Docker

You can have Read only or Read and Write only on the volume

docker -v /on/my/host/1:/on/the/container/1:ro \

docker -v /on/my/host/2:/on/the/container/2:rw \

Solution 5 - Docker

On Windows: if you had to mount two directories E:\data\dev & E:\data\dev2

Use:

docker run -v E:\data\dev:c:/downloads -v E:\data\dev2 c:/downloads2 -i --publish 1111:80 -P SomeBuiltContainerName:SomeLabel

Solution 6 - Docker

Or you can do

docker run -v /var/volume1 -v /var/volume2 DATA busybox true

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSeldoView Question on Stackoverflow
Solution 1 - DockerCharles DuffyView Answer on Stackoverflow
Solution 2 - DockerMadJangiView Answer on Stackoverflow
Solution 3 - DockerGaurav SehgalView Answer on Stackoverflow
Solution 4 - Dockersenthil sivasamyView Answer on Stackoverflow
Solution 5 - DockerLawrence PatrickView Answer on Stackoverflow
Solution 6 - DockerJoost van der LaanView Answer on Stackoverflow