1

I have an ASP.Net MVC web application (.Net Framework 4.6) that I usually ran in local dev machine (let's say it's running on localhost:10000). The application connect to few local services:

  • Database
  • Azure Storage (emulator)
  • Azure Cosmos (emulator)

Local dev machine (the host) is running on Windows 10.

Question

I'm in the process of moving this application to a Docker container and was able to run the application off of a Docker container. My question is, how do I configure the network in (using Docker Compose or docker run so that the application (that's in the the container) can connect to the services (database, storage and cosmos emulator) that are in my host?

What I have tried

None of these work. I still get error when connecting from Container to Host.

Error

enter image description here

Other Configurations

enter image description here

enter image description here

enter image description here

I'm using Dockerfile and Docker Compose yml files to build and run the container locally.

Dockerfile file

# escape=`

FROM mcr.microsoft.com/dotnet/framework/sdk:4.8 AS build
WORKDIR /app

# Copy everything else and build app
COPY Web/Web/. ./Web/Web/

# Build the projects
WORKDIR /app/Web/Web
RUN msbuild /p:Configuration=Debug

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8 AS runtime
WORKDIR /inetpub/wwwroot

# Configure website
SHELL ["powershell", "-command"]

# Install ASP.NET
RUN Install-WindowsFeature Web-Asp-Net45
EXPOSE 10000
RUN Remove-Website -Name 'Default Web Site'; `
    New-Website -Name 'Web' `
                -Port 10000 -PhysicalPath 'c:\inetpub\wwwroot' `
                -ApplicationPool '.NET v4.5'

# Final copy for build
COPY --from=build /app/Web/. ./

Docker Compose yml file.

version: '3.5'
services:
  web:
    build: .
    ports:
      - "10000:10000"
Community
  • 1
  • 1
stack247
  • 5,579
  • 4
  • 41
  • 64
  • `host.docker.internal` should work. Have you tried entering a running container to the command line (powershell) and then trying to debug the issue (using tools like ping, tracert, etc)? – phansen Jun 06 '19 at 21:03
  • @phansen great question. At one point, I was able to ping it from a command line. But this doesn't seem to work in ASP.NET MVC application itself – stack247 Jun 06 '19 at 23:11

2 Answers2

0

Not sure if this gets you all the way there, but maybe you could try co-locating all of those services inside a single Docker Compose file. As long as they're then all running on one docker-compose up command, I believe you should be able to reference the service names as the hosts in the other services.

For example, I maintain a collection of DC files for spinning up local databases. In each of those files, the service is named db so the host would be http://db:PORT.

https://github.com/alexmacarthur/local-docker-db

It's been a minute since I've toyed with a setup like this... but hopefully it helps point you in the right direction.

Alex MacArthur
  • 2,220
  • 1
  • 18
  • 22
0

On linux I use VSCode development container (docker container).

And if I want to connect from the container to some service run on the host I use 172.17.0.1 instead of localhost.

172.17.0.1 is the IP address of the gateway between the Docker host.

You can use ifconfig in container to check that you have save network.

$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.25

here you can see that my container has IP 172.17.0.2 so the host has IP 172.17.0.1 in the same subnet.

Alexey Muravyov
  • 1,106
  • 13
  • 17