15

I am currently using Boot2Docker on Windows. Is it possible to mount root to host?

Say that I'm using an Ubuntu image and I would like to mount / to the host. How can I do so?

I've been looking around and trying:

docker run -v /c/Users/ubuntu:/ --name ubuntu -dt ubuntu

But I ended up with an error:

docker: Error response from daemon: Invalid bind mount spec "/c/Users/ubuntu:/": volumeslash: Invalid specification: destination can't be '/' in '/c/Users/Leon/ubuntu:/'.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Leon
  • 446
  • 1
  • 5
  • 16

4 Answers4

8

If I understand correctly, you are trying to mount root inside a container as a volume? If that is the case, rather create a new directory inside and expose that one.

For example, dockerfile:

RUN mkdir /something
VOLUME /something

As the Docker documentation says, the container directory must always be an absolute path such as /src/docs. The host-dir can either be an absolute path or a name value.

For more information read this: https://docs.docker.com/engine/userguide/containers/dockervolumes/#mount-a-host-directory-as-a-data-volume and part "Mount a host directory as a data volume" should give you better understanding.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prototype
  • 276
  • 2
  • 12
  • 5
    What i'm trying to achieve actually is to expose all the files from container's / path to the Host, is it possible? I do know to mount a single directory but that's not really what I want. – Leon Mar 17 '16 at 08:20
  • 1
    No that's not possible...container has some files which cannot be exposed as you don't have root access inside container. – Prototype Mar 17 '16 at 09:04
  • hmm i guess this is a bad question then :\ i wish to expose all the files to host so that i can edit them easily using IDE or stuff like that.. I guess I'm down to mounting directories then – Leon Mar 17 '16 at 09:20
  • Why would you need root folder for doing all that. If you create project in folder project and then create same directory inside container, copy all files and expose a volume you will be able to do exactly what you want. – Prototype Mar 17 '16 at 09:37
  • Yup, thanks. I just wanted to justify the question above if it's achievable – Leon Mar 17 '16 at 10:05
  • @Prototype I am having the same problem as the OP, but slightly different environment. I have Windows 2016 as host system and running docker directly (with a Windows container). In my case, the folder on the guest does exist, but I am still getting the error as mentioned here in this post. (It works fine when I use a Ubuntu container, but having trouble with Windows): http://superuser.com/questions/1051520/docker-windows-container-how-to-mount-a-host-folder-as-data-volume-on-windows Do you have by any chance have an idea for this case? I could imagine it's still a bug in the Win2016 preview. – Mathias Conradt Mar 17 '16 at 12:43
  • I will check it on your question page and see if I can help ;) – Prototype Mar 18 '16 at 08:05
  • 4
    The link is broken. – Peter Mortensen Aug 23 '18 at 21:44
  • perhaps the link is broken because the Docker people don't give a damn about their users, or even backwards compatibility. @PeterMortensen, why can't they create URLs that don't change? – Felipe Valdes Jul 27 '19 at 10:30
2

It's the problem with how you are specifying the path. See the example of mounting a local volume to be used by a container for MongoDB:

docker run --name *container-name* -v **/Users/SKausha3/mongo/imageservicedb/data**:/*data* -v **/Users/SKausha3/mongo/imageservicedb/backup**:/*backup*

c:/Users/SKausha3/mongo/imageservicedb/data is my local folder, but you have to remove 'c:' from the path.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Since you cant mount "/" one option is to add a "WORKDIR" to your Dockerfile, that way all subsequent commands will be relative to that dir and you wont have to modify anything!

FROM python:latest
WORKDIR /myapp
COPY appfile.py appfile.py

In your docker image, the "appfile.py" file will be in the /myapp/appfily.py location.

BLang
  • 930
  • 3
  • 16
  • 35
0

You cannot specify the '/' root directory of container but you can mount all the folders in to docker volumes present in root directory.....

create volumes by running these command one by one or you can create bash script

docker volume create var

docker volume create usr

docker volume create tmp

docker volume create sys

docker volume create srv

docker volume create sbin

docker volume create run

docker volume create root

docker volume create proc

docker volume create opt

docker volume create mnt

docker volume create media

docker volume create libx32

docker volume create lib64

docker volume create lib32

docker volume create lib

docker volume create home

docker volume create etc

docker volume create dev

docker volume create boot

docker volume create bin 

Then run this command

docker run -it -d \
--name=ubuntu-container \
--mount source=var,destination=/var \
--mount source=usr,destination=/usr \
--mount source=tmp,destination=/tmp \
--mount source=sys,destination=/sys \
--mount source=srv,destination=/srv \
--mount source=sbin,destination=/sbin \
--mount source=run,destination=/run \
--mount source=root,destination=/root \
--mount source=opt,destination=/opt \
--mount source=mnt,destination=/mnt \
--mount source=media,destination=/media \
--mount source=libx32,destination=/libx32 \
--mount source=lib64,destination=/lib64 \
--mount source=lib32,destination=/lib32 \
--mount source=lib,destination=/lib \
--mount source=home,destination=/home \
--mount source=etc,destination=/etc \
--mount source=boot,destination=/boot \
--mount source=bin,destination=/bin \
ubuntu:latest