简体   繁体   English

如何从Docker容器正确编辑主机目录

[英]How to properly edit host directory from a docker container

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. 在这里,我不是在问如何将目录从主机安装到Docker容器,而是要如何编辑从容器安装的目录。

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: 现在,我正在使用Gitlab分支,并使用GDK (Gitlab开发套件),并在安装过程中运行以下命令:

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

This command will create a directory inside the project called gitlab 该命令将在项目内部创建一个名为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! 提示:所有安装步骤均在Dockerfile中进行了描述,因此一切都在容器内运行!

So is there a workaround or another way of doing that? 那么,有没有解决方法或另一种方法?

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. 当主机上文件的权限和所有权(通常为UID)与容器本身内部使用的权限和所有权不匹配时,就会发生主机卷的权限问题(将装入的目录绑定到容器中)。 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. 另一个可能的问题是使用适用于Windows的Docker或适用于Mac的Docker,以及使用未与嵌入式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. 默认情况下,这两个产品中的/ Users与VM共享。

Note that this issue is fairly typical and the reason I try to use named volumes using docker's "local" driver when possible. 请注意,此问题相当典型,也是我尝试尽可能使用docker的“本地”驱动程序使用命名卷的原因。 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 (eg a simple busybox container running a tar -xzf to update the contents). 命名卷将初始化为映像的内容,包括文件权限,并且您可以通过使用单独的管理容器来管理它们,该管理容器会为需要进行的任何更改安装相同的卷(例如,运行tar -xzf的简单busybox容器更新内容)。


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM