How to run an ASP.NET Core 3.1 project in a Docker container with Visual Studio 2019?
Asked Answered
T

2

9

I created a Web API project based on an ASP.NET Core 3.1 framework. Later, I decided to deploy it on a Linux instead of a Windows VPS. Before I deploy the app on a Linux VPS, I want to be able to run in a docker container locally using Visual Studio 2019 on Windows 7.

I installed docker toolbox for windows on my machine along with Oracle VM VirtualBox.

Since my project was not created with Docker support, I right-clicked on my project >> Add >> "Add Docker" Support which created the Dockerfile.

Now when I try to build/debug my app on Visual Studio, I get the following error

Error   CTC1003 Visual Studio container tools require Docker to be running. ProjectName C:\Users\MyUsername\.nuget\packages\microsoft.visualstudio.azure.containers.tools.targets\1.9.10\build\Container.targets    198 

I started Docker by clicking on "Docker Quickstart Terminal". I am also able to verify that the default virtual-box is running. Also, I added the "C_Drive" as a shared folder on the default VM.

How can I correctly start/debug my solution with docker?

Here the content of my Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

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

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

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

I also tried to add docker-compose support by right-clicking on my project >> Add >> Add Container Orchestraintor Support which docker-compose project.

When I run the app with Docker Composer the app runs Visual Studio turns orange for debugging state" and immediately stops. The following is what I get in the Debug window

-------------------------------------------------------------------
You may only use the Microsoft .NET Core Debugger (vsdbg) with
Visual Studio Code, Visual Studio or Visual Studio for Mac software
to help you develop and test your applications.
-------------------------------------------------------------------
  It was not possible to find any installed .NET Core SDKs
  Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from:
      https://aka.ms/dotnet-download
The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core.
The program 'dotnet' has exited with code 145 (0x91).

Updated 1

Here is what Visual Studio prints under the "Build" output

1>------ Build started: Project: MyProject, Configuration: Debug Any CPU ------
1>MyProject -> C:\MyProjects\MyProject\MyProject\bin\Debug\netcoreapp3.1\MyProject.dll
1>docker run -dt -v "C:\Users\MyUsername\vsdbg\vs2017u5:/remote_debugger:rw" -v "C:\MyProjects\MyProject\MyProject:/app" -v "C:\MyProjects\MyProject:/src" -v "C:\Users\MyUsername\AppData\Roaming\Microsoft\UserSecrets:/root/.microsoft/usersecrets:ro" -v "C:\Users\MyUsername\AppData\Roaming\ASP.NET\Https:/root/.aspnet/https:ro" -v "C:\Users\MyUsername\.nuget\packages\:/root/.nuget/fallbackpackages2" -v "C:\Program Files\dotnet\sdk\NuGetFallbackFolder:/root/.nuget/fallbackpackages" -e "DOTNET_USE_POLLING_FILE_WATCHER=1" -e "ASPNETCORE_ENVIRONMENT=Development" -e "ASPNETCORE_URLS=https://+:443;http://+:80" -e "NUGET_PACKAGES=/root/.nuget/fallbackpackages2" -e "NUGET_FALLBACK_PACKAGES=/root/.nuget/fallbackpackages;/root/.nuget/fallbackpackages2" -P --name MyProject --entrypoint tail myproject:dev -f /dev/null
1>docker: Error response from daemon: invalid mode: /root/.nuget/fallbackpackages.
1>See 'docker run --help'.
1>C:\Users\MyUsername\.nuget\packages\microsoft.visualstudio.azure.containers.tools.targets\1.9.10\build\Container.targets(198,5): error CTC1003: Visual Studio container tools require Docker to be running.
1>Done building project "MyProject.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Here is what VS prints under the "Container Tools" output

========== Checking for Container Prerequisites ==========
Verifying that Docker Desktop is installed...
Docker Desktop is installed.
========== Verifying that Docker Desktop is running... ==========
Verifying that Docker Desktop is running...
Visual Studio container tools require Docker to be running.
Docker Desktop is not running.
========== Finished ==========

Updated 2

As per Wiebe Tijsma answer below, I updated my docker-composer.yml file to the following

version: '3.4'

services:
  myproject:
    image: ${DOCKER_REGISTRY-}myproject
    build:
      context: .
      dockerfile: MyProject/Dockerfile
    volumes:
      - photos:/app/Storage

volumes:
  photos:

The build now seems to run fine, but the run fails. When running the app using docker-composer, Visual studio throws this error

Launching failed because directory '/remote_debugger' in the container is empty. This might be caused by Shared Drives credentials used by Docker Desktop being out of date. Try resetting the credentials in the Shared Drives page of the Docker Desktop Settings and then restart Docker

enter image description here

Here is the most recent build output from Visual Studio

1>------ Build started: Project: MyProject, Configuration: Debug Any CPU ------
1>MyProject -> C:\MyProjects\MyProject\MyProject\bin\Debug\netcoreapp3.1\MyProject.dll
2>------ Build started: Project: docker-compose, Configuration: Debug Any CPU ------
2>docker-compose  -f "C:\MyProjects\MyProject\docker-compose.yml" -f "C:\MyProjects\MyProject\docker-compose.override.yml" -f "C:\MyProjects\MyProject\obj\Docker\docker-compose.vs.debug.g.yml" -p dockercompose15694150546677200279 --no-ansi up -d
2>Creating network "dockercompose15694150546677200279_default" with the default driver
2>Creating MyProject ...
2>Creating MyProject ... done
========== Build: 2 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Tout answered 3/1, 2020 at 0:9 Comment(5)
@panoskarajohn that instructions seem to be for VS Code not VS Studio. Is there something, in particular, your advising to try? I have added the debug image but still getting the same error without helpful info curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l /vsdbgTout
can you launch other docker containers like docker run hello-world ?Baras
@Baras yes, it show Hello from Docker! This message shows that your installation appears to be working correctly.Tout
@John does this help -> #45870266 ?Aristaeus
I presume you checked that C:\Users\MyUsername\vsdbg\vs2017u5 actually exists and has enough permissions for docker service user to access it for read-write?Romanticism
M
3

In my case, the error occurred because I also mapped a volume to /app, which caused the entrypoint + all dotnet published files to be unavailable (beginners mistake :)).

myapp:
  container_name: myapp
  image: watch/myapp
  volumes:
    - ./testdata/:/app #<-- wrong
Milliner answered 7/1, 2020 at 14:2 Comment(3)
That seems to have made an improvement as the build is working now but the run fails. Please check the section under Update 2 in the question. I added the results of adding the volume as per your suggestion.Tout
I did the same and you just saved me from banging my head to the wallKevel
I guess the dockerfile must be updated in order to fit with the volumes :/app path ?Demetrademetre
B
1

You can run docker-compose and start your project like so:

services:
    xxxx.service:
    image: ${DOCKER_REGISTRY}orderservice
    build:
      context: .
      dockerfile: Dockerfile`

Here is the location of docker-composer

enter image description here

Busily answered 3/1, 2020 at 10:44 Comment(2)
I added Container Orchestraintor Support to my project which created the docker-compose file. The project runs and stops right away. I get the following error in the debug window The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core. The program 'dotnet' has exited with code 145 (0x91).Tout
check your dcoker image ,clear the exist image of this projectBusily

© 2022 - 2024 — McMap. All rights reserved.