16

net core project. I have successfully build the project. Below is my dockerfile.

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 4040
EXPOSE 5050

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["MWS.AspNetCoreApis/MWS.AspNetCoreApis.csproj", "MWS.AspNetCoreApis/"]
RUN dotnet restore "MWS.AspNetCoreApis/MWS.AspNetCoreApis.csproj"
COPY . .
WORKDIR "/src/MWS.AspNetCoreApis"
RUN dotnet build "MWS.AspNetCoreApis.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "MWS.AspNetCoreApis.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
CMD tail -f /dev/null
ENTRYPOINT ["dotnet", "MWS.AspNetCoreApis.dll"]

I build my application as docker build -t locationservices . Here I build my image. Then when I run my image using docker run -d locationservices it gives some long id. When I try to hit http://localhost:40/swagger/index.html or http://localhost:5050/swagger/index.html my web page doesnt open. When I run >docker run -it locationservices I get below message.

Hosting environment: Production Content root path: /app Now listening on: http://[::]:80 Application started. Press Ctrl+C to shut down.

But I am not able to hit my application using any of the below urls

http://localhost:5050/swagger/index.html
http://localhost:4040/swagger/index.html
http://localhost:80/swagger/index.html

can someone help me to figure out the issue. Any help would be appreciated. Thanks

7 Answers 7

25

I am little late to this question.

But the actual problem is that you are telling docker what ports to expose but they don't match the port that ASP.NET Core is listening on.

You need to add a Environment Variable to the Dockerfile that matches your exposed ports like this.

EXPOSE 4040
ENV ASPNETCORE_URLS=http://*:4040

The last line of the file:

ENTRYPOINT ["dotnet", "myapp.dll"]

Then run the container using -p 4040:4040 that way it maps the port to the "outside" world.

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

2 Comments

Thanks! "you are telling docker what ports to expose but they don't match the port that ASP.NET Core is listening on." Was what I was missing. Everything makes sense now.
why the exposed port does not match? it is 4040 where the apsdotnet core app is running afterall.. Is it because you have to force the app to listen on 4040 (through that env variable)?
9

You can also add docker container run arguments directly into the .csproj file:

<PropertyGroup>
    <DockerfileRunArguments>-p "4040:443" -p "5050:80"</DockerfileRunArguments>
</PropertyGroup>

2 Comments

Finally! This resolved my issue, though for docker it needs to be 4040:4040. Or at least in my case I had to put 8080:8080 and 8081:8081 to map the right ports to locahost.
If you need an UDP connection you can add: -p "4040:4040/udp". For more information read stackoverflow.com/a/27596415/3604523
6

I was putting -p 80:80 after the image name when it has to before!

Wrong and doesn't work: docker run image_name -p 80:80

Correct: docker run -p 80:80 image_name

3 Comments

After almost 30 minutes of debugging, this is what I was doing wrong. All the args (-p , -v) should come before the image name. Thanks!
that is insane!
This is absolutely insane. The errors you get for this are horridly unhelpful. Thank you SO much for this answer.
2

You have to publish ports when running the container so that when you hit localhost:someport the request will be forwarded to the container. This is done by using --publish/-p option when running the container :

docker run -d -p 4040:4040 -p 5050:5050 locationservices

and now you can access localhost:5050/swagger/index.html and localhost:4040/swagger/index.html.

4 Comments

Thanks. I done this and still not working. May be issue with image?
have you exposed port 80 also ? Using -p 80:80 and tried to access localhost:80/app?
yes few times back. after that i deleted image and created new one
I just logged into container and I can see all the dlls.
1

I had similar problem and found solution. You can pass urls parameter to your entrypoint to start app on some certain port. Example: ENTRYPOINT ["dotnet","watch", "run", "--server.urls", "http://0.0.0.0:5050"]

And if you want to see any changes on save just use volumes otherwise you will have to restart it after change.

I hope it will helps you ;)

Comments

0

Inside the container, the app binds to localhost on port 80. However, that's inside the container. When you attempt to hit http://localhost, localhost in that context is your machine, not the container instance. You need to access the container via its IP on the LAN, not localhost.

2 Comments

run docker inspect on the container. The IP will be in the resulting JSON.
my ip is 172.17.0.2. I tried to run as 172.17.0.2/swagger/index.html and still it doesnt work
0

if we use docker compose, can add like this,
docker file:
EXPOSE 8080
EXPOSE 8081

and

in docker-compse:

    ports:
      - 5000:8080
    environment:
      - ASPNETCORE_ENVIRONMENT=Docker
      - ASPNETCORE_HTTP_PORTS=8080
      - ASPNETCORE_HTTPS_PORTS=8081
      - ASPNETCORE_URLS=http://+:8080

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.