本文介绍了Docker dotnet watch 运行错误:无法在 IPv6 环回接口上绑定到 https://localhost:5000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所示,我有一个无法从主机端口绑定到容器端口的容器.我尝试搜索类似的问题,但没有发现任何与在 docker 容器中使用 dotnet watch 相关的问题,因为 Microsoft 引入了 microsoft/dotnet docker repo,在 sdk 映像中内置了 dotnet watch.

As the title indicates, i have a container that is unable to bind from host port to container port. I tried searching for similar issues, but have not found any related to using dotnet watch in a docker container since Microsoft introduced the microsoft/dotnet docker repo with dotnet watch built into the sdk image.

任何关于我做错了什么的建议都非常感谢.

Any suggestions as to what i am doing wrong are much appreciated.

Dockerfile

FROM microsoft/dotnet:2.1.301-sdk as build
ENV DOTNET_USE_POLLING_FILE_WATCHER 1
WORKDIR /app
COPY . .
RUN dotnet restore
EXPOSE 5000-5001
ENTRYPOINT [ "dotnet", "watch", "run", "--no-restore"]

docker-compose.yml

version: "3"

services:

  esportapp:
    container_name: esportapp
    image: esportapp:dev
    build:
      context: .
      dockerfile: Docker/dev.Dockerfile
    volumes:
      - esportapp.volume:/app
    ports:
      - "5000:5000"
      - "5001:5001"

volumes:
  esportapp.volume:

完全错误:

esportapp    | Hosting environment: Development
esportapp    | Content root path: /app
esportapp    | Now listening on: https://localhost:5001
esportapp    | Now listening on: http://localhost:5000
esportapp    | Application started. Press Ctrl+C to shut down.
esportapp    | warn: Microsoft.AspNetCore.Server.Kestrel[0]
esportapp    |       Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'.
esportapp    | warn: Microsoft.AspNetCore.Server.Kestrel[0]
esportapp    |       Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.

推荐答案

我自己刚遇到这个问题.我认为 dotnet watch run 不能很好地与 localhost 类型的 url 配合使用.尝试在您的容器中将您的托管网址设置为 https://0.0.0.0:5000.

Just ran into this problem myself. I don't think dotnet watch run plays nicely with localhost type urls. Try setting your hosting url to https://0.0.0.0:5000 in your container.

在 dockerfile 中:

In the dockerfile with:

ENTRYPOINT [ "dotnet", "watch", "run", "--no-restore", "--urls", "https://0.0.0.0:5000"]

或在 launchSettings.json 中,例如:

Or in launchSettings.json like:

{
  "profiles": {
    "[Put your project name here]": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "DOTNET_USE_POLLING_FILE_WATCHER": "true"
      },
      "applicationUrl": "https://0.0.0.0:5000/"
    }
  }
}

现在要让它从容器内自动重新加载,您必须使用轮询文件观察器.这就是第二个环境变量的用途.(这很常见,你必须使用 webpack、angular 等来做到这一点).

Now to get it to automatically reload from within the container you have to use the polling file watcher. That's what the second environment variable is for. (This is pretty common, you've got to do this with webpack, angular, etc).

在您的情况下,您需要将 esportsapp.volume 更改为主机上的目录:

In your case, you need to change the esportsapp.volume to a directory on your host:

volumes:
  - ./:/app

这会将容器中的/app 卷映射到 docker-compose 目录.您面临的问题是该应用程序构建在项目默认 docker-compose 网络上的卷中,因此当您更改源目录中的文件时,该卷中的文件实际上并未发生更改.但是,通过此修复程序,您将遇到容器内的 dotnet restore 和 dotnet watch 更改主机文件的问题.如果您有兴趣,可以修复所有这些问题...

That will map the /app volume in your container to the docker-compose directory. The problem you're facing is that the app is built in a volume on your project's default docker-compose network, so when you change a file in the source directory, it's not actually changing in that volume. With this fix, however, you'll run into the problem of the dotnet restore and dotnet watch inside the container changing your host's files. There's a fix for all of that, if you're interested...

我通常的 .Net Core 应用 Docker 设置

要调试,请运行:docker-compose -f run.yml up --build

构建一个版本:docker-compose -f build.yml up --build

项目结构

/                                               # source control root
/build.yml                                      # docker-compose file for building a release
/run.yml                                        # docker-compose file for running locally & debugging
/project                                        # an application
/project/build.Dockerfile                       # the docker container that will build "project" for release
/project/run.Dockerfile                         # the docker container that will build and run "project" locally for debugging
/project/.dockerignore                          # speeds up container builds by excluding large directories like "packages" or "node_modules"
/project/src                                    # where I hide my source codez
/project/src/Project.sln
/project/src/Project/Project.csproj
/project/src/Project/Directory.Build.props      # keeps a docker mapped volume from overwriting .dlls on your host
/project/src/Project.Data/Project.Data.csproj   # typical .Net project structure
/web-api                                        # another application...

Directory.Build.props(将它与您的 .csproj 放在同一文件夹中,防止您的 dotnet watch run 命令与主机上的源目录混淆)

Directory.Build.props (put this in the same folder as your .csproj, keeps your dotnet watch run command from messing with the source directory on your host)

<Project>

  <PropertyGroup>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(MSBuildProjectDirectory)/obj/**/*</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(MSBuildProjectDirectory)/bin/**/*</DefaultItemExcludes>
  </PropertyGroup>

  <PropertyGroup Condition="'$(DOTNET_RUNNING_IN_CONTAINER)' == 'true'">
    <BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/obj/container/</BaseIntermediateOutputPath>
    <BaseOutputPath>$(MSBuildProjectDirectory)/bin/container/</BaseOutputPath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(DOTNET_RUNNING_IN_CONTAINER)' != 'true'">
    <BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/obj/local/</BaseIntermediateOutputPath>
    <BaseOutputPath>$(MSBuildProjectDirectory)/bin/local/</BaseOutputPath>
  </PropertyGroup>

</Project>

run.yml(用于调试的 docker-compose.yml)

run.yml (docker-compose.yml for debugging)

version: "3.5"
services:
  project:
    build:
      context: ./project
      dockerfile: run.Dockerfile
    ports:
      - 5000:80
    volumes:
      - ./project/src/Project:/app

run.Dockerfile(用于调试的 Dockerfile)

run.Dockerfile (the Dockerfile for debugging)

FROM microsoft/dotnet:2.1-sdk

# install the .net core debugger
RUN apt-get update
RUN apt-get -y --no-install-recommends install unzip
RUN apt-get -y --no-install-recommends install procps
RUN rm -rf /var/lib/apt/lists/*

RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l /vsdbg

VOLUME /app
WORKDIR /app

CMD dotnet watch run --urls http://0.0.0.0:80

build.yml(用于构建发布版本的 docker-compose.yml)

build.yml (the docker-compose.yml for building release versions)

version: "3.5"
services:
  project:
    build:
      context: ./project
      dockerfile: build.Dockerfile
    volumes:
      - ./project:/app

build.Dockerfile(用于构建发布版本的 Dockerfile)

build.Dockerfile (the Dockerfile for building release versions)

FROM microsoft/dotnet:2.1-sdk

VOLUME /app

# restore as a separate layer to speed up builds
WORKDIR /src
COPY src/Project/Project.csproj .
RUN dotnet restore

COPY src/Project/ .
CMD dotnet publish -c Release -o /app/out/

这篇关于Docker dotnet watch 运行错误:无法在 IPv6 环回接口上绑定到 https://localhost:5000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 07:32