将Docker卷挂载到主机路径

时间:2018-11-23 12:58:24

标签: docker dockerfile

我需要从容器访问主机中的测试结果文件。我知道我需要创建一个在主机和容器之间映射的卷,如下所示,但是我什么也没写到主机上。

docker run --rm -it -v <host_directory_path>:<container_path> imagename

Dockerfile:

FROM microsoft/dotnet:2.1-sdk AS builder
WORKDIR /app
COPY ./src/MyApplication.Program/MyApplication.Program.csproj ./src/MyApplication.Program/MyApplication.Program.csproj
COPY nuget.config ./
WORKDIR ./src/MyApplication.Program/
RUN dotnet restore
WORKDIR /app
COPY ./src ./src
WORKDIR ./src/MyApplication.Program/
RUN dotnet build MyApplication.Program.csproj -c Release

FROM builder as tester
WORKDIR /app
COPY ./test/MyApplication.UnitTests/MyApplication.UnitTests.csproj ./test/MyApplication.UnitTests/MyApplication.UnitTests.csproj
WORKDIR ./test/MyApplication.UnitTests/
RUN dotnet restore
WORKDIR /app
COPY ./test ./test
WORKDIR ./test/MyApplication.UnitTests/
RUN dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura
ENTRYPOINT ["dotnet", "reportgenerator", "-reports:coverage.cobertura.xml", "-targetdir:codecoveragereports", "-reportTypes:htmlInline"]

入口点的命令正常运行。它将输出写入MyApplication.UnitTests/codecoveragereports目录,而不写入主机目录。

我的docker run如下:

docker run --rm -it -v /codecoveragereports:/app/test/MyApplication.UnitTests/codecoveragereports routethink.tests:latest

我可能做错了什么?

1 个答案:

答案 0 :(得分:1)

看起来像是权限问题。

-v /codecoveragereports:/app/***/codecoveragereports正在根/下装载目录,这很危险,您可能没有权限。

最好在本地安装,例如-v $PWD/codecoveragereports:/app/***/codecoveragereports,其中$PWD是等于当前工作目录的环境变量。