.Net Core WebApi refuses connection in Docker container

Viewed 3291

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?

4 Answers

I found a way round this. All you need to do is edit launchSettings.json change to "applicationUrl": "http://*:5000/" of your app setting. Build the image. Then run the image docker run -d -p 81:5000 aspnetcoreapp after it runs get ip address of the container docker exec container_id ipconfig. Then in browser http://container_ip:5000/api/values. For some reason it does not work http://localhost:81 still need to figure out why that is.

I had the same issue recently with Docker version 20.x. The comment above provided good lights. If anyone faces the same issue here is how I've solved: Edit launchSettings.json to

"applicationUrl": "http://localhost:5001;http://host.docker.internal:5001",

This will let you test your web API locally and also to be consumed from the container.

Related