How to force docker for windows to assign private and static IP to the websites

Hi Guys,
I am trying to build and run docker image for asp.net website on windows server 2016 using docker 1.12.2-cs2-ws-beta, build 050b611 .
I am able to build and run the website and able to access it through browser. But the docker assigns its own IP like “172.22.129.23” to the container and the website accessible through this IP only on the docker host.
How we can force the docker to assign the IP of our own choice?

Why do you need static IP address inside container? You can map port to host at start up and this way access externally to host based on IP:port and this removes neccessity to know IP address of invidual container.

Hi @artisticcheese,
How to map port to host. I have already build the image on another windows server 2016 and successfully pulled the image on different win 2016 machine and then build the image using command.
docker run banctecgajendra/docker-whale:sample
After this my container was up and running with the IP 172.22.129.23 and I can access the site using http://172.22.129.23
But I need this IP to be set as per my own list which then should be accessible from any machine using standard browsers.

add to docker run command parameter -p 8080:80
This will map port 8080 on your host to port 80 of your container. After this is done access your host on port 8080 and you end up pulling data from port 80 of your container without need to know it’s IP address.

I already tried it and even in this case I need to run the inspect command to get the IP and using that IP only I can access the site that on the machine where I pulled that image and executed the run command.
example http://172.24.20.156/
The IP I got by executing the inspect command resulted below.

“Networks”: {
“nat”: {
“IPAMConfig”: null,
“Links”: null,
“Aliases”: null,
“NetworkID”: “5f7f7469e8b4af462292225451acc7f5b9850673bf77f1e14f16ea3ba9332350”,
“EndpointID”: “fd71d914db6e9815f9ba9cad46428058e78cba7850af6f71eb6d8e2a4870fec0”,
“Gateway”: “”,
“IPAddress”: “172.24.20.156”,
“IPPrefixLen”: 16,
“IPv6Gateway”: “”,
“GlobalIPv6Address”: “”,
“GlobalIPv6PrefixLen”: 0,
“MacAddress”: “00:15:5d:32:ee:99”
}

Acess your computer via IP address of your host if you already mapped a port

I too am having this issue, the IP used with a windows container running under docker is dynamic and even with a port mapping the site is not accessible using http://localhost:[port] but is accessible via the ip address shown when using the docker network inspect nat command ?

It’s accessible but not on host itself. If you are checking HTTP connectivity on host you have to use internal IP address of container but if you are external to container host then accessing containerhost will work fine.

sorry if I misunderstand you, but if I’m correct your saying that when I’m accessing a container that is hosted by docker running on the same machine as the browser that I can’t use localhost:port even if that port is mapped using expose in the yaml file?

If this is the case how can solve this? I need to access multiple web sites on known IP address to simulate oauth & multi tenantcy scenarios on developer systems?

Yes, that’s correct, you can not use locally mapped port while you on the same machine. It’s bug in WinNAT which is still not fixed.
There is no solution except just doing what you are doing from some other machine or using static IP address for container and then accessing it directly.

OK, a known bug glad it’s not me begin a noob :wink: how can you assign static ips to a container I’ve tried a number of things but keep getting issues including docker reporting that you can’t assign in addresses for Windows containers :sob: -ports 192.168.0.10:80:80

BTW thanks for the quick response

Oh and do you have any links to this bug?

I never tried but I assume you can assign IP address to container see here (https://docs.docker.com/compose/compose-file/#ipv4_address-ipv6_address)
Here is bug (towards middle of article)
https://blogs.technet.microsoft.com/virtualization/2016/05/25/windows-nat-winnat-capabilities-and-limitations/

many thanks, I can confirm that setting a static IP address for a Windows container image has no effect, the container does not get assigned the IP address specified and is assigned a dynamic IP address.

I’m wondering if the container registers itself with a dns server using a FQDN if that would work. It’s late here in the U.K. So I’ll give that a shot in the morning. Thanks again for you help

Static IP works.

docker network create -d nat --subnet=172.16.238.0/24 --gateway=172.16.238.1 my_network

version: '3.2'
services:
   test:
      image: artisticcheese/cpuconsume:latest
      networks:
        my_network:
          ipv4_address: 172.16.238.10
networks:
  my_network:
    external: true

Or you can do in single compose file without creating network from CLI first but you have to downgrade compose version to 2.1 instead.

version: '2.1'
services:
   test:
      image: artisticcheese/cpuconsume:latest
      environment: 
        - "SHELL=powershell.exe"
      networks:
        my_network:
          ipv4_address: 172.16.238.10
networks:
  my_network:
    ipam:
      driver: default
      config:
        -
          subnet: 172.16.238.0/24
          gateway: 172.16.238.1

again many thanks your insight has help me greatly, I’ve opted to create 3 developer networks and only expose the frontend and management network to the host with front end containers connecting to both frontend and backend networks. this is working a treat.

I’m now looking at assigning the exposed containers a number of FQDNs so the external DNS can direct the host to the container which uses the FQDN to resolve which tenant is accessing it.

thanks again for all your help