docker.net-coredockerfile.net-core-rc2

Running .NET Core RC2 project on a defined port in Docker


I created an empty .NET Core RC2 project and running locally using Visual Studio 2015, and it works.

Now I want to deploy it to a Linux server using Docker - so how should my dockerfile look? I have been following these instructions, and this is what I ended up with:

FROM microsoft/dotnet:1.0.0-preview1

COPY . /app
WORKDIR /app

RUN dotnet restore

EXPOSE 5004
ENTRYPOINT dotnet run

then I built my app to image:

docker build -t my_app .

and run using:

docker run -t -p 8080:5004 my_app

After that I got information that image is running and it's listening on localhost:5000. Unfortunately I have been trying to connect to this server with xxxx:5000, xxxx:5004 and xxxx:8080 and none of those addresses worked (xxxx being the server address).

Am I doing something wrong?


Solution

  • You can tell kestrel which port to listen on by using the UseUrls() extension method, like this:

    (this generally goes in the Program.Main() entry point method for me)

    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseUrls("http://0.0.0.0:5004")
        .Build();
    
    host.Run();
    

    In this case, you would run the docker image like this:

    $ docker run -d -p 8080:5004 my_app
    

    I chose the -d option to run as a daemon. Just make sure that the EXPOSED port in your Dockerfile matches the one specified in UseUrls. For a complete example of this, feel free to look at my github sample project: https://github.com/mw007/adventure-works