As the title indicates, i have a container that is unable to bind from host port to container port. I tried searching for similar issues, but have not found any related to using dotnet watch in a docker container since Microsoft introduced the microsoft/dotnet
docker repo with dotnet watch built into the sdk image.
Any suggestions as to what i am doing wrong are much appreciated.
Dockerfile
FROM microsoft/dotnet:2.1.301-sdk as build
ENV DOTNET_USE_POLLING_FILE_WATCHER 1
WORKDIR /app
COPY . .
RUN dotnet restore
EXPOSE 5000-5001
ENTRYPOINT [ "dotnet", "watch", "run", "--no-restore"]
docker-compose.yml
version: "3"
services:
esportapp:
container_name: esportapp
image: esportapp:dev
build:
context: .
dockerfile: Docker/dev.Dockerfile
volumes:
- esportapp.volume:/app
ports:
- "5000:5000"
- "5001:5001"
volumes:
esportapp.volume:
Complete error:
esportapp | Hosting environment: Development
esportapp | Content root path: /app
esportapp | Now listening on: https://localhost:5001
esportapp | Now listening on: http://localhost:5000
esportapp | Application started. Press Ctrl+C to shut down.
esportapp | warn: Microsoft.AspNetCore.Server.Kestrel[0]
esportapp | Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'.
esportapp | warn: Microsoft.AspNetCore.Server.Kestrel[0]
esportapp | Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
Just ran into this problem myself. I don't think
dotnet watch run
plays nicely with localhost type urls. Try setting your hosting url tohttps://0.0.0.0:5000
in your container.In the dockerfile with:
Or in launchSettings.json like:
Now to get it to automatically reload from within the container you have to use the polling file watcher. That's what the second environment variable is for. (This is pretty common, you've got to do this with webpack, angular, etc).
In your case, you need to change the
esportsapp.volume
to a directory on your host:That will map the /app volume in your container to the docker-compose directory. The problem you're facing is that the app is built in a volume on your project's default docker-compose network, so when you change a file in the source directory, it's not actually changing in that volume. With this fix, however, you'll run into the problem of the dotnet restore and dotnet watch inside the container changing your host's files. There's a fix for all of that, if you're interested...
My Usual .Net Core App Docker setup
To debug, run:
docker-compose -f run.yml up --build
To build a release:
docker-compose -f build.yml up --build
Project structure
Directory.Build.props (put this in the same folder as your .csproj, keeps your
dotnet watch run
command from messing with the source directory on your host)run.yml (docker-compose.yml for debugging)
run.Dockerfile (the Dockerfile for debugging)
build.yml (the docker-compose.yml for building release versions)
build.Dockerfile (the Dockerfile for building release versions)