ar.freitas
ar.freitas

Reputation: 43

API dotnet not reachable with docker

I'm try create a container with dotnet api, but, after the build and use the docker run command, it not access. I need understand where mistakes occurred to fix it.

My some distro is Ubuntu, using docker version 18.09.5 and used the images for create Dockerfile:

microsoft/dotnet:2.2-sdk-alpine microsoft/dotnet:2.2-aspnetcore-runtime-alpine

The dockerfile content:

FROM microsoft/dotnet:2.2-sdk-alpine AS build-env
WORKDIR /app
ENV ASPNETCORE_URLS http://0.0.0.0:5000

COPY source/*.csproj ./
RUN dotnet restore

COPY source/ ./
RUN dotnet publish -c Release -o out


FROM microsoft/dotnet:2.2-aspnetcore-runtime-alpine
WORKDIR /app
COPY --from=build-env /app/out .

EXPOSE 5000
ENTRYPOINT ["dotnet", "devops-test.dll", "--urls", "http://0.0.0.0:5000"]

Command for build:

docker build -t freitas/apidotnet .

Commando for run the app:

docker run -it -p 5000:5000 freitas/apidotnet

After run, this message are show:

warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {09e12a74-3a22-4ff3-b882-3f546d91d02a} may be persisted to storage in unencrypted form.
warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Address not available'.
Hosting environment: Production
Content root path: /app
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

But, i try access the browser http://localhost:5000 or use the postman to get api, but not response.

I don't have idea for this. Sorry for my bad english and thanks for help.

Upvotes: 0

Views: 1600

Answers (2)

grinay
grinay

Reputation: 758

Use environment variable in docker file last lines should be like that

ENV ASPNETCORE_ENVIRONMENT Development
ENV ASPNETCORE_URLS http://+:5000
ENTRYPOINT ["dotnet", "devops-test.dll"]

Upvotes: 2

Simply Ged
Simply Ged

Reputation: 8642

The thing I noticed is the output says http://localhost:5000 but your docker files has multiple entries for http://0.0.0.0:5000.

You've not shown your program.cs so I'm going to assume that you've something like:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseUrls(new[] { "http://localhost:5000" });

This is what seems to be causing the problem. You are providing the URL to listen on from multiple places:

  • ASPNETCORE_URLS
  • command line [--urls]
  • program.cs

And they are being applied in that order, so the program.cs setting is winning. So, for some reason, your container is listening on localhost:5000 but that isn't being reached from outside (someone else might be able to explain why this is??)

The fix is to remove the UseUrls line from program.cs and rebuild your container.

Upvotes: 2

Related Questions