gpt4 book ai didi

visual-studio - 如何使用 Visual Studio 2019 在 Docker 容器中运行 ASP.NET Core 3.1 项目?

转载 作者:行者123 更新时间:2023-12-03 12:18:41 26 4
gpt4 key购买 nike

我创建了一个基于 ASP.NET Core 3.1 框架的 Web API 项目。后来,我决定将其部署在 Linux 上而不是 Windows VPS 上。在 Linux VPS 上部署应用程序之前,我希望能够在 Windows 7 上使用 Visual Studio 2019 在本地的 docker 容器中运行。

我安装了 docker toolbox for windows在我的机器上以及 Oracle VM VirtualBox 上。

由于我的项目不是使用 Docker 支持创建的,我右键单击我的项目 >> 添加 >>“添加 Docker”支持,它创建了 Dockerfile .

现在,当我尝试在 Visual Studio 上构建/调试我的应用程序时,出现以下错误

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 

我通过单击“Docker Quickstart Terminal”启动了 Docker。我还能够验证 default虚拟盒正在运行。此外,我在默认 VM 上添加了“C_Drive”作为共享文件夹。

如何使用 docker 正确启动/调试我的解决方案?

这里是我的内容 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"]

我也尝试添加 docker-compose支持通过右键单击我的项目 >> 添加 >> 添加容器 Orchestraintor 支持其中 docker-compose项目。

当我使用 Docker Composer 运行应用程序时应用程序运行 Visual Studio 变为橙色以进行调试状态”并立即停止。以下是我在调试窗口中得到的内容
-------------------------------------------------------------------
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).

更新 1

这是 Visual Studio 在“构建”输出下打印的内容
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 ==========

这是 VS 在“Container Tools”输出下打印的内容
========== 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 ==========

更新 2

根据 Wiebe Tijsma下面的回答,我更新了我的 docker-composer.yml文件到以下
version: '3.4'

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

volumes:
photos:

构建现在似乎运行良好,但运行失败。使用 docker-composer 运行应用程序时, Visual Studio 抛出这个错误

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

这是 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 ==========

最佳答案

就我而言,发生错误是因为我还将一个卷映射到/app,这基本上导致所有入口点 + 所有 dotnet 发布的文件都不可用(初学者错误:))。

myapp:
container_name: myapp
image: watch/myapp
volumes:
- ./testdata/:/app #<-- wrong

关于visual-studio - 如何使用 Visual Studio 2019 在 Docker 容器中运行 ASP.NET Core 3.1 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59571633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com