docker bind mounting not work with "not home" folder - docker

I'm tring to play with bind mounting and i encourred in a strange behavior, i understand that bind mounting mount a host's folder in the container file system obscuring the original container content. Now when i try to do for examle:
docker run -it -v /home/user:/tmp ubuntu bash
in the /tmp folder of contaner there is the user's home but when i try to bind a "not home folder" like /var/lib:
docker run -it -v /var/lib:/tmp ubuntu bash
the /tmp folder inside a container is empty, why this appen?
Moreover if i do inside at the last container for example "touch foo" and i run another container with the same binding:
docker run -it -v /var/lib:/tmp ubuntu bash
I'll find the foo file inside /tmp folder
additional info: i run a ubuntu 19 server inside a VMaware virtual machine

i found a "dirty" solution, i had previoussly installed docker via snap, i reinstalled docker via apt and now work fine, this will remain a minstery

Related

Windows WSL2 docker.exe volume mount differs from wsl docker volume mounts

I got Docker Desktop installed on Windows with WSL2 support. Everything works as expected. When I run my containers with a volume mount docker run -it --rm -v W:\projects:/projects busybox i can access all my windows files inside this folder.
Sadly the performance isn't that great with windows shares inside docker, so i tried to mount a path from my wsl machine.
i was under the impression that docker would run inside wsl? So I expected the two commands to output the same:
docker run -it --rm -v /home/:/myHome busybox ls -l /myHome
wsl docker run -it --rm -v /home/:/myHome busybox ls -l /myHome
but the output using docker is just total 0 where as the output using wsl is my home directory.
Can someone explain to me where this /home directory is (physically / in wsl / my computer) when I run docker from windows? And is it possible to run docker and it runs wsl docker without symlinks / path modifications so i can mount my linux directory inside the container?
If wsl-2 is installed, you can access its file system by going to the following path :-
\\wsl$
/home wouldn't just work as its not physically present in Windows's file system
You can however use /home or any other linux based directories if you login to your wsl distro. Please note that the following command won't mount any volumes if you run it from windows. It should be run only from your wsl distro
docker run --name mycontainer -v /home:myhome busybox
To access the /home directory in an Ubuntu-16.04 distro from windows:-
\\wsl$\Ubuntu-16.04\home
You can replace Ubuntu-16.04 with your distro name - version
To mount any of the directories which is under wsl, ensure that you have turned on the option "Enable integration with my default wsl distro"
https://docs.docker.com/docker-for-windows/wsl/
To mount a wsl's directory from windows as a volume, provide your host volume path in the given format
docker run --name mycontainer -v \\wsl$\Ubuntu-16.04\home:/myHome busybox
Basically, docker run -v has an effect from which environment its being executed i.e either windows or wsl
And docker volumes are present in the following path if you have enabled wsl-2 for docker but don't want to use your distro's file system
\\wsl$\docker-desktop-data\version-pack-data\community\docker\volumes\

Docker bind mount directory in /tmp not working

I'm trying to mount a directory in /tmp to a directory in a container, namely /test. To do this, I've run:
docker run --rm -it -v /tmp/tmpl42ydir5/:/test alpine:latest ls /test
I expect to see a few files when I do this, but instead I see nothing at all.
I tried moving the folder into my home directory and running again:
docker run --rm -it -v /home/theolodus/tmpl42ydir5/:/test alpine:latest ls /test
at which point I see the expected output. This makes me thing I have mis-configured something and/or the permissions have bitten me. Have I missed a step in installing docker? I did it via sudo snap install docker, and then configured docker to let me run as non-root by adding myself to the docker group. Running as root doesn't help...
Host machine is Ubuntu 20.04, docker version is 19.03.11
When running docker as a snap...
all files that Docker uses, such as dockerfiles, to be in $HOME.
Ref: https://snapcraft.io/docker
The /tmp filesystem simply isn't accessible to the docker engine when it's running within the snap isolation. You can install docker directly on Ubuntu from the upstream Docker repositories for a more traditional behavior.

Volume not mounting properly when running shell inside a container

I want to encrypt my Kubernetes file to integrate it with Travis CI and for that, I am installing Travis CI CLI via docker container. When the container runs and I mount my current working directory to /app It just creates an empty folder.
I have added the folder in shared folders as well in the Virtual Box but nothing seems to work. I am using Docker Toolbox on Windows 10 home.
docker run -it -v ${pwd}:/app ruby:2.3 sh
It creates the empty app folder along with the other folders in the container but does not mount the volumes.
I also tried using
docker run -it -v //c/complex:/app ruby:2.3 sh
as someone suggested to use the name you specify in the Virtual Box.
Docker run -it -v full path of current directory:/app ruby:2

using local folder into docker container

Hi I have a windows machine and I installed a docker desktop on it and created a ubuntu container on it.
In docker settings I checked my C: Drive under shared drive option. and I created a folder under /opt named /mydata in this container
Now I run this command:
docker my_container_name run -v /Users/john/Documents/DOCKER_FOLDER:/opt/mydata
But I don't see the files under DOCKER_FOLDER to be in /opt/mydata folder.
Not sure what I a doing wrong.
the right command is:
docker my_container_name run -v c:/Users/john/Documents/DOCKER_FOLDER:/opt/mydata ls /opt/mydata
so you need to specify the volume letter and a command to run

Docker on WSL won't bind mount $HOME

I have the strangest situation using Docker on WSL (Windows Subsystem for Linux, Ubuntu 16.04). I'm trying to bind mount /home/username (or just $HOME for convenience) as a volume in a container, and instead of finding the content of my home directory in the container, I get some other volume entirely.
What's stranger is that this 'other volume' persists from one container to the other, whenever I try to bind mount $HOME or /home/username. If I touch a new file, it appears in all other containers I mount $HOME into. All other bind mounts to any other directory work correctly.
E.g. these all share the same mystery folder:
docker run -it --rm -v /home/username:/test alpine sh
docker run -it --rm -v $HOME:/test alpine sh
docker run -it --rm -v $HOME:/test -v $HOME:/test2 alpine sh
When I do a docker volume ls there's no volume called /home/username, so that rules out accidentally having a docker-hosted volume with the same name.
What is this mysterious volume I'm mounting, and why is docker not mounting my $HOME directory correctly?
I used the instructions in https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly#ensure-volume-mounts-work to set up everything.
Then I had to explicitly export HOME=/c/Users/rfay so that the Docker daemon on Windows could access it. But that worked. The basic magic is that your pathing in WSL has to be something that the Docker daemon can translate in native Windows.
There is no need to change the mound point as suggested by #rfay. Rather if you use a little command foo you can use pwd and sed to fix the value for you.
docker run -it -v $(pwd | sed 's/^\/mnt//'):/var/myfolder -w "/var/myfolder" centos:7
pwd will return the current working folder, usually in the format '/mnt/c/code/myfolder'. Piping this to sed and replacing '/mnt' with nothing will leave you with a path such as '/c/code/myfolder' which is the desired path for docker for windows. you need to wrap the whole thing in $() to cause it to be executed in place.
I find this works really well.

Resources