28

I am new to Docker. I created a Web API using ASP.Net Core using Visual Studio 2019 as well as in VS Code. It works fine. Then I added docker support and added Dockerfile with default values.

When I try to build the docker image, it fails in Visual Studio 2019 as well as in VS Code.

However, If I try to run the Docker image using the Visual Studio 2019 provided option (where I can select docker as run), then the image gets created. But when I run the build command in Visual Studio 2019 or VS Code i.e.

docker build -f ./Dockerfile --force-rm -t mytestapp:dev ..
it throws following error<br>
 => ERROR [build 3/7] COPY [myTestApp.csproj, ./]  
Content of my docker file is given below
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["myTestApp.csproj", "./"]
RUN dotnet restore "myTestApp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "myTestApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "myTestApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "myTestApp.dll"]

The project structure picture is also attached:

structure

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
Shahid Riaz Bhatti
  • 381
  • 1
  • 4
  • 6
  • Could you give us a picture of your project structure directories? Seem you basiclly run the command in a wrong context. – Bao To Quoc Apr 05 '21 at 03:14

9 Answers9

46

A simple docker build command cannot work with the default Dockerfiles created by Visual Studio because the paths are specified relative to the root of the solution, and not the root of the project.

You can inspect the build output from VS to determine how it builds the image (simplified version):

docker build 
  -f "PROJECT_PATH\Dockerfile" 
  -t IMAGE_NAME:dev 
  "SOLUTION_PATH"

As you can see, it builds using the Dockerfile in the project folder (-f), but from the solution folder.

I guess they did it because it has the advantage of keeping each Dockerfile in its own project folder, while letting you reference resources outside that folder using more consistent solution-based paths. Apart from that, it's pretty annoying.

You can move the Dockefile to the solution folder and leave it unchanged, but then the Docker features in VS will stop working as expected. Or you can adopt the VS convention and adapt your scripts accordingly.

Mathieu Renda
  • 14,069
  • 2
  • 35
  • 33
  • 2
    I got here searching for a way to do with with `docker-compse`, and you have to have to set the `build:` -> `context: `. so like `build: context: ./project dockerfile: Dockerfile` for a directory structure like ./project/Dockerfile from the root. – gabe Sep 22 '21 at 00:12
  • I was running into the same. This fixed the problem for me. I moved the Docker config to the solution root folder. In my case, Azure DevOps pipelines was complaining not being able to find the csproj file while Docker was sitting in the project folder instead of the solution folder. Using onpremise Agent, Windows 11, .NET 6.0 and VS 2022. – Sha Oct 16 '22 at 11:41
22

Try running the command from the parent folder, you can specify the path to the Dockerfile using the -f flag.

cd ..

docker build -t imagename:tag -f ProjectDir/Dockerfile .

Docker copy's the .csproj and other files from the current location on the host machine, so if you say:

COPY ["myTestApp.csproj", "./"]

Make sure you are in the right directory on the host machine. The Dockerfile created by Docker Support is not always ideal for building images if you use for example other project references but can be a good base.

axtck
  • 3,707
  • 2
  • 10
  • 26
8

Run this from your Solution root:

docker build . -f [ProjectDir]\Dockerfile

online Thomas
  • 8,864
  • 6
  • 44
  • 85
7

Answer from *@axtc*k worked for me. The only change required to make it work was to remove the slash:

cd ..

docker build -t imagename:tag -f ProjectDir/Dockerfile .
ouflak
  • 2,458
  • 10
  • 44
  • 49
Carlos
  • 71
  • 1
  • 2
  • This did the trick for me using an ASP .NET 6 API with the auto generated Docker files, thanks. – MondQ Feb 27 '22 at 16:14
3

Use docker-compose to easily create and tear down your setup.

Step 1: Save code below as docker-compose.yml one directory higher than your Dockerfile (same path as your project's .sln file):

version: '3'
services:
    web:
        build:
            context: .
            dockerfile: [PROJECTNAME]\Dockerfile
        ports:
            - "5000:80"
        networks:
            - aspcore-network
    sql-server:
        image: mcr.microsoft.com/mssql/server
        networks:
            - aspcore-network

networks:
    aspcore-network:
        driver: bridge

Step 2: Add additional services (MYSQL/REDIS/ETC)

Step 3: Open terminal to docker-compose.yml location

Step 4: Run docker-compose build then docker-compose up -d

Step 5: When done run docker-compose down

dKen
  • 3,078
  • 1
  • 28
  • 37
1

Remove the .(dot) you included at WORKDIR "/src/."

Jay
  • 147
  • 2
  • 4
1

I solved this issue by providing the absolute path to the docker command.

Shahid Riaz Bhatti
  • 381
  • 1
  • 4
  • 6
0

Instead, go to the parent directory, with the .sln file and use the docker -f option to specify the Dockerfile to use in the subfolder:

cd \CoreDockerAPI docker build -f CoreDockerAPI\Dockerfile --force-rm -t myfirstimage .

docker run -it myfirstimage

Pritam
  • 41
  • 1
0

Here are the steps I used to solve this problem :

  • I checked Enable Docker while creating my .NET 5 Web API Project.

  • For Docker OS, I chose Linux.

  • Then I opened a terminal, navigated to the directory where my project is and typed the following command : docker build -f Movie.WebAPI\Dockerfile --force-rm -t movie-api:v1 .

Which gave the following results :

As the last step, I ran this command : docker run -it --rm -p 8080:80 movie-api:v1

Which created the container image.

Now movie-api appears when I type docker images.

Besworks
  • 4,123
  • 1
  • 18
  • 34