263

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?

2
  • as I understand current state of docs terms, you cannot mount a volume (title of question), you certainly can have many bind mounts to host folders (that is what is asked for in body of question) Commented Dec 2, 2019 at 14:56
  • do 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. Commented Jun 24, 2022 at 17:19

8 Answers 8

400

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 \ ... 
Sign up to request clarification or add additional context in comments.

8 Comments

Okay, so i'm doing this the exact same way but when i try calling the second one it says that it isn't found.
docker run -v /home/usr/workspace/proj/WebContent/file/username:/mycode -v /home/usr/workspace/proj/WebContent/file:/tst gcc:4.9 sh -c 'cd mycode; gcc -o myapp ./mycode.c; cd tst; ./myapp < ./test.txt' This is my command, I'm trying to compile the mycode.c that is in the first volume, but give that same file an stdin from a different volume. How do I do it?
Does it always need absolute paths?
The destination must always be absolute. The source needs to be either an absolute path or the identifier for a named volume -- see docs.docker.com/engine/admin/volumes/volumes for docs on the latter.
this is working only for the second volume i am mapping... how can i map two volumes in the same run command assuming I can't use the docker file only run command
|
69

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.

2 Comments

It's worth adding the only reason Docker recommend migrating is because their research shows --mount is easier to use and it has more options. Using either -v or --mount is perfectly fine, this is down to your personal preference and needs.
what wrong with docker run -v /Users/brandomiranda/iit-term-synthesis:/home/bot/iit-term-synthesis \ -v /Users/brandomiranda/pycoq:/home/bot/pycoq \ -v /Users/brandomiranda/ultimate-utils:/home/bot/ultimate-utils \ -ti brandojazz/iit-term-synthesis:test bash?
35

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 

2 Comments

Does this add anything to the preceding answer to the same effect?
love this answer! With a concrete thing that is essentially runable and a useful interactive demo.
11

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 \ 

2 Comments

which one is the default option?
@HammadDar Pretty sure 'rw' is the default for volumes. You could try testing it out locally without specifying ':rw' or ':ro' at the end.
4

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 

Comments

1

I saw a comment asking if read-write or read-only was the default option; read-write is the default option. (making a post because I haven't enough rep to comment)

Per docker documentation, running the following:

docker run -d \ --name devtest \ --mount source=myvol2,target=/app \ nginx:latest 

and then using docker inspect devtest and locating the "RW" option in the "Mounts" section of the output:

"Mounts": [ { "Type": "volume", "Source": "/var/lib/docker/volumes/myvol2/_data", "Destination": "/app", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], 

lets us see that the default option lets the volume be both read- and write-able.


To set your volume as read-only (again per official documentation), add readonly after your source and destination tags:

docker run -d \ --name=nginxtest \ --mount source=nginx-vol,destination=/usr/share/nginx/html,readonly \ nginx:latest 

Run docker inspect nginxtest and locate "Mounts":

"Mounts": [ { "Type": "volume", "Source": "/var/lib/docker/volumes/nginx-vol/_data", "Destination": "/usr/share/nginx/html", "Driver": "local", "Mode": "", "RW": false, "Propagation": "" } ], 

(Note: I don't know why official documentation swaps between target and destination tags, but I am working under the assumption they can be used interchangeably.)

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
1

For a runable example:

docker run -v /Users/brandomiranda/iit-term-synthesis:/home/bot/iit-term-synthesis \ -v /Users/brandomiranda/pycoq:/home/bot/pycoq \ -v /Users/brandomiranda/ultimate-utils:/home/bot/ultimate-utils \ -ti brandojazz/iit-term-synthesis:test bash 

but first do:

docker pull brandojazz/iit-term-synthesis:test 

Comments

-12

Or you can do

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

2 Comments

This is not what the OP is after. This creates a volume within the container and doesn't bind it on the host.
You're right, should be: docker run -v /var/volume1:/container/volume1 -v /var/volume2:/container/volume2 DATA busybox true

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.