簡體   English   中英

Dotnet watch 在 docker-compose 中失敗

[英]Dotnet watch failing in docker-compose

我最近不得不更新 docker(2.2.0.0 穩定版 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

我沒有使用特定版本,並且看到 github 問題帖子說要將您的版本降低到 2.1 某些東西。 但是,如果其他人出現,這里有一個 3.1.404 版本,它可以為您指明正確的方向。

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 中聲明的 workindir
  • {APPDATA} 卷是本地存儲秘密的地方,我還將展示如何制作它
  • {USERPROFILE} 我現在記不清了,但它與秘密有關

--no-launch-profile對我--no-launch-profile ,所以我把它保留在--no-restore就像我在其他讓我失敗的項目示例中看到的那樣。

我找不到有關如何使 https 工作的帖子,但如果您希望我編輯此回復並將其添加進來,請發表評論,我會的。 我省略了它,因為它與問題無關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM