【问题标题】:Dotnet watch failing in docker-composeDotnet watch 在 docker-compose 中失败
【发布时间】:2020-05-08 21:48:46
【问题描述】:

我最近不得不更新 docker(2.2.0.0 stable 42247),现在我在使用 docker-compose 的 docker 容器中运行 dotnet watch 时遇到问题。

我的工作流程是将解决方案目录(在主机上)挂载到在相关项目上运行 dotnet watch 的 docker 容器上,初始版本看起来有点像这样:

./docker-compose.yml

version: "3.2"
services:
  my-api:
    build: 
      context: $MY_API_LOCATION_ON_HOST # the folder with the .sln file and all projects
      dockerfile: $DOCKERFILE_ROOT/Dockerfile-aspcore-dev
      args:
        PROJECT: app/src/MyApi # Where the .csproj will be
    volumes:
      - type: bind
        source: $MY_API_LOCATION_ON_HOST # the folder with the .sln file and all projects
        target: /app
      - type: bind
        source: $SECRETS_FOLDER # C:\Users\DevUser\AppData\Roaming\Microsoft\UserSecrets
        target: /root/.microsoft/usersecrets

./Dockerfile-aspcore-dev

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS base
ARG PROJECT
WORKDIR ${PROJECT}

ENTRYPOINT ["dotnet", "watch", "run", "--urls", "http://0.0.0.0:5000"]

它会成功运行。更新后出现以下错误:

docker-compose up --build --force-recreate my-api
Building my-api
Step 1/4 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS base
 ---> 2fe8fe202baf
Step 2/4 : ARG PROJECT
 ---> Using cache
 ---> cee4bd05745b
Step 3/4 : WORKDIR ${PROJECT}
 ---> Using cache
 ---> ed12fa68fc7e
Step 4/4 : ENTRYPOINT ["dotnet", "watch", "run", "--urls", "http://0.0.0.0:5000"]
 ---> Using cache
 ---> 6c0b8b95cd8e
Successfully built 6c0b8b95cd8e
Successfully tagged src_my-api:latest
Recreating src_my-api_1 ... done
Attaching to src_my-api_1
my-api_1                 | System.IO.FileNotFoundException: Unable to find the specified file.
my-api_1                 |    at Interop.Sys.GetCwdHelper(Byte* ptr, Int32 bufferSize)
my-api_1                 |    at Interop.Sys.GetCwd()
my-api_1                 |    at System.Environment.get_CurrentDirectory()
my-api_1                 |    at System.IO.Directory.GetCurrentDirectory()
my-api_1                 |    at Microsoft.DotNet.CommandFactory.CommandResolver.TryResolveCommandSpec(ICommandResolverPolicy 
commandResolverPolicy, String commandName, IEnumerable`1 args, NuGetFramework framework, String configuration, String outputPath, 
String applicationName)
my-api_1                 |    at Microsoft.DotNet.CommandFactory.CommandFactoryUsingResolver.Create(ICommandResolverPolicy commandResolverPolicy, String commandName, IEnumerable`1 args, NuGetFramework framework, String configuration, String outputPath, String applicationName)
my-api_1                 |    at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient)
my-api_1                 |    at Microsoft.DotNet.Cli.Program.Main(String[] args)
src_my-api_1 exited with code 1

此输出以及我的临时解决方法表明,这与无法弄清楚 cwd 或 pwd 是什么有关,而不是 dotnet watch 的问题。解决方法如下:

./docker-compose.yml

version: "3.2"
services:
  my-api:
+    tty: true
+    stdin_open: true
    working_dir: /app/src/MyApi # Where the .csproj will be
    build: 
      context: $MY_API_LOCATION_ON_HOST
      dockerfile: $DOCKERFILE_ROOT/Dockerfile-aspcore-dev
    volumes:
      - type: bind
        source: $MY_API_LOCATION_ON_HOST # the folder with the .sln file and all projects
        target: /app
      - type: bind
        source: $SECRETS_FOLDER # C:\Users\DevUser\AppData\Roaming\Microsoft\UserSecrets
        target: /root/.microsoft/usersecrets

./Dockerfile-aspcore-dev

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS base
ARG PROJECT
WORKDIR ${PROJECT}

- ENTRYPOINT ["dotnet", "watch", "run", "--urls", "http://0.0.0.0:5000"]

运行:

docker-compose up --build --force-recreate search-api
Building search-api
Step 1/3 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS base
 ---> 2fe8fe202baf
Step 2/3 : ARG PROJECT
 ---> Using cache
 ---> cee4bd05745b
Step 3/3 : WORKDIR ${PROJECT}
 ---> Using cache
 ---> ed12fa68fc7e
Successfully built ed12fa68fc7e
Successfully tagged src_search-api:latest
Recreating src_search-api_1 ... done
Attaching to src_search-api_1

然后运行:

docker exec -it src_my-api_1 dotnet "watch" "run" "--urls" "http://0.0.0.0:5000"
watch : Polling file watcher is enabled
watch : Started
watch : Exited
watch : File changed: /app/src/MyApi/Constants.cs
watch : Started
...

另外运行类似

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS base
ARG PROJECT
WORKDIR ${PROJECT}

ENTRYPOINT ["dotnet", "fsx"]

会产生非常相似的错误

docker-compose up --build --force-recreate my-api
Building my-api
Step 1/4 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS base
 ---> 2fe8fe202baf
Step 2/4 : ARG PROJECT
 ---> Using cache
 ---> cee4bd05745b
Step 3/4 : WORKDIR ${PROJECT}
 ---> Using cache
 ---> ed12fa68fc7e
Step 4/4 : ENTRYPOINT ["dotnet", "fsx"]
 ---> Running in eaa49fc2294c
Removing intermediate container eaa49fc2294c
 ---> f2f4f212d0e9
Successfully built f2f4f212d0e9
Successfully tagged src_my-api:latest
Recreating src_my-api_1 ... done
Attaching to src_my-api_1
my-api_1                 | System.IO.FileNotFoundException: Unable to find the specified file.
my-api_1                 |    at Interop.Sys.GetCwdHelper(Byte* ptr, Int32 bufferSize)
my-api_1                 |    at Interop.Sys.GetCwd()
my-api_1                 |    at System.Environment.get_CurrentDirectory()
my-api_1                 |    at System.IO.Directory.GetCurrentDirectory()
my-api_1                 |    at Microsoft.DotNet.CommandFactory.CommandResolver.TryResolveCommandSpec(ICommandResolverPolicy 
commandResolverPolicy, String commandName, IEnumerable`1 args, NuGetFramework framework, String configuration, String outputPath, 
String applicationName)
my-api_1                 |    at Microsoft.DotNet.CommandFactory.CommandFactoryUsingResolver.Create(ICommandResolverPolicy commandResolverPolicy, String commandName, IEnumerable`1 args, NuGetFramework framework, String configuration, String outputPath, String applicationName)
my-api_1                 |    at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient)
my-api_1                 |    at Microsoft.DotNet.Cli.Program.Main(String[] args)
src_my-api_1 exited with code 1

【问题讨论】:

  • 我们观察到同样的错误,只能通过在 Windows 主机上从 2.2.0.0 降级到 2.1.0.5 来解决它
  • 我在 Windows 10 上遇到了同样的问题。从 2.2.0.0 降级到 2.1.05 也修复了我的错误。

标签: .net docker .net-core docker-compose


【解决方案1】:

我没有使用特定版本,并且看到 github 问题帖子说将您的版本降低到 2.1 的东西。但是,如果其他人出现在这里,它可能会为您指明正确的方向。

Dockerfile.dev

FROM mcr.microsoft.com/dotnet/core/sdk:3.1.404-buster
WORKDIR /app

COPY *.sln ./
COPY *.csproj ./
RUN dotnet restore

ENTRYPOINT ["dotnet","watch","run","--no-launch-profile"]

还有撰写文件

version: "3.8"
services:
    aspnet_service:
        container_name: aspnet_dev_container
        restart: always
        hostname: api
        build:
          context: .
          dockerfile: ./Docker/api/Dockerfile.dev
        environment:
          - ASPNETCORE_URLS=https://+;http://+
          - ASPNETCORE_HTTPS_PORT=5001
          - ASPNETCORE_ENVIRONMENT=Development
        ports:
          - target: 80
            published: 5000
            protocol: tcp
            mode: host
      - target: 443
        published: 5001
        protocol: tcp
        mode: host
        volumes:
          - ./:/app
          - ${APPDATA}\microsoft\UserSecrets\:/root/.microsoft/usersecrets
          - ${USERPROFILE}\.aspnet\https:/root/.aspnet/https/
        working_dir: /app
        stdin_open: true
        tty: true
        command: dotnet watch run --no-launch-profile

如果你愿意的话,这个 docker-compose 还可以让你在本地运行 https 证书。 Build: dockerfile 部分只是我的 dockerfile 的路径,而上下文是项目的根目录。

  • ./:/app - 上下文中的所有内容现在都是容器中 /app 的卷挂载,这是 Dockerfile 中声明的工作目录
  • {APPDATA} 卷是本地存储秘密的地方,我将展示如何制作它
  • {USERPROFILE} 我现在记不太清了,但它与秘密有关

--no-launch-profile 为我工作,所以我将它保留在 --no-restore 上,就像我在其他失败的项目示例中看到的那样。

我找不到有关如何使 https 工作的帖子,但如果您希望我编辑此回复并将其添加,请发表评论,我会的。我把它省略了,因为它与问题无关。

【讨论】:

    猜你喜欢
    • 2021-07-06
    • 2022-01-16
    • 1970-01-01
    • 2020-09-07
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    相关资源
    最近更新 更多