Cannot mount data volume in Docker with Windows - docker

I am running Docker on Windows. I have succesfully build an image with command:
docker build -t my/tag /c/Users/user/Docker/docker-php/image/
docker images result:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
my/tag latest 763a2f01c832 28 minutes ago 445.8 MB
php 5.6-cli 32d2bd1d7a19 6 days ago 444.5 MB
Unfortunately I have a problem, when I am trying to mount data volume to image. I run:
docker run --rm -it -v /``pwd``:/src -w /src my/tag bash
(There is single backtick on pwd, but I don't knwo how to escape it.)
But files aren't visible in docker:
root#3e91981ef517:/src# ls -al
total 4
drwxr-xr-x 2 root root 40 Aug 31 18:59 .
drwxr-xr-x 47 root root 4096 Aug 31 19:15 ..
I have tried with different approach, such as:
docker run --rm -it -v /``pwd``:/src -w /src my/tag bash
(There is single backtick on pwd, but I don't knwo how to escape it.)
docker run --rm -it -v /full/path/to/sources:/src -w /src my/tag bash
docker run --rm -it -v //full/path/to/sources:/src -w /src my/tag bash
But every time the same result.

Related

Docker Volume Persistence not working for more than one run of a container

Docker version 20.10.2
I'm just starting out on Docker and following training guides - but something hasn't been mentioned so far (that I have discovered) - when I run a container to write some data out to Docker volume, if I run that container again and attach to the same volume, the newly named data will not append into it ?
Here is my rather basic Dockerfile
FROM ubuntu
RUN mkdir applocal
RUN touch applocal/applocalfile."$(date --iso-8601=seconds)"
RUN ls -la applocal
I run this sequence of commands...
docker build Dockerfile -t mine/applocal-persist
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM ubuntu
---> c29284518f49
Step 2/4 : RUN mkdir applocal
---> Running in 9f796f4d988a
Removing intermediate container 9f796f4d988a
---> 99005a7ffed1
Step 3/4 : RUN touch applocal/applocalfile."$(date --iso-8601=seconds)"
---> Running in ffbf2f4c636a
Removing intermediate container ffbf2f4c636a
---> 199bc706dcc6
Step 4/4 : RUN ls -la applocal
---> Running in 7da02faa9fba
total 8
drwxr-xr-x 1 root root 4096 Jul 16 13:52 .
drwxr-xr-x 1 root root 4096 Jul 16 13:52 ..
-rw-r--r-- 1 root root 0 Jul 16 13:52 applocalfile.2021-07-16T13:52:00+00:00
Removing intermediate container 7da02faa9fba
---> 7387c521d82b
Successfully built 7387c521d82b
Successfully tagged mine/applocal-persist:latest
Then run the command...
docker run -v applocalsaved:/applocal mine/applocal-persist
Looking at the Volume data it has worked
ls -la /var/lib/docker/volumes/applocalsaved/_data/
total 8
drwxr-xr-x 2 root root 4096 Jul 16 14:55 .
drwxr-xr-x 3 root root 4096 Jul 16 14:55 ..
-rw-r--r-- 1 root root 0 Jul 16 14:52 applocalfile.2021-07-16T13:52:00+00:00
If I wait a few minutes later and re-run docker run -v applocalsaved:/applocal mine/applocal-persist
...and check the volume data again, no new file exists
ls -la /var/lib/docker/volumes/applocalsaved/_data/
total 8
drwxr-xr-x 2 root root 4096 Jul 16 14:55 .
drwxr-xr-x 3 root root 4096 Jul 16 14:55 ..
-rw-r--r-- 1 root root 0 Jul 16 14:52 applocalfile.2021-07-16T13:52:00+00:00
Run history...
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d16e9aa495e mine/applocal-persist "bash" 57 seconds ago Exited (0) 55 seconds ago distracted_cohen
69ff06d9c886 mine/applocal-persist "bash" 2 minutes ago Exited (0) 2 minutes ago affectionate_lehmann
I've listed the Volume Inspect here...
docker volume inspect applocalsaved
[
{
"CreatedAt": "2021-07-16T14:55:24+01:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/applocalsaved/_data",
"Name": "applocalsaved",
"Options": null,
"Scope": "local"
}
]
I'm obviously missing a trick here - or misunderstanding what is going on or the design around this.
Thanks in advance
For info: I'm using Windows running Virtual Box and running Ubuntu 21.04 as a VM
Those commands run once when the image is built.
If you want something to run on container startup, you can use CMD or ENTRYPOINT
https://docs.docker.com/engine/reference/builder/#cmd
https://docs.docker.com/engine/reference/builder/#entrypoint
The commands in the Dockerfile only run once, when the image is originally built. You can verify this for example by just running the image without a volume mount:
docker build -t mine/applocal-persist .
docker run --rm mine/applocal-persist \
ls -l ./applocal
sleep 60
docker run --rm mine/applocal-persist \
ls -l ./applocal
If you start the container with a named volume mounted, only if the volume is a Docker named volume and only if the volume is empty, the contents of the image will be copied into the volume. (This doesn't happen on Docker bind mounts, Kubernetes volumes, or if the image has changed; I would not rely on this for any sort of data sharing since it works in so few contexts.)
Conversely, if you start the container with any sort of volume mounted, whatever content is in the volume completely replaces what's in the image. You can see this with some more experimentation:
# Build the image
docker build -t mine/applocal-persist
# Start the container with a new named volume mounted; see what's there.
docker volume rm applocalsaved
docker run --rm -v applocalsaved:/applocal mine/applocal-persist \
ls -l /applocal
# Edit a file in the volume and see that it gets persisted across restarts
docker run --rm -v applocalsaved:/applocal mine/applocal-persist \
touch /applocal/foo
docker run --rm -v applocalsaved:/applocal mine/applocal-persist \
ls -l /applocal
# But it is not included in the image without the bind mount
docker run --rm mine/applocal-persist \
ls -l /applocal
sleep 60
# Rebuild the image
docker build -t mine/applocal-persist
# In the base image, you will see the updated timestamp
docker run --rm mine/applocal-persist \
ls -l /applocal
# But if you mount the volume, the old volume contents replace the
# image contents and you will only see the old timestamp
docker run --rm -v applocalsaved:/applocal mine/applocal-persist \
ls -l /applocal

Cannot mount directory inside docker container

I can't mount directory. I'm using linux containers on win10.
docker run --rm -v c:\users\pperak:/data alpine ls /data -la
docker run --rm -v /c/users/pperak:/data alpine ls /data -la
ls -la returns
drwxr-xr-x 2 root root 40 Feb 14 15:29 .
drwxr-xr-x 1 root root 4096 Feb 14 15:33 ..
https://docs.docker.com/storage/bind-mounts/ says not to use --volume and use --mount instead
docker run --rm --mount type=bind,source=c:\users\pperak,target=/data alpine ls /data -la
docker run --rm --mount type=bind,source=/c/users/pperak,target=/data alpine ls /data -la
But this also doesn't work.
What am I doing wrong?
After spending half a day on it I reminded myself that I once had similar problem. Some files not visible and other visible as folders after adding Docker volume
(Some files not visible and other visible as folders after adding Docker volume)
It didn't work because I changed password and after you change your password you need to disable sharing drives and enable it again. You will be asked for your password then and it will work again.

Permission denied when get contents generated by a docker container on the local fileystem

I use the following command to run a container:
docker run -it -v /home/:/usr/ ubuntu64 /bin/bash
Then I run a program in the container, the program generates some files in the folder:/usr/ which also appear in /home/ but I can't access the generated files with an error: Permission denied outside the container.
I think this may because the files generated by root in the container but outside the container, the user have no root authority, but how to solve it?
What I want to do is accessing the files generated by the program(installed in the container) outside the container.
You need to use the -u flag
docker run -it -v $PWD:/data -w /data alpine touch nouser.txt
docker run -u `id -u` -it -v $PWD:/data -w /data alpine touch onlyuser.txt
docker run -u `id -u`:`id -g` -it -v $PWD:/data -w /data alpine touch usergroup.txt
Now if you do ls -alh on the host system
$ ls -alh
total 8.0K
drwxrwxr-x 2 vagrant vagrant 4.0K Sep 9 05:22 .
drwxrwxr-x 30 vagrant vagrant 4.0K Sep 9 05:19 ..
-rw-r--r-- 1 root root 0 Sep 9 05:21 nouser.txt
-rw-r--r-- 1 vagrant root 0 Sep 9 05:21 onlyuser.txt
-rw-r--r-- 1 vagrant vagrant 0 Sep 9 05:22 usergroup.txt

Mount "named volume" as non-root in Docker

Is there any way to mount a named volume as a non-root user? I am trying to avoid having to run a chown in each Dockerfile but I need the mount to be writable by a non-root user to be able to write the artifacts created by a build in the image
This is what I'm trying
docker run --rm -it -v /home/bob/dev/:/src/dev -v builds:/mnt/build --name build hilikus/build /bin/bash
but for the second mount I get
[user#42f237282128 ~]$ ll /mnt
total 4
drwxr-xr-x 2 root root 4096 Sep 18 19:29 build
My other mount (/src/dev/) is owned by user, not by root so it gives what I need; however, I haven't been able to do the same with the named volume.
The named volume initializes to the contents of your image at that location, so you need to set the permissions inside your Dockerfile:
$ cat df.vf-uid
FROM busybox
RUN mkdir -p /data && echo "hello world" > /data/hello && chown -R 1000 /data
$ docker build -t test-vf -f df.vf-uid .
Sending build context to Docker daemon 23.06 MB
Step 1 : FROM busybox
---> 2b8fd9751c4c
Step 2 : RUN mkdir -p /data && echo "hello world" > /data/hello && chown -R 1000 /data
---> Using cache
---> 41390b132940
Successfully built 41390b132940
$ docker run -v test-vol:/data --rm -it test-vf ls -alR /data
/data:
total 12
drwxr-xr-x 2 1000 root 4096 Sep 19 15:26 .
drwxr-xr-x 19 root root 4096 Sep 19 15:26 ..
-rw-r--r-- 1 1000 root 12 Aug 22 11:43 hello
If you use the new --mount syntax instead of the old -v/--volume syntax it is supposedly possible to assign a uid to the volume's contents via docker volume create somename --opt -o=uid=1000 or something similar.
See https://docs.docker.com/engine/reference/commandline/volume_create/#driver-specific-options
I haven't fully tested this to run as non-root or using the dockremap dynamic user with the userns-map option but hope to soon.

docker cp from host to container is not working

I am new to docker concept and trying to copy some file from my host to the container. Assuming that my docker name is my_docker when I run the following:
docker cp my_docker:/usr/local/src/test.txt test.txt
It copies the test.txt file from my container to the local host. But doing it the other way around is not working. Here is the command:
docker cp test.txt my_docker:/usr/local/src
Is there something that I am doing run? Any help would be much appreciated.
Thanks
As ahajib mentioned in the comments, you're looking for the file you copied in a different container.
$ docker run --name my_container alpine
$ docker cp test.txt my_container:/tmp/test.txt
$ docker run alpine ls -l /tmp
total 0
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6224c947fbbd alpine "ls -l /tmp" 11 seconds ago Exited (0) 10 seconds ago brave_neumann
22951689a3e4 alpine "/bin/sh" About a minute ago Exited (0) About a minute ago my_container
You copied the file into my_container, but you ran the ls -l /tmp command in brave_neumann.
If you want to copy files into a container and then use those files in the container, you either have to copy the files while the container is still running, or you copy them to a docker volume.
Mounting that volume in a new container then lets it see the files you copied in.
$ docker volume create my_volume
my_volume
$ docker run --name my_container2 -v my_volume:/data alpine echo OK
OK
$ docker cp test.txt my_container2:/data/test.txt
$ docker run -v my_volume:/data alpine ls -l /data
total 4
-rw-r--r-- 1 1006 1006 29 Oct 6 19:43 test.txt
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
15b36ccd535f alpine "ls -l /data" 7 seconds ago Exited (0) 6 seconds ago agitated_khorana
b9d4c9e0902f alpine "echo OK" 54 seconds ago Exited (0) 53 seconds ago my_container2
6224c947fbbd alpine "ls -l /tmp" 2 minutes ago Exited (0) 2 minutes ago brave_neumann
22951689a3e4 alpine "/bin/sh" 3 minutes ago Exited (0) 3 minutes ago my_container
$
In your second command you appear to be trying to copy test.txt into /usr/local with the name src. Try adding a / to the end of the destination path:
docker cp test.txt my_docker:/usr/local/src/
to copy with the file name or: docker cp test.txt my_docker:/usr/local/src/newname.txt to rename it in the container.

Resources