Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot run asp.net 5 from docker

I have followed the following guide: Running ASP.NET 5 applications in Linux Containers with Docker and I cannot get this to work on my Windows PC or Linux server. My dockerfile looks like this:

FROM microsoft/aspnet

COPY . /app
WORKDIR /app
RUN ["dnu", "restore"]

EXPOSE 5000/tcp
ENTRYPOINT ["dnx", "-p", "project.json", "web"]

I then ran docker build -t myapp . and then docker run -d -p 80:5000 myapp it says it is running but I cannot open the website in the browser. I know on Windows you are supposed to find the ip address that the actual virtual machine is running against by using docker-machine ip default which ended up being 192.168.99.100 but when I navigated to http://192.168.99.100 I just get the generic "This webpage is not available" error message. I have also tried different variations of this docker run command, such as docker run -it -p 80:5000 myapp, docker run -p 80:5000 myapp, and I have also tried different ports, such as docker run -d -p 5000:5000 myapp but nothing seems to work.

I have tried this both on my windows machine and on my linux server, but they both do not work.

I am able to run dnx web without docker and everything works as expected.

like image 468
girlcode Avatar asked Nov 28 '15 23:11

girlcode


People also ask

Can ASP.NET run in Docker?

By default, Docker runs on port 80 with ASP.NET Core, but you can override that. In the example below, the Kestrel server that will run in the container is being configured to listen on port 5000. The other environment variable is simply specifying our environment, which is development in this case.

Does .NET support Docker?

NET 6; you can use Docker containers that include the traditional . NET Framework. However, a recommended approach is to use . NET 6 as you extend an existing application, such as writing a new service in ASP.NET Core.


3 Answers

Take a look at my answer here: ASP.NET 5.0 beta 8 in Docker doesn't start

Essentially, Docker is forwarding requests to your container on the 0.0.0.0 network interface, but Kestrel is only listening on localhost by default.

So yes, the requests are being passed off to your docker container, but they are not being accepted by the Kestrel webserver. For that reason, you need to override the server.urls property as others have posted:

ENTRYPOINT ["dnx", "web", "--server.urls", "http://0.0.0.0:5000"]

You should then see:

Now listening on: http://0.0.0.0:5000

when running your container. You can also do a quick docker ps command to verify that 0.0.0.0 is in fact the network interface that Docker is forwarding requests for.

I also wrote a bit about how to get ASP.NET 5 running on Docker on Windows - it's a bit more involved since not only does Docker have to forward requests to the container, but we have to get VirtualBox to pass off requests to the Docker virtual machine boot2docker (typically called default in Virtual Box) before Docker can hand them off to our container.

Post is here: http://dotnetliberty.com/index.php/2015/10/25/asp-net-5-running-in-docker-on-windows/

like image 52
armen.shimoon Avatar answered Sep 23 '22 23:09

armen.shimoon


For a more complete understanding of your app environment, please post your project.json file and the beta version of ASP.net you are working with.

For now you can try cleaning up your Dockerfile by taking out "project.json" and "-p" arguments from the ENTRYPOINT instruction, remove tcp from the EXPOSE command, and finally, specify the "--server.urls" argument in the ENTRYPOINT instruction so that it uses 0.0.0.0 instead of the default localhost as follows:

FROM microsoft/aspnet

COPY . /project        
WORKDIR /project       
RUN ["dnu", "restore"]

EXPOSE 5000

ENTRYPOINT ["dnx", "web", "--server.urls"]

Alternatively, you can try dropping the EXPOSE command altogether and expose the docker port, 5000, in the ENTRYPOINT instruction as follows:

FROM microsoft/aspnet

COPY . /project        
WORKDIR /project       
RUN ["dnu", "restore"]

ENTRYPOINT ["dnx", "web", "--server.urls", "http://0.0.0.0:500"]

Either way you would then build your container and run it using something like the following:

$ docker run -it -p 80:5000 myapp
like image 38
trm5073 Avatar answered Sep 22 '22 23:09

trm5073


For anyone having this issue now in RC2, commands no longer exists. You have to update Program.cs by chaining in .UseUrls("http://0.0.0.0:5000"). You can also change from 5000 to whatever your desired port is here.

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

        host.Run();
    }
}
like image 30
Joe Avatar answered Sep 24 '22 23:09

Joe