12

I've created a app in asp.net core and create a dockerfile to generate a local image and run it.

FROM microsoft/dotnet:latest

COPY . /app

WORKDIR /app

RUN ["dotnet", "restore"]

RUN ["dotnet", "build"]

EXPOSE 5000/tcp

ENTRYPOINT ["dotnet", "run", "--server.urls", "http://0.0.0.0:5000"]

Then, I've builded my image with the following command

docker build -t jedidocker/first .

Then, I've created a container with the following command

docker run -t -d -p 5000:5000 jedidocker/first

But when I run the following url into my host browser

http://localhost:5000/

I have an ERR_EMPTY_RESPONSE

is it a network problem? How can I solve it?

P.S: My windows version is 10.0.10586

4 Answers 4

24

I had the same issue and found the solution to this. You will need to change the default listening hostname. By default, the app will listen to the localhost, ignoring any incoming requests from outside the container. Change your code in program.cs to listen for all incoming calls:

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseUrls("http://*:5000")
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

More info:

https://medium.com/trafi-tech-beat/running-net-core-on-docker-c438889eb5a#.hwhoak2c0

Make sure you are running the container with the -p flag with the port binding.

docker run -p 5000:5000 <containerid>

At the time of this writing, it seems that the

EXPOSE 5000:5000

command does not have the same effect as the -p command. I haven't had real success with -P (upper case) either.

Edit 8/28/2016:

One thing to note. If you are using docker compose with the -d (detached) mode, it may take a little while for the dotnet run command to launch the server. Until it's done executing the command (and any other before it), you will receive ERR_EMPTY_RESPONSE in Chrome.

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

2 Comments

It works! Just increasing answer details, usually, when you create an app, this setting is set to http://localhost:8080 or something like it. It works well in a local environment, but, if you, like mine, is trying to run it from a docker container, localhost is another machine (inside container), so, you have to accept all requests outside or set it to accept the host ip address (machine running docker engine). So, open it to web, use *:port :)
That only works if you're only going to run the app locally in docker, because the port number is hard-coded and is not the same for the other profiles defined in launchSettings.json
8

Just update your entry point as follows, no code change is required:

ENTRYPOINT ["dotnet", "run", "--server.urls", "http://*:5000"]

The default Docker network on Windows Server 2016 is NAT, so you may want to use the IP which you can find out with docker inspect <container name/ID>. Not sure if this is the case with other Windows versions as well.

3 Comments

Balint, could you explain why this works? It fixed my problem, but I'm not sure I understand why.
@tylerjgarland it will make dotnet listen on all IP addresses (interfaces) not just localhost (loopback)
As of May 2018 VS2017 Preview sends this information as an environment variable: -e "ASPNETCORE_URLS=https://+:443;http://+:80" -e "ASPNETCORE_HTTPS_PORT=44341"
5

Here's what worked for me in ASP.NET 3.1

On the Dockerfile You need to specify the ENV ASPNETCORE_URLS

ENV ASPNETCORE_URLS http://*:5000

then EXPOSE the PORT

EXPOSE 5000

Comments

0

Use HostUrl as an enviroment variable

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "HostUrl": "http://*:8080"
}

program.cs

public class Program
{
    public static void Main(string[] args)
    {
        var hosturl = configuration.GetSection("HostUrl").Value;
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseUrls(hosturl)
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

Then, you can override HostUrl as an environment argument on docker run

docker run --rm -it -e HostUrl="http://*:80" -e DOTNET_RUNNING_IN_CONTAINER=true -e ConnectionStrings__DefaultConnection="<your_connection>" -p 80:80 <containerid>

Open browser at localhost an be happy!

Comments

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.