0

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

3
  • Can you show your logs of the 'myapi.sample.api' container ? Commented Mar 16, 2019 at 19:43
  • For some reason I am not able to get log output from my container. Locally it works. Commented Mar 16, 2019 at 19:48
  • Maybe a stupid question, but is the container running ? (docker ps -a) Commented Mar 16, 2019 at 19:50

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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/

5 Comments

I'm not sure about this cause I'm able to access sql server on containerized port 1445 using same approach. I still tried to switch to 80:5000 but its still the same problem.
Dockerfile should expose 5000 to, isn't it ?
Tried that to expose 5000 instead of 80, remove image, rebuild it again but still the same problem. Question udpated.
make it - "5000:5000"
@Thomasleveil that actually work. Can you please elaborate a bit and post as an answer I'll accept it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.