Kiksen
Kiksen

Reputation: 1767

Cant access my Docker DotNet core website

I have run in to a dead end. I have a dotnet core 1.0.0 app I am trying to get up and running. It works great from linux and from windows. Now I am trying to get it into docker. I have made this docker file:

FROM microsoft/dotnet:1.0.0-preview2-sdk

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

ENTRYPOINT ["dotnet", "run"] 

It simply just copies code into app folder in docker image and restore dependencies and then runs it. When I try to run it, it starts up as everything is working and prints same as it was on windows or linux start up.

Docker Console

Command for running the project:

docker run --name dotNetWeb -p 8080:8080 kiksen1987/dotnetcore

Link to Code: https://github.com/kiksen1987/dotnetcore

Link to Docker image: https://hub.docker.com/r/kiksen1987/dotnetcore/

I really have no idea what is going wrong. I have more or less taking the same approach as 99% of my other projects.

Any feedback to improve this question would be nice aswell :)

Upvotes: 6

Views: 6934

Answers (3)

mrbitzilla
mrbitzilla

Reputation: 398

For newer .NET Core frameworks (such as 3.1 in this moment) this setting is on the launchSettings.json

"commandName": "Project",
  "launchBrowser": true,
  "applicationUrl": "https://localhost:5001;http://localhost:5000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"

Upvotes: 1

Ha Hoang
Ha Hoang

Reputation: 1684

You can find some useful information for build an custom image with ASP.NET Core and Docker at here: https://hahoangv.wordpress.com/2016/05/23/asp-net-core-run-in-docker/

Dockerfile:

FROM microsoft/dotnet:1.0.0-preview2-sdk

# Set environment variables
ENV ASPNETCORE_URLS="http://*:5000"
ENV ASPNETCORE_ENVIRONMENT="Development"

# Copy files to app directory
COPY . /app

# Set working directory
WORKDIR /app

# Restore NuGet packages
RUN ["dotnet", "restore"]

# Open up port
EXPOSE 5000

# Run the app
ENTRYPOINT ["dotnet", "run"]

and the Program.cs file:

public static void Main(string[] args)
        {
            // Get environment variables
            var config = new ConfigurationBuilder()
                .AddEnvironmentVariables("")
                .Build();
            // You need to add these lines for accessing outside of Docker
            var url = config["ASPNETCORE_URLS"] ?? "http://*:5000";
            var env = config["ASPNETCORE_ENVIRONMENT"] ?? "Development";

            var host = new WebHostBuilder()
                .UseKestrel()
                .UseUrls(url)
                .UseEnvironment(env)
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }

Hope this help!

Upvotes: 0

Kiksen
Kiksen

Reputation: 1767

Finally.

I found this blog post: http://dotnetliberty.com/index.php/2015/11/26/asp-net-5-on-aws-ec2-container-service-in-10-steps/

Even though it used an old version of the dotnet core there was an important point I had overseen;

Notice that I’ve provided an extra parameter to the dnx web command to tell it to serve on 0.0.0.0 (rather than the default localhost). This will allow our web application to serve requests that come in from the port forwarding provided by Docker which defaults to 0.0.0.0.

Which is pretty damn important.

Solution:

var host = new WebHostBuilder()
            .UseKestrel()
            .UseStartup<Startup>()
            .UseUrls("http://0.0.0.0:5000")
            .Build();

Old code:

var host = new WebHostBuilder()
            .UseKestrel()
            .UseStartup<Startup>()
            .UseUrls("http://localhost:5000")
            .Build();

Which is wery frustrating since it seemed to work perfect in Linux and windows and app starting up in Docker, but never getting any requests. Hope this helps some other poor souls out there :)

Upvotes: 16

Related Questions