Mount container volume root folder?

First of all thanks for the great product! I am really enjoying exploring Docker.

Here is my first post. If something is wrong in posting, please guide me. So my question is;

Is it possible to mount container top level folder to a folder on host?
like this;
docker run -it -v /root/any/folder:/ --name test1 ubuntu:16.04
I tried it but failed. Is there any way to do that?

Hi , you can mount a host directory in docker container

the right syntax should be

docker run -it -v <host_directory_path>:<container_path> imagename

since your run command is searching for /root/any/folder in your host machine , and as this location is invalid…its failing

Applying above command should give you a sync for both container directory and host directory

Thanks a lot for the reply.

The folder /root/any/folder does exist. It is giving an error message about the container_path. That container path “/” is not valid.

My question is that if a container path /root/any/path is valid then why not we can mount the top level container folder. The /

Is it clear now?

If you could successfully run docker run -v /host/path:/ image then it would cause the contents of /host/path to be the only thing visible in the container; it would be the container’s root. That is, you can mount things into the container but not out.

My recommendation would be to use docker run -v sparingly – maybe only for /var/log and any persistent data your application requires – but if you don’t mind manually managing paths and aren’t using a multi-host setup then using explicit host paths for these are fine.

Got it. Thanks for the reply. It makes sense.

It would appear that docker now blocks you mounting root / :

~$ docker run -it --name alp -v /mnt/docker/alp:/ alpine
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
59bf1c3509f3: Pull complete 
Digest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
Status: Downloaded newer image for alpine:latest
docker: Error response from daemon: invalid volume specification: '/mnt/docker/alp:/': invalid mount config for type "bind": invalid specification: destination can't be '/'.

It was probably always the case :slight_smile:

The exact error message may have changed.

Just out of curiosity: what sense does it make to create a container and replace it’s entire filesystem (image layers + copy-on-write container layer) with a bind-mount from a host folder?

It would not make sense usually but I think the original goal was to make the container’s filesystem available in a specific directory on the host.

which is not what bind mount is for. Other goal could be creating an empty image without filesystem (from scratch), starting a container and mounting the filesystem from the host. I did something similar in my latest tutorial but I mounted a file not the root filesystem.