Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core WebApi refuses connection in Docker container

I am trying out Docker with a small WebApi which I have written in dotnet core.

The Api seems to work fine because when I run it with dotnet run it starts normally and is reachable on port 5000. But when I run it in a Docker container it starts, but I cannot reach it on the exposed/mapped port. I'm running Docker on Windows 10 withing VirtualBox.

My Dockerfile looks like this:

FROM microsoft/aspnetcore-build:latest
COPY . /app
WORKDIR /app
RUN dotnet restore
EXPOSE 5000
ENV ASPNETCORE_URLS http://*:5000
ENTRYPOINT ["dotnet", "run"]

I am building the dontainer like this:

docker build -t api-test:v0 .

And run it with this command:

docker run -p 5000:5000 api-test:v0

The output of the run command is:

Hosting environment: Production
Content root path: /app
Now listening on: http://localhost:5000

I have also tried different approaches of binding the URL:

  • as http://+:5000, http://0.0.0.0:5000, http://localhost:5000, ...
  • via CLI parameters --urls / --server.urls

but without success. Does anyone see what I'm doing wrong or missing?

like image 843
Daniel Lerps Avatar asked Oct 29 '22 07:10

Daniel Lerps


2 Answers

Now listening on: http://localhost:5000

Binding to localhost will not work for your scenario. You need to get the app to bind to 0.0.0.0 for the docker port forwarding to work. Once you do that, you should be able to reach the app on the VM IP, port 5000

like image 199
BMitch Avatar answered Nov 09 '22 14:11

BMitch


  1. Make sure your service is listening on all ports using http://*:500 or similar (if it prints localhost when running, it won't work).
  2. If you set up your docker environment with VirtualBox and used e.g. docker-machine, you n need to use the IP address of the virtual machine that runs the docker containers. you can get the IP via docker-machine ip default.
like image 45
Martin Ullrich Avatar answered Nov 09 '22 14:11

Martin Ullrich