How to copy a csproj file using docker without visual studio and without docker compose?

Refresh

April 2019

Views

219 time

1

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!

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.

How can I solve this? The only thing I want is simply build my image and then run it by just using docker commands.

Screenshot of commandline

Commandline with directory itself 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 is the compose file:

version: '3.4'

services:
  trynewdocker2:
    image: ${DOCKER_REGISTRY}trynewdocker2
    build:
      context: .
      dockerfile: TryNewDocker2/Dockerfile

Logically, I want "docker-compose up" to keep working when fixing this problem.

1 answers

3

This is caused by the wrong root folder for the file path in dockerfile.

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

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"]

Update

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 .