Skip to content

.NET Core 新建 Docker 支持项目但是无法启动的问题

新建一个 .NET Core API 项目并且使用 Docker,但是启动 docker-compose 项目时总是报错。

txt
========== 正在准备容器 ==========
正在准备 Docker 容器...
docker-compose  -f "D:\work\Middleware\2_SourceCode\dev\net\octopus\docker-compose.yml" -f "D:\work\Middleware\2_SourceCode\dev\net\octopus\docker-compose.override.yml" -f "D:\work\Middleware\2_SourceCode\dev\net\octopus\obj\Docker\docker-compose.vs.debug.g.yml" -p dockercompose5563428382167899646 --no-ansi config
The DOCKER_REGISTRY variable is not set. Defaulting to a blank string.
services:
  octopus:
    build:
      context: D:\work\Middleware\2_SourceCode\dev\net\octopus
      dockerfile: octopus/Dockerfile
      target: base
    entrypoint: tail -f /dev/null
    environment:
      ASPNETCORE_ENVIRONMENT: Development
      DOTNET_USE_POLLING_FILE_WATCHER: '1'
      NUGET_FALLBACK_PACKAGES: /root/.nuget/fallbackpackages
    image: octopus:dev
    labels:
      com.microsoft.visualstudio.debuggee.arguments: ' --additionalProbingPath /root/.nuget/packages
        --additionalProbingPath /root/.nuget/fallbackpackages  bin/Debug/netcoreapp2.1/octopus.dll'
      com.microsoft.visualstudio.debuggee.killprogram: /bin/bash -c "if PID=$$(pidof
        -x dotnet); then kill $$PID; fi"
      com.microsoft.visualstudio.debuggee.program: dotnet
      com.microsoft.visualstudio.debuggee.workingdirectory: /app
    ports:
    - target: 80
    volumes:
    - /d/work/Middleware/2_SourceCode/dev/net/octopus/octopus:/app:rw
    - /c/Users/liujiajia/vsdbg/vs2017u5:/remote_debugger:ro
    - /c/Program Files/dotnet/sdk/NuGetFallbackFolder:/root/.nuget/fallbackpackages:ro
    - /c/Users/liujiajia/.nuget/packages:/root/.nuget/packages:ro
version: '3.4'
docker ps --filter "status=running" --filter "name=dockercompose5563428382167899646_octopus_" --format {{.ID}} -n 1
docker-compose  -f "D:\work\Middleware\2_SourceCode\dev\net\octopus\docker-compose.yml" -f "D:\work\Middleware\2_SourceCode\dev\net\octopus\docker-compose.override.yml" -f "D:\work\Middleware\2_SourceCode\dev\net\octopus\obj\Docker\docker-compose.vs.debug.g.yml" -p dockercompose5563428382167899646 --no-ansi build 
The DOCKER_REGISTRY variable is not set. Defaulting to a blank string.
Building octopus
Step 1/3 : FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
 ---> 84e946977e6f
Step 2/3 : WORKDIR /app
 ---> Using cache
 ---> ec0ac0017bcb
Step 3/3 : EXPOSE 80
 ---> Running in 0af5744be080
Removing intermediate container 0af5744be080
 ---> 865a37e2ba37
Successfully built 865a37e2ba37
Successfully tagged octopus:dev
docker-compose  -f "D:\work\Middleware\2_SourceCode\dev\net\octopus\docker-compose.yml" -f "D:\work\Middleware\2_SourceCode\dev\net\octopus\docker-compose.override.yml" -f "D:\work\Middleware\2_SourceCode\dev\net\octopus\obj\Docker\docker-compose.vs.debug.g.yml" -p dockercompose5563428382167899646 --no-ansi up -d --no-build --force-recreate --remove-orphans
The DOCKER_REGISTRY variable is not set. Defaulting to a blank string.
Creating network "dockercompose5563428382167899646_default" with the default driver
Creating dockercompose5563428382167899646_octopus_1 ... 
Creating dockercompose5563428382167899646_octopus_1 ... done
完成!Docker 容器已准备就绪。
========== 调试 ==========
docker ps --filter "status=running" --filter "name=dockercompose5563428382167899646_octopus_" --format {{.ID}} -n 1
ba7bee30e308
docker inspect --format="{{json .NetworkSettings.Ports}}" ba7bee30e308
{"80/tcp":[{"HostIp":"0.0.0.0","HostPort":"32772"}]}

调试信息:

txt
-------------------------------------------------------------------
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.
-------------------------------------------------------------------
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
[http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409](http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409)
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 '[24] dotnet' has exited with code 145 (0x91).
程序“”已退出,返回值为 145 (0x91)。

再次对照了之前的博客 使用 VS 2017 新建 Docker 支持项目,除了之前创建的是 Web 项目,这次创建的是 API 项目之外,没有什么不同的地方。
本以为是环境坏掉了,但是打开之前的工程是可以正常通过 Docker 调试的。

试了半天终于发现:


必须将工程创建在 windows 的用户目录(C:\Users\yourusername)下,才能通过 vs 直接 debug docker 项目。


why?

猜测是因为我在 windows 下装的 docker,默认是把当前用户的目录当做系统根目录了。