912

I am trying to mount a host directory into a Docker container so that any updates done on the host is reflected into the Docker containers.

Where am I doing something wrong. Here is what I did:

kishore$ cat Dockerfile

FROM ubuntu:trusty
RUN apt-get update
RUN apt-get -y install git curl vim
CMD ["/bin/bash"]
WORKDIR /test_container
VOLUME ["/test_container"]

kishore$ tree
.
├── Dockerfile
└── main_folder
    ├── tfile1.txt
    ├── tfile2.txt
    ├── tfile3.txt
    └── tfile4.txt

1 directory, 5 files kishore$ pwd /Users/kishore/tdock

kishore$ docker build --tag=k3_s3:latest .

Uploading context 7.168 kB
Uploading context
Step 0 : FROM ubuntu:trusty
 ---> 99ec81b80c55
Step 1 : RUN apt-get update
 ---> Using cache
 ---> 1c7282005040
Step 2 : RUN apt-get -y install git curl vim
 ---> Using cache
 ---> aed48634e300
Step 3 : CMD ["/bin/bash"]
 ---> Running in d081b576878d
 ---> 65db8df48595
Step 4 : WORKDIR /test_container
 ---> Running in 5b8d2ccd719d
 ---> 250369b30e1f
Step 5 : VOLUME ["/test_container"]
 ---> Running in 72ca332d9809
 ---> 163deb2b1bc5
Successfully built 163deb2b1bc5
Removing intermediate container b8bfcb071441
Removing intermediate container d081b576878d
Removing intermediate container 5b8d2ccd719d
Removing intermediate container 72ca332d9809

kishore$ docker run -d -v /Users/kishore/main_folder:/test_container k3_s3:latest c9f9a7e09c54ee1c2cc966f15c963b4af320b5203b8c46689033c1ab8872a0ea

kishore$ docker run -i -t k3_s3:latest /bin/bash

root@0f17e2313a46:/test_container# ls -al
total 8
drwx------  2 root root 4096 Apr 29 05:15 .
drwxr-xr-x 66 root root 4096 Apr 29 05:15 ..

root@0f17e2313a46:/test_container# exit exit

kishore$ docker -v
Docker version 0.9.1, build 867b2a9

  • I don't know how to check boot2docker version

Questions, issues facing:

  1. How do I need to link the main_folder to the test_container folder present inside the docker container?
  2. I need to make this automatically. How do I to do that without really using the run -d -v command?
  3. What happens if the boot2docker crashes? Where are the Docker files stored (apart from Dockerfile)?
3
  • 2
    FYI, this seems to be fixed in docker as of this comment. I'm using boot2docker on my mac. I can use the -v option and my local directory gets mounted all the way into the container.
    – Marco
    Dec 3, 2014 at 8:14
  • 1
    None of the answers below meet the OP's requirement #2. They want to do it from Dockerfile, not the docker run command. I'm also wondering. Sep 28, 2020 at 22:58
  • Please don't tag your images latest! medium.com/@mccode/…
    – mteam88
    May 9, 2022 at 13:43

27 Answers 27

711

There are a couple ways you can do this. The simplest way to do so is to use the dockerfile ADD command like so:

ADD . /path/inside/docker/container

However, any changes made to this directory on the host after building the dockerfile will not show up in the container. This is because when building a container, docker compresses the directory into a .tar and uploads that context into the container permanently.

The second way to do this is the way you attempted, which is to mount a volume. Due to trying to be as portable as possible you cannot map a host directory to a docker container directory within a dockerfile, because the host directory can change depending on which machine you are running on. To map a host directory to a docker container directory you need to use the -v flag when using docker run, e.g.,:

# Run a container using the `alpine` image, mount the `/tmp`
# directory from your host into the `/container/directory`
# directory in your container, and run the `ls` command to
# show the contents of that directory.
docker run \
    -v /tmp:/container/directory \
    alpine \
    ls /container/directory
9
  • I tried like this kishore$ docker -v /Users/kishore/main_folder:/test_container Docker version 0.9.1, build 867b2a9 kishore$ docker run -v /Users/kishore/main_folder:/test_container Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container -P, --publish-all=false: Publish all exposed ports to the host interfaces -a, --attach=[]: Attach to stdin, stdout or stderr. -c, --cpu-shares=0: CPU shares (relative weight)
    – Kishore
    May 4, 2014 at 16:40
  • 3
    can you have both ADD . and -v volume mount at same time. using default app_dir if not specify a host mount?
    – alexzg
    May 5, 2014 at 5:02
  • @alexzg I don't think we can have only the first one be taken precedence. Also, I am not clear about your 2nd point default app_dir... host mount.. ?
    – Kishore
    May 5, 2014 at 6:29
  • 52
    This worked for me on mac os x with docker-machine and VirtualBox. One caveat I discovered by trial and error is that the host directory must be a full path and not a relative path. Feb 9, 2017 at 22:23
  • 78
    ADD is actually not "mounting" it copies the file into the container persisting it inside.
    – kev
    Nov 3, 2017 at 5:13
280

The user of this question was using Docker version 0.9.1, build 867b2a9, I will give you an answer for docker version >= 17.06.

What you want, keep local directory synchronized within container directory, is accomplished by mounting the volume with type bind. This will bind the source (your system) and the target (at the docker container) directories. It's almost the same as mounting a directory on linux.

According to Docker documentation, the appropriate command to mount is now mount instead of -v. Here's its documentation:

  • --mount: Consists of multiple key-value pairs, separated by commas. Each key/value pair takes the form of a <key>=<value> tuple. The --mount syntax is more verbose than -v or --volume, but the order of the keys is not significant, and the value of the flag is easier to understand.

  • The type of the mount, which can be bind, volume, or tmpfs. (We are going to use bind)

  • The source of the mount. For bind mounts, this is the path to the file or directory on the Docker daemon host. May be specified as source or src.

  • The destination takes as its value the path where the file or directory will be mounted in the container. May be specified as destination, dst, or target.

So, to mount the the current directory (source) with /test_container (target) we are going to use:

    docker run -it --mount src="$(pwd)",target=/test_container,type=bind k3_s3

If these mount parameters have spaces you must put quotes around them. When I know they don't, I would use `pwd` instead:

    docker run -it --mount src=`pwd`,target=/test_container,type=bind k3_s3

You will also have to deal with file permission, see this article.

6
  • 2
    I've upvoted your answer because I love how concise it is and it isn't wrong, but you may want to update it a bit in light of my answer at stackoverflow.com/a/51829247/901899
    – rainabba
    Aug 13, 2018 at 19:25
  • 3
    I guess the documentation has been updated since this answer, as right now it seems to recommend volumes, rather than mounts. Worth going though it, before committing to anything.
    – Sipty
    Aug 13, 2019 at 11:12
  • 1
    @Sipty may be referring to the following: "If you are developing new Docker applications, consider using named volumes instead. You can’t use Docker CLI commands to directly manage bind mounts."
    – Aluthren
    Nov 10, 2019 at 23:20
  • 2
    Docker still recommends bind mounts for development use cases here: docs.docker.com/develop/dev-best-practices "One case where it is appropriate to use bind mounts is during development, when you may want to mount your source directory or a binary you just built into your container. For production, use a volume instead, mounting it into the same location as you mounted a bind mount during development."
    – kjones
    Apr 20, 2021 at 15:49
  • this works very well but how can mount on existing container Oct 12, 2021 at 13:16
145

You can use -v option from cli, this facility is not available via Dockerfile

docker run -t -i -v <host_dir>:<container_dir>  ubuntu /bin/bash

where host_dir is the directory from host which you want to mount. You don't need to worry about directory of container if it doesn't exist docker will create it.

If you do any changes in host_dir from host machine (under root privilege) it will be visible to container and vice versa.

2
  • 13
    What a nice, clean, to-the-point example to illustrate the host mount option. It could perhaps be improved by using real directory: docker run -t -i -v /tmp:/tmp ubuntu /bin/bash where host /tmp will be mounted on container /tmp. Now one can touch /tmp/hello-world from the container and see the file appear on the host. Jan 30, 2017 at 21:29
  • 8
    Works perfect. But mounting this way sets the directory owner as root and this also expects the container to be run as root. How do we mount the dir as non-root when the container is run as non-root?
    – Arun
    Jul 11, 2018 at 3:57
90

2 successive mounts: I guess many posts here might be using two boot2docker, the reason you don't see anything is that you are mounting a directory from boot2docker, not from your host.

You basically need 2 successive mounts:

the first one to mount a directory from your host to your system

the second to mount the new directory from boot2docker to your container like this:

  • 1) Mount local system on boot2docker

    sudo mount -t vboxsf hostfolder /boot2dockerfolder
    
  • 2) Mount boot2docker file on linux container

    docker run -v /boot2dockerfolder:/root/containerfolder -i -t imagename
    

Then when you ls inside the containerfolder you will see the content of your hostfolder.

5
  • 1
    Best answer. I have to add that (under windows) hostfolder in the mount command is refering to a folder share name. The one that appears with a net share command inside a windows terminal (cmd). And you have to add that one as a shared folder with the virtualBox GUI (using the same name if you follow my logic). If you omit to share it, it will work, but you'll face issues with permissions and refresh performances.
    – Kir Kanos
    Apr 15, 2015 at 14:47
  • 1
    how does mount command know the host where /boot2dockerfolder is ?
    – alfonsodev
    Jul 15, 2015 at 16:50
  • I got the error: mount: unknown filesystem type 'vboxsf', using an ubuntu guest and host
    – nnyby
    Aug 26, 2015 at 18:50
  • "vboxsf" is the VirtualBox shared-folder "file system" driver. It must be configured into the VM by installing the VirtualBox extensions. The boot2docker image must not have these installed. VirtualBox has a "device" function to "insert" the VirtualBox Additions ISO images as a virtual volume. Typically you can mount this on mount by using mount /dev/cdrom /mnt, going into /mnt and running bash VBoxLinuxAdditions. It may need installation of gcc, kernel, or other dependencies. Sep 24, 2015 at 17:07
  • mount.vboxsf: mounting failed with the error: No such file or directory mount: mounting mymapfoldername on /boot2dockerfolder failed: No such file or directory
    – vee
    Apr 22, 2016 at 15:59
37

For those who wants to mount a folder in current directory:

docker run -d --name some-container -v ${PWD}/folder:/var/folder ubuntu
1
  • 1
    Exactly what I needed for Docker version 20.10.14 and nothing more. Thanks! May 11, 2022 at 4:35
34

Is it possible that you use docker on OS X via boot2docker or something similar.

I've made the same experience - the command is correct but nothing (sensible) is mounted in the container, anyway.

As it turns out - it's already explained in the docker documentation. When you type docker run -v /var/logs/on/host:/var/logs/in/container ... then /var/logs/on/host is actually mapped from the boot2docker VM-image, not your Mac.

You'll have to pipe the shared folder through your VM to your actual host (the Mac in my case).

5
  • 1
    Thanks! This helped me with a distantly related problem. Postgres running inside Docker had bound to the VBox IP, not the localhost, as it would have if not running on OS X. Aug 26, 2014 at 4:56
  • 2
    That URL to the docker documentation is no longer valid Dec 29, 2015 at 2:48
  • haha the best joke ever.. they really could have made a hint or so. Apr 4, 2016 at 15:56
  • 3
    Your documentation link is a 404 May 25, 2016 at 15:00
  • 3
    I'm curious to see an example of piping the shared folder through the VM to the actual host.
    – Derek
    Jun 22, 2016 at 18:39
30

I'm just experimenting with getting my SailsJS app running inside a Docker container to keep my physical machine clean.

I'm using the following command to mount my SailsJS/NodeJS application under /app:

cd my_source_code_folder
docker run -it -p 1337:1337 -v $(pwd):/app my_docker/image_with_nodejs_etc
1
  • 8
    How can do this in the Dockerfile?
    – orad
    Jun 20, 2018 at 22:51
28

[UPDATE] As of ~June 2017, Docker for Mac takes care of all the annoying parts of this where you have to mess with VirtualBox. It lets you map basically everything on your local host using the /private prefix. More info here. [/UPDATE]

All the current answers talk about Boot2docker. Since that's now deprecated in favor of docker-machine, this works for docker-machine:

First, ssh into the docker-machine vm and create the folder we'll be mapping to:

docker-machine ssh $MACHINE_NAME "sudo mkdir -p \"$VOL_DIR\""

Now share the folder to VirtualBox:

WORKDIR=$(basename "$VOL_DIR")
vboxmanage sharedfolder add "$MACHINE_NAME" --name "$WORKDIR" --hostpath "$VOL_DIR" --transient

Finally, ssh into the docker-machine again and mount the folder we just shared:

docker-machine ssh $MACHINE_NAME "sudo mount -t vboxsf -o uid=\"$U\",gid=\"$G\" \"$WORKDIR\" \"$VOL_DIR\""

Note: for UID and GID you can basically use whatever integers as long as they're not already taken.

This is tested as of docker-machine 0.4.1 and docker 1.8.3 on OS X El Capitan.

6
  • 1
    +1. After several hours of banging my head, your answer worked. This should be part of the docker-machine doc. Nov 27, 2015 at 4:51
  • Why not use the -v flag?
    – igracia
    May 13, 2016 at 18:27
  • 2
    @igracia the -v flag doesn't work like you'd expect in OS X (or windows). Docker runs in a VM so the folder referenced in -v points to a folder in the VM, not in the root OS. This technique takes the next step of mapping the VM folder back to the root OS. May 20, 2016 at 7:50
  • 1
    @PatrickGunderson In my case (OS X) the folder is in the host, not in the VM, and it works fine. Docker Tolls take care of exporting it to the VM, and from there to the container.
    – igracia
    May 20, 2016 at 8:19
  • 1
    The one exception to the rule I mentioned is that the VM will automatically mount /Users for sharing to docker. That means if your working files are in ~/* then you're good to go. If you have your dev environment setup to run out of /development or anything other than /Users you're gonna have problems docs.docker.com/engine/userguide/containers/dockervolumes May 20, 2016 at 9:18
27

Using command-line :

docker run -it --name <WHATEVER> -p <LOCAL_PORT>:<CONTAINER_PORT> -v <LOCAL_PATH>:<CONTAINER_PATH> -d <IMAGE>:<TAG>

Using docker-compose.yaml :

version: '2'
  services:
    cms:
       image: <IMAGE>:<TAG>
       ports:
       - <LOCAL_PORT>:<CONTAINER_PORT>
       volumes:
       - <LOCAL_PATH>:<CONTAINER_PATH>

Assume :

  • IMAGE: k3_s3
  • TAG: latest
  • LOCAL_PORT: 8080
  • CONTAINER_PORT: 8080
  • LOCAL_PATH: /volume-to-mount
  • CONTAINER_PATH: /mnt

Examples :

  1. First create /volume-to-mount. (Skip if exist)
$ mkdir -p /volume-to-mount
  1. docker-compose -f docker-compose.yaml up -d
version: '2'
  services:
    cms:
       image: ghost-cms:latest
       ports:
       - 8080:8080
       volumes:
       - /volume-to-mount:/mnt
  1. Verify your container :
docker exec -it CONTAINER_ID ls -la /mnt
0
25
docker run -v /host/directory:/container/directory -t IMAGE-NAME /bin/bash

docker run -v /root/shareData:/home/shareData -t kylemanna/openvpn /bin/bash

In my system I've corrected the answer from nhjk, it works flawless when you add the -t flag.

1
  • Are you able to create/update files from local FS and see the changes inside docker container? Sep 22, 2019 at 12:30
22

On Mac OS, to mount a folder /Users/<name>/projects/ on your mac at the root of your container:

docker run -it -v /Users/<name>/projects/:/projects <container_name> bash

ls /projects

17

If the host is windows 10 then instead of forward slash, use backward slash -

docker run -it -p 12001:80 -v c:\Users\C\Desktop\dockerStorage:/root/sketches

Make sure the host drive is shared (C in this case). In my case I got a prompt asking for share permission after running the command above.

15

For Windows 10 users, it is important to have the mount point inside the C:/Users/ directory. I tried for hours to get this to work. This post helped but it was not obvious at first as the solution for Windows 10 is a comment to an accepted answer. This is how I did it:

docker run -it -p 12001:80 -v //c/Users/C/Desktop/dockerStorage:/root/sketches \
<your-image-here> /bin/bash

Then to test it, you can do echo TEST > hostTest.txt inside your image. You should be able to see this new file in the local host folder at C:/Users/C/Desktop/dockerStorage/.

3
  • The double slashes on the path are what made it work for me. It was confusing because I used -v $(pwd):/var/xx first and that did work, but resulted in an empty directory. I am running cyg via babun.
    – kenchilada
    Nov 26, 2018 at 16:58
  • Had the same issue in Windows 7 while using /c/data/, moving the host directory under /c/Users/<user>/ solved it for me. Thanks. Jan 15, 2019 at 22:10
  • 2
    can we have a link to the post that helped you resolve the issue?
    – PBo
    Mar 26, 2019 at 9:51
13

As of Docker 18-CE, you can use docker run -v /src/path:/container/path to do 2-way binding of a host folder.

There is a major catch here though if you're working with Windows 10/WSL and have Docker-CE for Windows as your host and then docker-ce client tools in WSL. WSL knows about the entire / filesystem while your Windows host only knows about your drives. Inside WSL, you can use /mnt/c/projectpath, but if you try to docker run -v ${PWD}:/projectpath, you will find in the host that /projectpath/ is empty because on the host /mnt means nothing.

If you work from /c/projectpath though and THEN do docker run -v ${PWD}:/projectpath and you WILL find that in the container, /projectpath will reflect /c/projectpath in realtime. There are no errors or any other ways to detect this issue other than seeing empty mounts inside your guest.

You must also be sure to "share the drive" in the Docker for Windows settings.

2
  • I'm having this problem with Win10/WSL and Docker-CE/docker-ce. Empty /projectpath/. What do you mean by "work from /c/projectpath"? I tried linking /c/ to /mnt/c and running from /c/projectpath but then I get the error bind mount source path does not exist: /host_mnt/c/projectpath. Aug 31, 2018 at 20:26
  • This link solved my problem: support.divio.com/local-development/docker/… (Docker on Windows only allows mounting from /Users/ by default) Sep 5, 2018 at 16:24
11

Jul 2015 update - boot2docker now supports direct mounting. You can use -v /var/logs/on/host:/var/logs/in/container directly from your Mac prompt, without double mounting

11

I've been having the same issue. My command line looked like this:

docker run --rm -i --name $NAME -v `pwd`:/sources:z $NAME

The problem was with 'pwd'. So I changed that to $(pwd):

docker run --rm -i --name $NAME -v $(pwd):/sources:z $NAME
2
  • 1
    What is the z for?
    – hhh
    Jan 5, 2019 at 23:32
  • 4
    The z option indicates that the bind mount content is shared among multiple containers.
    – Jason
    Jan 27, 2019 at 4:22
8

How do I link the main_folder to the test_container folder present inside the docker container?

Your command below is correct, unless your on a mac using boot2docker(depending on future updates) in which case you may find the folder empty. See mattes answer for a tutorial on correcting this.

docker run -d -v /Users/kishore/main_folder:/test_container k3_s3:latest

I need to make this run automatically, how to do that without really using the run -d -v command.

You can't really get away from using these commands, they are intrinsic to the way docker works. You would be best off putting them into a shell script to save you writing them out repeatedly.

What happens if boot2docker crashes? Where are the docker files stored?

If you manage to use the -v arg and reference your host machine then the files will be safe on your host.

If you've used 'docker build -t myimage .' with a Dockerfile then your files will be baked into the image.

Your docker images, i believe, are stored in the boot2docker-vm. I found this out when my images disappeared when i delete the vm from VirtualBox. (Note, i don't know how Virtualbox works, so the images might be still hidden somewhere else, just not visible to docker).

7

Had the same problem. Found this in the docker documentation:

Note: The host directory is, by its nature, host-dependent. For this reason, you can’t mount a host directory from Dockerfile, the VOLUME instruction does not support passing a host-dir, because built images should be portable. A host directory wouldn’t be available on all potential hosts.

So, mounting a read/write host directory is only possible with the -v parameter in the docker run command, as the other answers point out correctly.

3
  • What is host directory exactly? Jul 6, 2017 at 1:14
  • @NicolasS.Xu Any path on the actual docker host machine. Feb 10, 2019 at 17:50
  • "This feature would not always be useful, so let's make it never useful, for consistency"? Oct 30, 2021 at 14:02
7

I found that any directory laying under system directive like /var, /usr, /etc could not be mount under the container.

The directive should be at user's space -v switch instructs docker daemon to mount local directory to the container, for example:

docker run -t -d -v /{local}/{path}:/{container}/{path} --name {container_name} {imagename}
7

Here's an example with a Windows path:

docker run -P -it --name organizr --mount src="/c/Users/MyUserName/AppData/Roaming/DockerConfigs/Organizr",dst=/config,type=bind organizrtools/organizr-v2:latest

As a side note, during all of this hair pulling, having to wrestle with figuring out, and retyping paths over and over and over again, I decided to whip up a small AutoHotkey script to convert a Windows path to a "Docker Windows" formatted path. This way all I have to do is copy any Windows path that I want to use as a mount point to the clipboard, press the "Apps Key" on the keyboard, and it'll format it into a path format that Docker appreciates.

For example:

Copy this to your clipboard:

C:\Users\My PC\AppData\Roaming\DockerConfigs\Organizr

press the Apps Key while the cursor is where you want it on the command-line, and it'll paste this there:

"/c/Users/My PC/AppData/Roaming/DockerConfigs/Organizr"

Saves a lot to time for me. Here it is for anyone else who may find it useful.

; --------------------------------------------------------------------------------------------------------------
;
; Docker Utility: Convert a Windows Formatted Path to a Docker Formatter Path
;                   Useful for (example) when mounting Windows volumes via the command-line.
;
; By:       J. Scott Elblein
; Version:  1.0
; Date:     2/5/2019
;
; Usage:    Cut or Copy the Windows formatted path to the clipboard, press the AppsKey on your keyboard
;           (usually right next to the Windows Key), it'll format it into a 'docker path' and enter it
;           into the active window. Easy example usage would be to copy your intended volume path via
;           Explorer, place the cursor after the "-v" in your Docker command, press the Apps Key and
;           then it'll place the formatted path onto the line for you.
;
; TODO::    I may or may not add anything to this depending on needs. Some ideas are:
;           
;           - Add a tray menu with the ability to do some things, like just replace the unformatted path
;               on the clipboard with the formatted one rather than enter it automatically.
;           - Add 'smarter' handling so the it first confirms that the clipboard text is even a path in
;               the first place. (would need to be able to handle Win + Mac + Linux)
;           - Add command-line handling so the script doesn't need to always be in the tray, you could
;               just pass the Windows path to the script, have it format it, then paste and close.
;               Also, could have it just check for a path on the clipboard upon script startup, if found
;               do it's job, then exit the script.
;           - Add an 'all-in-one' action, to copy the selected Windows path, and then output the result.
;           - Whatever else comes to mind.
;
; --------------------------------------------------------------------------------------------------------------

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

AppsKey::

    ; Create a new var, store the current clipboard contents (should be a Windows path)
    NewStr := Clipboard

    ; Rip out the first 2 chars (should be a drive letter and colon) & convert the letter to lowercase
    ; NOTE: I could probably replace the following 3 lines with a regexreplace, but atm I'm lazy and in a rush.
    tmpVar := SubStr(NewStr, 1, 2)
    StringLower, tmpVar, tmpVar

    ; Replace the uppercase drive letter and colon with the lowercase drive letter and colon
    NewStr := StrReplace(NewStr, SubStr(NewStr, 1, 2), tmpVar)

    ; Replace backslashes with forward slashes
    NewStr := StrReplace(NewStr,  "\", "/")

    ; Replace all colons with nothing
    NewStr := StrReplace(NewStr, ":", "")

    ; Remove the last char if it's a trailing forward slash
    NewStr :=  RegExReplace(NewStr, "/$")

    ; Append a leading forward slash if not already there
    if RegExMatch(NewStr, "^/") == 0
        NewStr :=  "/" . NewStr

    ; If there are any spaces in the path ... wrap in double quotes
    if RegExMatch(NewStr, " ") > 0
        NewStr :=  """" . NewStr . """"

    ; Send the result to the active window
    SendInput % NewStr 
0
6

To get this working in Windows 10 I had to open the Docker Settings window from the system tray and go to the Shared Drives section.

I then checked the box next to C. Docker asked for my desktop credentials to gain authorisation to write to my Users folder.

Then I ran the docker container following examples above and also the example on that settings page, attaching to /data in the container.

docker run -v c:/Users/<user.name>/Desktop/dockerStorage:/data -other -options
5

boot2docker together with VirtualBox Guest Additions
How to mount /Users into boot2docker

https://medium.com/boot2docker-lightweight-linux-for-docker/boot2docker-together-with-virtualbox-guest-additions-da1e3ab2465c

tl;dr Build your own custom boot2docker.iso with VirtualBox Guest Additions (see link) or download http://static.dockerfiles.io/boot2docker-v1.0.1-virtualbox-guest-additions-v4.3.12.iso and save it to ~/.boot2docker/boot2docker.iso.

3
  • I tried using that iso and I still ran into the same problems... Using the directions outlined in that post helped me to sort it out though - I basically had to force the shared folder. Aug 18, 2014 at 17:13
  • I used the iso, it stumbled in that i'd not stopped the Virtual Box VM before calling the 'VBoxManage sharedfolder add ...' cmd. Finally got it all working though so huge thanks.
    – Emile
    Sep 4, 2014 at 15:36
  • Note: the reason the -v arg doens't work through boot2docker, is apparently something to do with it mounting to the boot2docker-vm and not your actual host. which is why i guess the Vboxmanage part helps it work.
    – Emile
    Sep 4, 2014 at 15:38
5

Note that in Windows you'll have to provide the absolute path.

Below worked for me.

docker run -t -i -v D:/projects/:/home/chankeypathak/work -p 8888:8888 jupyter/tensorflow-notebook /bin/bash
0
3

i had same issues , i was trying to mount C:\Users\ folder on docker
this is how i did it Docker Toolbox command line

 $ docker run -it --name <containername> -v /c/Users:/myVolData <imagename>
3

You can also do this with Portainer web application for a different visual experience.

First pull the Portainer image:

docker pull portainer/portainer

Then create a volume for Portainer:

docker volume create portainer_data

Also create a Portainer container:

docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

You will be able to access the web app with your browser at this URL: "http://localhost:9000". At the first login, you will be prompted to set your Portainer admin credentials.

In the web app, follow these menus and buttons: (Container > Add container > Fill settings > Deploy Container)

I had trouble to create a "mount" volume with Portainer and I realized I had to click "bind" when creating my container's volume. Below is an illustration of the volume binding settings that worked for my container creation with a mounted volume binded to the host.

volume binding settings

P.S.: I'm using Docker 19.035 and Portainer 1.23.1

3

Quoting from the Official Website:

  1. Make sure you don’t have any previous getting-started containers running.

  2. Run the following command from the app directory.

  • x86-64 Mac or Linux device:

    docker run -dp 3000:3000 \
      -w /app -v "$(pwd):/app" \
      node:12-alpine \
      sh -c "yarn install && yarn run dev"
    
  • Windows (PowerShell):

    docker run -dp 3000:3000 \
      -w /app -v "$(pwd):/app" \
      node:12-alpine \
      sh -c "yarn install && yarn run dev"
    
  • Aple silicon Mac or another ARM64 device:

    docker run -dp 3000:3000 \\
      -w /app -v "$(pwd):/app" \\
      node:12-alpine \\
      sh -c "apk add --no-cache python2 g++ make && yarn install && yarn run dev"
    

Explaining:

  • dp 3000:3000 - same as before. Run in detached (background) mode and create a port mapping
  • w /app - sets the “working directory” or the current directory that the command will run from
  • v "$(pwd):/app" - bind mount the current directory from the host into the /app directory in the container
  • node:12-alpine - the image to use.

Note that this is the base image for our app from the Dockerfile sh -c "yarn install && yarn run dev" - the command.

We’re starting a shell using sh (alpine doesn’t have Bash) and running yarn install to install all dependencies and then running yarn run dev. If we look in the package.json, we’ll see that the dev script is starting nodemon.

2

I had the same requirement to mount host directory from container and I used volume mount command. But during testing noticed that it's creating files inside container too but after some digging found that they are just symbolic links and actual file system used form host machine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.