无法在自定义端口上打开dockerized .net核心api

时间:2019-03-16 19:39:41

标签: .net docker .net-core

docker-组成myapp.sample.api

Creating network "myapp_frontend" with the default driver
myapp-sample-mssql-cnr is up-to-date
Creating myapp-sample-api-cnr ... done
Attaching to myapp-sample-api-cnr
myapp-sample-api-cnr       | warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
myapp-sample-api-cnr       |       No XML encryptor configured. Key {daa358c7-8c64-4e3c-90ea-06f48de12ad4} may be persisted to storage in unencrypted form.
myapp-sample-api-cnr       | Hosting environment: Development
myapp-sample-api-cnr       | Content root path: /app
myapp-sample-api-cnr       | Now listening on: http://[::]:5000
myapp-sample-api-cnr       | Application started. Press Ctrl+C to shut down.

这里的问题是我无法在此指定的端口上打开我的Web api应用程序

http://localhost:5000 

返回无法获得邮递员的任何答复

  

在本地,我可以通过Visual Studio调试打开此应用程序。

docker-compose.yml

version: '3.4'

networks:
    frontend:
    backend:

services:
    myapi.sample.api:        
        build:
            context: .
            dockerfile: MyApi.Sample.API/Dockerfile
        image: myappsampleapi-img          
        container_name: myapp-sample-api-cnr    
        ports:
            - "5000:80"
        networks:
            - backend
            - frontend
        depends_on:
            - mssqlserver

    mssqlserver:
        image: "microsoft/mssql-server-linux:latest"
        ports:
            - "1445:1433"

        container_name: myapp-sample-mssql-cnr        
        networks:
            - backend

Dockerfile

FROM microsoft/dotnet:2.2-aspnetcore-runtime as base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ...
...
ENTRYPOINT ["dotnet", "MyApi.Sample.API.dll"]

我无法从api容器中获取任何日志输出

docker logs -f myapp-sample-api-cnr

warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {82dd75ea-8302-4859-a3e5-78eedf856e34} may be persisted to storage in unencrypted form.
Hosting environment: Development
Content root path: /app
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.

编辑: 我试图更改Dockerfile以公开端口5000而不是80

FROM microsoft/dotnet:2.2-aspnetcore-runtime as base
WORKDIR /app
EXPOSE 5000

在docker ps -a上获取

CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS              PORTS                                                   NAMES
964724837d13         myappsampleapi-img                   "dotnet MyApi.S…"   45 seconds ago      Up 43 seconds       5000/tcp, 0.0.0.0:5000->80/tcp, 0.0.0.0:32788->80/tcp   myapp-sample-api-cnr

仍然有相同的问题,无法从localhost:5000上的api获得响应

2 个答案:

答案 0 :(得分:1)

似乎您在混合作曲家文件中的端口。

不应该是80:5000吗?

  

要么指定两个端口(HOST:CONTAINER),要么仅指定容器端口(选择临时主机端口)。

https://docs.docker.com/compose/compose-file/

答案 1 :(得分:1)

在您的docker-compose.yml文件中,myapi.sample.api服务运行一个进程,该进程提供以下日志:

...
Now listening on: http://[::]:5000

这意味着您的进程正在监听绑定到容器IP地址的端口5000

您还使用了ports:指令将在容器内打开的端口发布到在 docker主机上打开的另一个端口上。请记住,每个容器都有自己的IP地址,该IP地址与您的Docker主机的IP地址不同。在docker世界中,发布端口表示在docker主机上打开与容器端口相连的端口

ports:指令语法为:

HOST:CONTAINER

或换句话说:

<port to open on the docker host>:<port already opened within the container>

由于您似乎想通过连接到Docker主机的端口5000来连接到容器的端口5000,因此ports:指令应为:

    ports:
      - 5000:5000