0

I deployed a demo web API project on port 8086.I am able to run it on my local browser using localhost:8086/api/controllername and also using local machine IP address for example: 192.0.0.0:8086/api/controllername. I tried accessing the URL from another machine on same LAN and I am able to access it.

But now I want to access it from machines on other networks (publicly). How can I assign a static IP so that I can use the API from any machine irrespective of network? I created a network using below commands

 docker network create --driver bridge --subnet 172.18.0.0/16 -- gateway=172.18.0.1 IPStatic

and

 docker network connect --ip 172.18.0.2 IPStatic Containerid.

But unable to access the api using 172.18.0.2:8086/api. Am I missing something? I am using asp.net core web api and I am fairly new to Docker.

halfer
  • 19,824
  • 17
  • 99
  • 186
Pwavel002
  • 279
  • 2
  • 10

1 Answers1

0

You always use the host IP address for this, the same way as if you were running the service outside of Docker. The container-private IP addresses are unreachable from other hosts (and on some platforms aren't even reachable from outside Docker on the same host); it's usually wrong to manually set them or to try to look them up.

If it's specifically important that this service have its own IP address, you need to ask your network administrator to assign an additional address to the host. The docker run -p option can bind a service to only specific network interfaces or addresses. On a Linux host I might run

# Assign the alias address
ifconfig eth0:0 192.0.0.2

# Run the service bound to only this interface
docker run -p 192.0.0.2:80:8080 ...

You might need to reconfigure other services to not listen on this new interface. For Docker services you'd use the same docker run -p option to bind to only the host's primary interface and to localhost (127.0.0.1); configuration for non-Docker services is specific to the service.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • I have referred following link for running container on static ip https://stackoverflow.com/questions/27937185/assign-static-ip-to-docker-container I am using Docker on Windows 10 which i tried to duplicate but not successful. Not sure if i need to approach my Network team for that. Your thoughts. – Pwavel002 Jun 10 '19 at 12:10