Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 3.0 Docker Container port mapping fails

I am trying to get a brand new ASP.NET core MVC web application running from within a Docker container on my windows 10 machine.

I'm no docker expert but the port doesn't seem to get bound on the host. I get a response from within the container, but not from the host. The 2.2 sample works fine for me.

dotnet --version
3.0.100-preview6-012264

Steps to reproduce:

mkdir aspnetcore3_test
cd .\aspnetcore3_test\

mkdir aspnetapp
cd .\aspnetapp\

dotnet new mvc

cd ..
dotnet new sln

dotnet sln "aspnetcore3_test.sln" add "aspnetapp/aspnetapp.csproj"

Create a docker file with the following content:

FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
RUN dotnet restore ./aspnetapp/*.csproj

# copy everything else and build app
COPY aspnetapp/ ./aspnetapp/
WORKDIR /app/aspnetapp
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime
WORKDIR /app
COPY --from=build /app/aspnetapp/out ./
EXPOSE 80
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

Then do the docker bits:

docker build -t dockertest .
docker run --rm -it --name=dockertest dockertest:latest -p 80:80

Docker run outputs the following:

warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
      Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {5b22d641-4632-4440-8f7d-8ad9b5e48098} may be persisted to storage in unencrypted form.
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://[::]:80
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /app

With the container running Firefox says that it was unable to connect and docker port dockertest comes up empty

docker exec -i -t dockertest curl localhost spits out the template MVC app HTML

like image 684
Mike Wade Avatar asked Jul 03 '19 09:07

Mike Wade


1 Answers

By adding the -p 80:80 after the name of the image, you're actually passing -p 80:80 as the args for the image executable, rather than as an arg for docker itself. The solution is to move that to before the image name:

docker run --rm -it --name=dockertest -p 80:80 dockertest:latest 
like image 193
Kirk Larkin Avatar answered Nov 18 '22 13:11

Kirk Larkin