2

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
  • Next

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 ?

Clock
  • 974
  • 3
  • 17
  • 35

2 Answers2

4

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

SilentTremor
  • 4,747
  • 2
  • 21
  • 34
  • Hi, thank you for response, could you please tell me where should I find these settings in Visual Studio ? I just have the clean Visual Studio Community edition, and the default web api core project template. – Clock Feb 25 '19 at 07:31
  • Thank you ! I will try them once I get home. – Clock Feb 25 '19 at 07:59
  • I tried to install the VS extension you recommend but the installation was failing. Still I manged to add the "docker-compose.yml" from Visual Studio, but together with it there were added also some other files (.dockerignore, docker-compose.ci.build.yml), and also a new Docker file that was just not building the image. – Clock Feb 25 '19 at 20:45
  • If you have visual studio then run from there, remove what you don't need, and use your docker file instead (no the one generated) – SilentTremor Feb 26 '19 at 08:33
  • Follow step by step the tutorial, if you have problems launching the app on the new setup, close project, remove from root directory .vs, then reopen solution, some times visual studio launcher config get's clogged. – SilentTremor Feb 26 '19 at 08:35
  • Thank you for the tips, I will try them – Clock Feb 26 '19 at 08:42
0

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"]
Edward
  • 28,296
  • 11
  • 76
  • 121
  • Hi, I tried using the docker file you suggested but it fails at the publishing step with the following message: "The specified runtimeconfig.json [/src/DockerSample/bin/Release/netcoreapp2.0/DockerSample.runtimeconfig.json] does not exist" . – Clock Feb 25 '19 at 20:18
  • @Clock Is there any demo to reproduce your issue? – Edward Feb 28 '19 at 05:36
  • I was following this tutorial https://www.stevejgordon.co.uk/docker-for-dotnet-developers-part-2 . And did the steps I mentioned in the question. I am running a Windows 10 Home Edition and VS 2017 Community. – Clock Feb 28 '19 at 06:36