user1765862
user1765862

Reputation: 14155

Cannot open dockerized .net core api on custom port

docker-compose up 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.

Problem here is that I cannot open my web api app on this specified port

http://localhost:5000 

returns Could not get any response from a postman

Locally I am able to open this app through visual studio debug.

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

I am not able to get any log output from the api container

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.

Edit: I tried to change Dockerfile to expose port 5000 instead of 80

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

and on docker ps -a I'm getting

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

Still having the same problem, cannot get response from api on localhost:5000

Upvotes: 0

Views: 211

Answers (2)

Thomasleveil
Thomasleveil

Reputation: 103985

In your docker-compose.yml file, the myapi.sample.api service runs a process which gives the following log:

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

which means that your process is listening on port 5000 bound to the container IP address.

You also used the ports: directive to publish a port opened within the container on another port opened on the docker host. Remember that each container has its own IP address which is different that the one of your docker host. In the docker world, publishing a port means opening a port on the docker host which is connected to a port of a container.

The ports: directive syntax is:

HOST:CONTAINER

or in other words:

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

Since you seem to be wanting to connect to port 5000 of your container by connecting to port 5000 of your docker host, the ports: directive should be :

    ports:
      - 5000:5000

Upvotes: 1

jgoday
jgoday

Reputation: 2836

Seems that you are mix the ports in the composer file.

It should not be 80:5000 ?

Either specify both ports (HOST:CONTAINER), or just the container port (an ephemeral host port is chosen).

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

Upvotes: 1

Related Questions