通过关于 ASP.NET 核心的 dockerized API WeatherForecast Web 模板的教程工作:
我必须从这里开始,因为我没有使用这里的教程来构建新图像:https://docs.docker.com/compose/aspnet-mssql-compose/
“1”有效,这很棒。但是,“2”不适用于
localhost:5000/WeatherForecast
端口如宣传的那样,在对可用文档进行了多次审查后,我在调试为什么会遇到一些麻烦。我应该注意,在从命令行创建模板化应用程序时,我确实选择了 --no-https 选项。
然后我使用 docker ps 调出 PORTS 。 Web 应用程序正在使用
5000/tcp, 0.0.0.0:32779->80/tcp
.当我替换 5000
为 32779
,我得到了返回的 API 字符串!我知道我在 docker-compose 中遗漏了一些东西,可以多加注意一下。谢谢!
编辑:作为引用,以下文件是由我的 VSCode 编辑器生成的。
1. 我运行了 dotnet new webapi --no-https
2.然后我调出VSCode“命令面板”并运行
Docker: Add Dockerfiles to Workspace
并选择"is"以包含 docker-compose.yml 文件和 Linux。我也选择使用端口5000
.我使用 Fedora 30。4.我跑
dotnet build
从终端中的项目根目录。5. 如果我从
docker
运行命令并使端口显式,它会像宣传的那样工作,但是如果我运行 docker-compose -f <yml-file> up -d- --build
, 它不会。我只是重新阅读了这篇文章,发现我被困在 VSCode 中来解决这个问题很烦人(根据文档)
By default Docker will assign a randomly chosen host port to a port exposed by a container (the container port). In this case the exposed (container) port is 5000, but it will be exposed on the host via a random port, such as 32737.
You can use specific port on the host by changing the Docker run options used by docker-run: debug task (defined in .vscode/tasks.json file). For example, if you want to use the same port (5000) to expose the service, the docker-run: debug task definition would look like this:
一种。
Dockerfile
# Please refer https://aka.ms/HTTPSinContainer on how to setup an
https developer certificate for your ASP .NET Core service.
version: '3.4'
services:
aspdotnetdocker2:
image: aspdotnetdocker2
build:
context: .
dockerfile: Dockerfile
ports:
- 5000
湾
docker-compose.yml
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 5000
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["aspdotnet_docker2.csproj", "./"]
RUN dotnet restore "./aspdotnet_docker2.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "aspdotnet_docker2.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "aspdotnet_docker2.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "aspdotnet_docker2.dll"]
最佳答案
SHORT SYNTAX
Either specify both ports (HOST:CONTAINER), or just the container port (an ephemeral host port is chosen).
所以尝试:
ports:
- "5000:<port in ASPNETCORE_URLS>"
# e.g.
# - "5000:80"
# - "44388:443"
关于c# - ASP.NET docker-compose 应用程序没有出现在分配的端口上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61493240/