0

Here I'm not asking about how to mount a directory from the host machine to a docker container, but instead how can I edit a mounted directory from a container.

I'll give more details about my use case

Now I'm working on a Gitlab fork, and using GDK (Gitlab Development Kit) and during the installation, I run this command:

gdk install gitlab_repo=https://gitlab.com/MY-FORK/gitlab.git

This command will create a directory inside the project called gitlab

And to work on the project, I'll be working using a text-editor from the host machine, so I need to sync the two directories

This was my attempt:

docker run -it -p 3000:3000 -v /gitlab:${project-location}/gitlab ${image-name}

The problem with this approach is when I'm running the installation command:

gdk install gitlab_repo=https://gitlab.com/MY-FORK/gitlab.git 

It fails because it's trying to change the files inside the mounted directory and this is not allowed (permission denied)

Hint: all the installation steps are described in a Dockerfile so everything is running inside the container!

So is there a workaround or another way of doing that?

Muhammad Hamada
  • 725
  • 5
  • 13
  • I have tried something similar but using bind mount volume (-v : ) – Abdullah Shaikh Jan 23 '17 at 03:08
  • @MrSpark I'm doing that already, the problem is the default behaviour is changing this volume from the host machine only, but in my case, I need both (host/container) to be able to edit this volume content – Muhammad Hamada Jan 23 '17 at 06:47

1 Answers1

1

Permission problems with a host volume (bind mounted directory into the container) happen when the permissions and ownership on the files at the host, typically the UID, do not match those used inside the container itself. You'll need to either adjust the user used inside the container, change the permissions of the files on the host, or both.

Another possible problem is using Docker for Windows or Docker for Mac and using a directory that isn't shared with the embedded VM. The volume mount will result in an empty folder in those cases. By default, /Users is shared with the VM in both of these products.

Note that this issue is fairly typical and the reason I try to use named volumes using docker's "local" driver when possible. Named volumes initialize to the contents of the image, including the file permissions, and you can manage them by using a separate management container that mounts the same volume for any changes you need to make (e.g. a simple busybox container running a tar -xzf to update the contents).


Edit: here's an example of editing a file from inside the container

$ ls -al data
total 16
drwxr-xr-x  4 bmitch bmitch 4096 Jun  8  2016 .
drwxr-xr-x 12 bmitch bmitch 4096 Jan 22 20:13 ..
-rw-r--r--  1 bmitch bmitch    0 Jun  8  2016 1
-rw-r--r--  1 bmitch bmitch    0 Jun  8  2016 2
drwxr-xr-x  2 bmitch bmitch 4096 Jun  8  2016 a
drwxr-xr-x  2 bmitch bmitch 4096 Jun  8  2016 b

$ id
uid=1000(bmitch) gid=1000(bmitch) groups=1000(bmitch),24(cdrom),27(sudo),120(bluetooth),127(vboxusers),999(docker)

$ docker run -v `pwd`/data:/data -u 1000 -it --rm busybox

/ $ ls -al /data
total 16
drwxr-xr-x    4 1000     1000          4096 Jun  8  2016 .
drwxr-xr-x   19 root     root          4096 Jan 23 10:24 ..
-rw-r--r--    1 1000     1000             0 Jun  8  2016 1
-rw-r--r--    1 1000     1000             0 Jun  8  2016 2
drwxr-xr-x    2 1000     1000          4096 Jun  8  2016 a
drwxr-xr-x    2 1000     1000          4096 Jun  8  2016 b

/ $ echo 'hello from inside the container' >/data/inside-container.txt

/ $ ls -al /data
total 20
drwxr-xr-x    4 1000     1000          4096 Jan 23 10:25 .
drwxr-xr-x   19 root     root          4096 Jan 23 10:24 ..
-rw-r--r--    1 1000     1000             0 Jun  8  2016 1
-rw-r--r--    1 1000     1000             0 Jun  8  2016 2
drwxr-xr-x    2 1000     1000          4096 Jun  8  2016 a
drwxr-xr-x    2 1000     1000          4096 Jun  8  2016 b
-rw-r--r--    1 1000     root            32 Jan 23 10:25 inside-container.txt

/ $ cat /data/inside-container.txt
hello from inside the container

/ $ exit

$ ls -al data
total 20
drwxr-xr-x  4 bmitch bmitch 4096 Jan 23 05:25 .
drwxr-xr-x 12 bmitch bmitch 4096 Jan 22 20:13 ..
-rw-r--r--  1 bmitch bmitch    0 Jun  8  2016 1
-rw-r--r--  1 bmitch bmitch    0 Jun  8  2016 2
drwxr-xr-x  2 bmitch bmitch 4096 Jun  8  2016 a
drwxr-xr-x  2 bmitch bmitch 4096 Jun  8  2016 b
-rw-r--r--  1 bmitch root     32 Jan 23 05:25 inside-container.txt
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • thanks, and I understood that if I changed the directory owner during the installation in the Dockerfile to the container user, it'll be possible to edit that directory from inside the container, right? Then how the host machine user and edit it again, I need both can do any changes required – Muhammad Hamada Jan 23 '17 at 06:44
  • Not quite sure I'm following the comment. I've updated my answer with an example. If this is still confusing, please provide a [mcve](http://stackoverflow.com/help/mcve) of your own where this doesn't work. Thanks. – BMitch Jan 23 '17 at 10:30
  • 1
    Thanks man, your hint about the *permissions and ownership* helped solving the problem – Muhammad Hamada Jan 23 '17 at 19:27