【问题标题】:Write Permissions in Docker Volume在 Docker 卷中写入权限
【发布时间】:2018-07-02 13:14:31
【问题描述】:

我创建了一个 docker 卷:

docker volume create my-volume-name

在 docker 容器中运行的控制台应用程序中,我使用如下创建配置:

{
  "HostConfig": {
    "Binds": [
      "my-volume-name:/app/my-volume-name:rw"
    ],
    "Privileged": true
  }
}

容器看到该卷,但没有任何写入权限。
它给了我一个权限被拒绝的错误。 (下面的例外是我试图在卷中创建一个文件)。

        //create a file
        try{
        string path = Path.Combine(Directory.GetCurrentDirectory(), "my-volume-name","example.txt");
        Console.WriteLine($"PATH {path}");
        if (!File.Exists(path)){
            File.Create(path);
            TextWriter tw = new StreamWriter(path);
            tw.WriteLine($"{DateTime.Now.ToString()}The very first line!");
            tw.Close();
        }else if (File.Exists(path)){
            using(var tw = new StreamWriter(path, true)){tw.WriteLine($"{DateTime.Now.ToString()}The next line!");}
        }
        }catch(Exception e){Console.WriteLine($"ERROR:{e.ToString()}");}

这是我得到的:

 Access to the path '/app/my-volume-name/example.txt' is denied. ---> System.IO.IOException: Permission denied

为了能够写入使用docker volume create 命令创建的卷,我缺少什么?这里的目标是在主机上拥有一个共享卷,我创建的某些容器可以对其进行读写。

编辑(附加的 dockerfile):

FROM microsoft/dotnet:2.0-runtime-stretch AS base

RUN apt-get update && \
    apt-get install -y --no-install-recommends unzip procps && \
    rm -rf /var/lib/apt/lists/*

RUN useradd -ms /bin/bash moduleuser
USER moduleuser


RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg

FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Debug -o out

FROM base
WORKDIR /app
COPY --from=build-env /app/out ./

#moving down because CI/CD builds in VSTS didnt like two copies next to each other.

ENTRYPOINT ["dotnet", "ResourceModule.dll"]

当我在容器中ls -l 时,我看到了这个:

drwxr-xr-x 3 root root    4096 Jul  2 13:28 my-volume-name

编辑 2: 当我在 dockerfile 中删除 moduleuser 的创建时,出现此错误:

ERROR:System.IO.IOException: The process cannot access the file '/app/my-volume-name/example.txt' because it is being used by another process.   

但它是被创造出来的——所以看起来我走在正确的道路上。

【问题讨论】:

    标签: c# docker asp.net-core


    【解决方案1】:

    docker volume create my-volume-name 将创建具有 root 权限的空间。请检查您的应用正在与哪个用户一起运行的容器。更新卷的所有权或使用 root 用户运行应用程序。如需进一步帮助,请提供申请时使用的dockerfile。

    【讨论】:

    • 我已将上面的 dockerfile 添加到问题中,并附加到末尾。这是 IOT Edge 的一部分,moduleuser 是他们项目构建脚手架的一部分。我可以尝试删除它。
    • 我注释掉了 moduleuser 的创建,所以看起来容器现在以 root 身份运行——而且看起来事情确实按预期工作。文件创建成功,我的代码中有一些错误 - 但它让我感动。我会将您的答案标记为正确 - 谢谢 Rohit。
    • 不客气,杰森。也请接受答案。如果您想使用moduleuser,请告诉我。
    猜你喜欢
    • 2018-11-24
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 2020-08-07
    • 2019-08-12
    • 1970-01-01
    • 2019-05-30
    相关资源
    最近更新 更多