如何在没有Visual Studio且没有docker compose的情况下使用docker复制csproj文件? [英] How to copy a csproj file using docker without visual studio and without docker compose?
问题描述
我刚刚使用Visual Studio 15.8.8通过.NET Core项目(2.1)开始了新的解决方案。通过将docker compose文件设置为启动项目,它可以运行和调试它。
I just started a new solution with a .NET Core Project (2.1) using visual studio 15.8.8. It can run and debug it by setting the docker compose file as a startup project. It works!
从逻辑上讲,我应该能够使用简单的命令行语句来构建docker镜像。但是,它抱怨找不到csproj。这很奇怪。该文件存在,并且正如我所告诉的,我可以从Visual Studio中运行它。我从一个目录和具有dockerfile的目录尝试了它。同样的问题。
Logically, I should be able to build the docker image with a simple commandline statement. However, it complains that the csproj cannot be found. This is strange. The file exist and as I told, I can run it from visual studio. I tried it from one directory up and the directory that has the dockerfile. Same problem.
我该如何解决?我唯一想要的就是简单地构建映像,然后仅使用docker命令运行它。
How can I solve this? The only thing I want is simply build my image and then run it by just using docker commands.
Dockerfile
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["TryNewDocker2/TryNewDocker2.csproj", "TryNewDocker2/"]
RUN dotnet restore "TryNewDocker2/TryNewDocker2.csproj"
COPY . .
WORKDIR "/src/TryNewDocker2"
RUN dotnet build "TryNewDocker2.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "TryNewDocker2.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TryNewDocker2.dll"]
Het是撰写文件:
version: '3.4'
services:
trynewdocker2:
image: ${DOCKER_REGISTRY}trynewdocker2
build:
context: .
dockerfile: TryNewDocker2/Dockerfile
从逻辑上讲,我希望 docker-compose up保持不变
Logically, I want "docker-compose up" to keep working when fixing this problem.
推荐答案
这是由dockerfile中文件路径的错误根文件夹引起的。
This is caused by the wrong root folder for the file path in dockerfile.
要从 Docker
启动,其根文件夹为 C:\Users\ ... \repos\TryNewDocker2
,但是从命令
运行时,其根目录为 C:\用户-... \repos\TryNewDocker2\TryNewDocker2
,因此 TryNewDocker2.csproj
的路径已从 TryNewDocker2 / TryNewDocker2.csproj
到 TryNewDocker2.csproj
For launching from Docker
, its root folder is C:\Users\...\repos\TryNewDocker2
, but while running from command
, its root fodler is C:\Users\...\repos\TryNewDocker2\TryNewDocker2
, so the path for TryNewDocker2.csproj
has changed from TryNewDocker2/TryNewDocker2.csproj
to TryNewDocker2.csproj
尝试以下dockerfile:
Try dockerfile below:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 59162
EXPOSE 44342
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["TryNewDocker2.csproj", "TryNewDocker2/"]
RUN dotnet restore "TryNewDocker2/TryNewDocker2.csproj"
COPY . ./TryNewDocker2/
WORKDIR "/src/TryNewDocker2"
RUN dotnet build "TryNewDocker2.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "TryNewDocker2.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TryNewDocker2.dll"]
更新
要在Docker和命令中都可以使用,请不要更改dockerfile,请从下面的路径通过指定dockerfile路径运行命令。
For working in both Docker and command, do not change your dockerfile, and from path below to run your command with specifying the dockerfile path.
C:\Users\...\repos\TryNewDocker2>docker build -t gogo -f TryNewDocker2/Dockerfile .
这篇关于如何在没有Visual Studio且没有docker compose的情况下使用docker复制csproj文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!