title: How can I expose a port from ASP.NET Core in Docker linux container in Windows?
body: <p>I have several ASP.NET Core services defined in <code>docker-compose</code>. I need to be able to expose port in the <code>host</code> network (I use linux containers on Docker for Windows 18.03.0-ce).</p>
  
  <p>Sample:</p>
  
  <ul>
  <li>Services C1 and C2 are running in containers.</li>
  <li>Service H3 is running directly in the host, not in container.</li>
  <li>All services (C1, C2 and H3) must be accesible to each other.</li>
  </ul>
  
  <p>I don't know how to setup networking for services C1 and C2 in <code>docker-compose</code>. I tried this:</p>
  
  <pre><code>version: '3'
  
  services:
    c1:
      image: &lt;some image name&gt;
      build:
        context: .
        dockerfile: &lt;path to dockerfile&gt;
      environment:
        - ASPNETCORE_ENVIRONMENT=Development      
        - ASPNETCORE_URLS=http://localhost:9084
      ports:
        - "9084:9084"
      network_mode: "host"
  
    c2:
      image: &lt;some image name&gt;
      build:
        context: .
        dockerfile: &lt;path to dockerfile&gt;
      environment:
        - ASPNETCORE_ENVIRONMENT=Development      
        - ASPNETCORE_URLS=http://localhost:9085
      ports:
        - "9085:9085"
      network_mode: "host"
  </code></pre>
  
  <p>I see this in the console when containers are started:</p>
  
  <pre><code>Hosting environment: Development
  Content root path: /app
  Now listening on: http://localhost:9085
  Application started. Press Ctrl+C to shut down.
  </code></pre>
  
  <p>But I cannot connect them from the host. <code>netstat -an</code> doesn't show these ports, <code>telnet localhost 9084</code> fails.</p>
  
  <p>This is output from <code>docker inspect</code> for that container:</p>
  
  <pre><code>"NetworkSettings": {
      "Bridge": "",
      "SandboxID": "df480d4d68cb109dc6b85f689f8b210a9f74e650c48a343cdd3d11eb3d10bec7",
      "HairpinMode": false,
      "LinkLocalIPv6Address": "",
      "LinkLocalIPv6PrefixLen": 0,
      "Ports": {},
      "SandboxKey": "/var/run/docker/netns/default",
      "SecondaryIPAddresses": null,
      "SecondaryIPv6Addresses": null,
      "EndpointID": "",
      "Gateway": "",
      "GlobalIPv6Address": "",
      "GlobalIPv6PrefixLen": 0,
      "IPAddress": "",
      "IPPrefixLen": 0,
      "IPv6Gateway": "",
      "MacAddress": "",
      "Networks": {
          "host": {
              "IPAMConfig": null,
              "Links": null,
              "Aliases": null,
              "NetworkID": "324d836f54fa685d56b416f01a18e3e75f88d4516dafac43bb1a4ff760a2b67a",
              "EndpointID": "9212f6a01e142ef9fb71116c9fe03c37567c60ed8a2add91fe28bf0bd6de001a",
              "Gateway": "",
              "IPAddress": "",
              "IPPrefixLen": 0,
              "IPv6Gateway": "",
              "GlobalIPv6Address": "",
              "GlobalIPv6PrefixLen": 0,
              "MacAddress": "",
              "DriverOpts": null
          }
      }
  }
  </code></pre>
  
  <p>When I remove <code>network_mode: "host"</code> configuration from <code>docker-compose</code> (network is then <code>bridge</code> as default), I get this message form the application:</p>
  
  <pre><code>warn: Microsoft.AspNetCore.Server.Kestrel[0]
  Unable to bind to http://localhost:9084 on the IPv6 loopback interface: 'Error -99 EADDRNOTAVAIL address not available'.
  </code></pre>
  
  <p>Can you help me to setup the network configuration?</p>