我创建了一个 docker 卷:
docker volume create my-volume-name
在 Docker 容器中运行的控制台应用程序中,我使用如下创建配置:
{
"HostConfig": {
"Binds": [
"my-volume-name:/app/my-volume-name:rw"
],
"Privileged": true
}
}
容器看到卷,但没有任何写入权限。
它给了我一个权限被拒绝的错误。 (下面的异常(exception)是我尝试在卷中创建文件)。
//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.
但它已经创建了 - 所以看起来我走在正确的道路上。
最佳答案
docker volume create my-volume-name
将创建具有 root 权限的空间。请检查您的应用程序正在与哪个用户一起运行的容器。更新卷的所有权或使用 root 用户运行应用程序。如果需要进一步帮助,请提供应用程序使用的dockerfile。
关于c# - Docker Volume 中的写入权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51137075/