Clock
Clock

Reputation: 984

Could not access Web API inside docker container from localhost

I know that there are many discussion about this, but none of the proposed solutions worked for me, so I will have to know at least if I was doing something wrong or I was hitting a limitation.

Step 1. I created the default .NET Core 2.0 WEB API project from Visual Studio, nothing special here, output type set to Console Application, running OK from Visual Stuido 2017 Community.

Step 2. I installed latest Docker Toolbox since I am running Windows 10 Home Edition, that installed also the Virtual Box.

Step 3. I added the following docker file next to the sln:

    FROM microsoft/aspnetcore-build:2.0

    WORKDIR /app

    EXPOSE 80

    COPY . .

    RUN dotnet restore

    RUN dotnet build

    WORKDIR /app/DockerSample

    ENTRYPOINT dotnet run

Step 4. I successfully build the image with a command like 'docker build -t sample1 .'

Step 5. The container successfully started to run, it was started by the following command 'docker run -d -p 8080:80 sample1'

Step 6. Pull info about the container using command docker logs c6 The following info was shown:

enter image description here

Interesting here is the address where the service is listening, this seems to be the same with the address I was getting when running the service directly from Visual Studio.

Is this the service address from the virtual machine that is running inside Virtual box ? Why the port is not 8080 or 80 as I mentioned inside of the "run" command ?

The container looks ok, something like:

enter image description here

Step 7.

Now starts the fun trying to hit the service from Windows 10 machine, was impossible using calls like http://localhost:8080/values/api I also tried calls like http://192.168.99.100:8080/values/api where 192.168.99.100 is the address of the default docker machine.

I also tried with something like 'http://172.17.0.2:8080/values/api' where the IP address was got after a call like 'docker inspect a2', changing the port to 80 did not help :). Trying to change the port number to 80 or 58954 , the one shown in red as listening, did not help. Also Windows Firewall or any other firewalls were stopped.

I tried to port forward from VirtualBox having something like

enter image description here

Trying to change the 80 and 8080 ports between them for host and guest also did not work.

Basically none of the suggested solutions I found did not gave me the chance to hit the service from my Windows machine.

Mainly I was following this tutorial https://www.stevejgordon.co.uk/docker-for-dotnet-developers-part-2 which explains quite well what should be done only that at some point is using the Docker Desktop for Windows so the Docker Toolbox was left behind.

Do you know what should I do so that I can hit the service from the docker container ?

Upvotes: 2

Views: 10012

Answers (2)

Edward
Edward

Reputation: 29966

For your issue, it is caused by that you run the container under Development environment which did not use the port 80 for the application.

For FROM microsoft/aspnetcore-build:2.0, it seems you could not change the ASPNETCORE_ENVIRONMENT to Production.

For a solution, you could change your docker file like below which change the base image with microsoft/aspnetcore:2.0.

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY ["TestAPI/TestAPI.csproj", "TestAPI/"]
RUN dotnet restore "TestAPI/TestAPI.csproj"
COPY . .
WORKDIR "/src/TestAPI"
RUN dotnet build "TestAPI.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "TestAPI.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TestAPI.dll"]

Upvotes: 0

SilentTremor
SilentTremor

Reputation: 4902

In docker compose (visual studio add docker integration "docker-compose.yml") set this:

version: '3.4'
   services:
      webapi.someapi:
        image: ${DOCKER_REGISTRY-}somenamesomeapi
        build:
          context: .
          dockerfile: ../webapi/Dockerfile
        environment:
          - ASPNETCORE_URLS=https://+:443;http://+:80
          - ASPNETCORE_HTTPS_PORT=443
        ports:
          - "80:80"
          - "443:443"

in lunch settings specify your app to start on ports 80 and 443 https

Docker for visual studio code: https://marketplace.visualstudio.com/items?itemName=PeterJausovec.vscode-docker

Follow this steps to orchestrate your containers: https://marketplace.visualstudio.com/items?itemName=PeterJausovec.vscode-docker

Upvotes: 4

Related Questions