简体   繁体   English

无法测试部署到 Docker 的简单 Asp.net 核心 Web api

[英]Cannot test a simple Asp.net core web api deployed to Docker

I'm new to Docker and really want to experience its features.我是 Docker 的新手,真的很想体验它的功能。 Here I would like to run a simple Asp.net core web api in Docker (using docker run ) instead of running it using IIS.在这里,我想在 Docker 中运行一个简单的 Asp.net core web api(使用docker run ),而不是使用 IIS 运行它。

It's simple because it's exactly the default auto-gen ASP.NET core Web API project with only one controller named ValuesController .这很简单,因为它正是默认的自动生成 ASP.NET 核心 Web API 项目,只有一个名为ValuesController控制器。 Normally when debugging with IISExpress, the following URI should respond an array of values:通常在使用 IISExpress 进行调试时,以下 URI 应响应一组值:

http://localhost:[some_port]/api/values

Now I add Docker support for the project (using Windows container).现在我为项目添加 Docker 支持(使用 Windows 容器)。 After building the Docker image, it can be listed using docker images .构建 Docker 镜像后,可以使用docker images列出它。 Now I run the Docker image to host my web api like this:现在我运行 Docker 镜像来托管我的 web api,如下所示:

docker run -t -rm -p 80:50633 hellodocker:dev

It runs OK and I can check that using docker ps .它运行正常,我可以使用docker ps检查。 However to test if it's actually working I've tried typing the following address into a browser:但是为了测试它是否真的有效,我尝试在浏览器中输入以下地址:

http://localhost/api/values

and it's not working, nothing displayed and it looked just like a non-existent site.它不起作用,没有显示任何内容,它看起来就像一个不存在的站点。

When I try the following command docker exec [container_id] netstat , it sometimes shows a record with status of TIME_WAIT and almost the time there is not any.当我尝试以下命令docker exec [container_id] netstat ,它有时会显示状态为TIME_WAIT的记录,并且几乎没有任何时间。 Although I'm not sure if this relates to the outside listening.虽然我不确定这是否与外部聆听有关。

Here is the Dockerfile's content:这是 Dockerfile 的内容:

FROM microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-1803 AS base
WORKDIR /app
EXPOSE 50633
EXPOSE 44322

FROM microsoft/dotnet:2.2-sdk-nanoserver-1803 AS build
WORKDIR /src
COPY HelloDocker/HelloDocker.csproj HelloDocker/
RUN dotnet restore HelloDocker/HelloDocker.csproj
COPY . .
WORKDIR /src/HelloDocker
RUN dotnet build HelloDocker.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish HelloDocker.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "HelloDocker.dll"]

I'm really stuck at this.我真的被困在这一点上。 A hello-world app may just print out a simple string but this hellodocker should be hosted in docker and serve any HTTP requests just like when we host it in IIS.一个 hello-world 应用程序可能只打印出一个简单的字符串,但这个hellodocker应该托管在hellodocker中,并像我们在 IIS 中托管它一样处理任何 HTTP 请求。

Update更新

After trying removing the built image and rebuilding another one instead, it looks like different after running with the same docker run command above:在尝试删除构建的映像并重建另一个映像后,使用上述相同的docker run命令运行后,它看起来有所不同:

Hosting environment: Production
Content root path: C:\app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.

As before nothing displayed and the prompt root path becomes C:\\app .和以前一样,没有显示任何内容,提示根路径变为C:\\app So this time it looks more obvious that it's listening for requests.所以这一次看起来更明显的是它正在侦听请求。 However it's still not working.但是,它仍然无法正常工作。

Before your ENTRYPOINT statement, try adding在您的 ENTRYPOINT 语句之前,尝试添加

 ENV ASPNETCORE_URLS=http://+:50633 DOTNET_RUNNING_IN_CONTAINER=true 

so it looks like this:所以它看起来像这样:

FROM base AS final WORKDIR /app COPY --from=publish /app . ENV ASPNETCORE_URLS=http://+:50633 DOTNET_RUNNING_IN_CONTAINER=true ENTRYPOINT ["dotnet", "HelloDocker.dll"]

Does that help?这有帮助吗?

I am not sure why I could not search for a solution from Docker's forums but after trying searching for Now listening on: http://[::]:80 and it led me to an at-very-top link to a very similar issue in Docker's forums here .我不知道为什么我无法从 Docker 的论坛中搜索解决方案,但是在尝试搜索Now listening on: http://[::]:80 ,它让我找到了一个非常相似的链接Docker 论坛中的问题

So I've tried one solution there by using docker inspect [container_id] first to find the container's IP and used that IP to access the web API successfully :)所以我在那里尝试了一种解决方案,首先使用docker inspect [container_id]来查找容器的 IP 并使用该 IP 成功访问 Web API :)

The container's IP can be found in the Networks section:容器的 IP 可以在Networks部分找到:

"Networks": {
            "nat": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": null,
                "NetworkID": "e336becc4500435f7338ebd8f84fef47ec2fc247f77bc82b6dbf49553f68afd5",
                "EndpointID": "a8ede68e3f4bb02392c295901f65d50f0ee014c114f564768d9c82a4644b0218",
                "Gateway": "172.30.240.1",
                "IPAddress": "172.30.242.85",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "00:15:5d:5a:29:8d",
                "DriverOpts": null
            }
        }

The correct address I used to test the web api now is:我现在用来测试web api的正确地址是:

http://172.30.242.85/api/values

UPDATE :更新

I assumed the EXPOSE 50633 means the docker will expose web api via port 50633 so I run the command docker run -t -p 80:50633 hellodocker:dev .我假设EXPOSE 50633意味着EXPOSE 50633将通过端口50633公开 web api,所以我运行命令docker run -t -p 80:50633 hellodocker:dev

But actually the port it exposes here is still the default 80 (not sure why).但实际上它在这里公开的端口仍然是默认的80 (不知道为什么)。 But at least that makes sense.但至少这是有道理的。 So to map the ports between docker and the host machine we use the command docker run -t -p:80:80 hellodocker:dev instead.因此,为了映射 docker 和主机之间的端口,我们使用命令docker run -t -p:80:80 hellodocker:dev代替。 I've tried that and it works perfectly for http://localhost/api/values .我已经试过了,它非常适合http://localhost/api/values

I think the command used before docker run -t -p:80:50633 hellodocker:dev has useless ports mapping (at least for testing the web api from host machine), because the http://172.30.242.85/api/values should work if the docker actually runs the web api to listen on the default port 80 .我认为在docker run -t -p:80:50633 hellodocker:dev之前使用的命令docker run -t -p:80:50633 hellodocker:dev用的端口映射(至少对于从主机测试 web api),因为http://172.30.242.85/api/values应该如果 docker 实际运行 web api 以侦听默认端口80

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

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