简体   繁体   English

Dotnet watch 在 docker-compose 中失败

[英]Dotnet watch failing in docker-compose

I have recently had to update docker (2.2.0.0 stable 42247) and now I am having issues running dotnet watch in a docker container using docker-compose .我最近不得不更新 docker(2.2.0.0 稳定版 42247),现在我在使用docker-compose在 docker 容器中运行dotnet watch时遇到问题。

My workflow is to mount the solution directory (on the host) onto a docker container that runs dotnet watch on the relevant project, the initial version looked a little like so:我的工作流程是将解决方案目录(在主机上)挂载到在相关项目上运行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"]

and it would run successfully.它会成功运行。 After the update I get the following error:更新后,我收到以下错误:

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

This output along with my temporary workaround suggest that it is something to do with not being able to figure out what the cwd or pwd is rather than an issue with dotnet watch.此输出以及我的临时解决方法表明,这与无法弄清楚 cwd 或 pwd 是什么有关,而不是 dotnet watch 的问题。 The work around looks like so:解决方法如下:

./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"]

run:跑:

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

then run:然后运行:

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
...

additionally running something like另外运行类似的东西

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

ENTRYPOINT ["dotnet", "fsx"]

will yield a very similar error会产生非常相似的错误

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

I am not using the particular version and have seen github issue posts saying to drop your version to 2.1 something.我没有使用特定版本,并且看到 github 问题帖子说要将您的版本降低到 2.1 某些东西。 However if anyone else comes along here is a 3.1.404 version that works that might point you in the right direction.但是,如果其他人出现,这里有一个 3.1.404 版本,它可以为您指明正确的方向。

Dockerfile.dev 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"]

And the compose file和撰写文件

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

A little walk through, this docker-compose also lets you have https cert working locally if you would like that.稍微介绍一下,如果您愿意,此 docker-compose 还可以让您在本地使用 https 证书。 the Build: dockerfile section is just the path to my dockerfile, while the context is the root of the project. Build: dockerfile 部分只是我的 dockerfile 的路径,而上下文是项目的根。

The volumes

  • ./:/app - all contents in the context are now a volume mount in the container at /app which is the workindir as declared in the Dockerfile ./:/app - 上下文中的所有内容现在都是 /app 容器中的卷挂载,这是 Dockerfile 中声明的 workindir
  • the {APPDATA} volume is where the secret is stored locally I will show how to make that as well {APPDATA} 卷是本地存储秘密的地方,我还将展示如何制作它
  • {USERPROFILE} I cant particularly remember at this moment but its related to the secret {USERPROFILE} 我现在记不清了,但它与秘密有关

The --no-launch-profile worked for me so I kept it over the --no-restore like I have seen in other project examples that failed me. --no-launch-profile对我--no-launch-profile ,所以我把它保留在--no-restore就像我在其他让我失败的项目示例中看到的那样。

I couldn't find the post on how to get the https to work but if you would like me to edit this response and add it in please leave a comment and I will.我找不到有关如何使 https 工作的帖子,但如果您希望我编辑此回复并将其添加进来,请发表评论,我会的。 I left it out since it is not related to the question.我省略了它,因为它与问题无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM