【发布时间】: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