1

When I try to run my ASP.NET Core application on docker through http://localhost:5004 I'm getting this response from my browser: ERR_CONNECTION_REFUSED

Here's my docker file

# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:5.0.404 AS build


WORKDIR /code

COPY . .

# copy everything else and build app

RUN dotnet publish -c release -o /app 

# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:5.0.13

WORKDIR /app
COPY --from=build /app ./

ENTRYPOINT ["dotnet", "ProductCatalogApi.dll", "--server.urls", "http://+:5004"]

my docker-compose.yml file (do not mind the password format):

version: "5.0.4"

networks:
   frontend:
   backend:

services:
   catalog:
      build:
         context: .\src\Services\ProductCatalogApi
         dockerfile: Dockerfile
      image: shoes/catalog
      environment:
         - DatabaseServer=mssqlserver
         - DatabaseName=CatalogDb
         - DatabaseUser=sa
         - DatabasePassword=(passwordhere)
         - ASPNETCORE_URLS=http://+:5004
         - ASPNETCORE_ENVIRONMENT=Production
      container_name: catalogapi
      ports:
         - "5004:80"
      networks:
         - backend
         - frontend
      depends_on:
         - mssqlserver
         
   mssqlserver:
      image: "mcr.microsoft.com/mssql/server:2019-latest"
      ports:
         - "1445:1433"

      container_name: mssqlcontainer
      environment:
         - ACCEPT_EULA=Y
         - SA_PASSWORD=(passwordhere)
         - MSSQL_PID=Developer
      networks:
         - backend

  

CreateHostBuilder in Program.cs:

        private static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    // webBuilder.UseKestrel(options => { options.Listen(IPAddress.Any, 5000); });
                    webBuilder.UseKestrel().UseUrls(Environment.GetEnvironmentVariable("ASPNETCORE_URLS"));
                });
    }
}

I can't figure what's missing or wrong on my configuration that I can't access the app on my local machine through localhost:5004(or whatever port ex. 5000 )

Note: I run this commands in the following order

  1. docker-compose build
  2. docker-compose up mssqlserver
  3. docker-compose up catalog

1 Answer 1

2

When you set ASPNETCORE_URLS=http://+:5004, your app will listen on port 5004. So that's the port you should map to a port on the host. You've mapped port 80.

Change your docker-compose file to

  ports:
     - "5004:5004"

Now you should be able to access it. Remember that Swagger by default isn't available in the Production environment, so you won't be able to use the Swagger pages.

You've tried to configure what port the app listens on in a lot of different ways. A good idea might be to remove it all and only configure it in the launchSettings.json file. Then it'll listen on the port specified in launchSettings.json when you run it locally during development and it'll listen on port 80 when run in the container.

The reason it'll listen on port 80 when run in a container is that Microsoft set the ASPNETCORE_URLS environment variable to http://+:80 in the aspnet images.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice it works now, honestly I forgot that swagger only runs on development(at least in my configuration), It could've already been working earlier, I was checking against the swagger url. But yeah, I'll take a look on how I could simplify setting the ports.

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.