0

I'm using docker for building both UI and some backend microservices, and using Spring Zuul as the Proxy to pass Restful API calls from UI to the downstream microservices. My UI project needs to specify an IP address in the JS file before the build, and the Zuul project also needs to specify the IP addresses for the downstream microservices. So that after starting the containers, I can access my application using my docker machine IP http://192.168.10.1/myapp and the restful API calls in the browser network tab will be http://192.168.10.1/mymicroservices/getProduct, etc.

I can set all the IPs to my docker machine IP and build them without issues. However for my colleagues located in other countries, their docker machine IP will be different. How can I make docker use a specific IP, for example, 192.168.10.50, which I can set in the UI project and Zuul Proxy project, so that the docker IP will be the same for everyone, regardless of what their actual docker machine IP is?

What I've tried:

  1. I've tried port forwarding in VirtualBox. It works for the UI, however the restful API calls failed.
  2. I also tried the solution mentioned in this post: Assign static IP to Docker container However I can't access the services from the browser using the container IP address.

Do you have any better ideas? Thank you!

  • `My UI project needs to specify an IP address in the JS file before the build, and the Zuul project also needs to specify the IP addresses for the downstream microservices.` - This sounds strange. Can't you use hostnames instead of hardcoded IPs? – Shashank V Dec 09 '20 at 17:09
  • Hi Shashank, you mean using localhost or 127.0.0.1? That doesn't work when I build the images in docker and run the containers. – RememberLove Dec 09 '20 at 18:02

1 Answers1

0

first of to clarify couple things,

  • If you are doin docker run ..... then you just starting container in your docker which is installed on the host machine. And there now way docker can change ip of your host machine. Thus if your other services are running somewhere else they will have to know something about docker host machine, ip or dns name. so basically docker does runs on 127.0.0.1 if you are trying it on docker host machine, or on host machine IP if from outside of it. So docker don't need IP of host to start.

  • The other thing is if you are doing docker-composer up/start. Which means all services are in that docker compose file. In this case docker composer creates docker network for all containers in it. in this case you definitely can use fixed IPs for containers, though most often you don't need to because docker takes care of name resolution in that network.

if you are doing k8s way - then it is third way (production way), and it os another story.

if that is neither of above then please provide more info on how are you doing stuff.

EDIT - to:

if you are using docker composer and need to expose any of your containers to host machine you can do it through port mapping:

web:
    image: some image here
    ports:
      - 8181:8080

left is the host machine port, right is container port

and then in browser on the host you can do request to localhost:8181

here is doc https://docs.docker.com/compose/compose-file/#ports

user2932688
  • 1,546
  • 11
  • 24
  • Thank you for your answer. I followed this post https://stackoverflow.com/questions/27937185/assign-static-ip-to-docker-container and tried to create my own network: docker network create --subnet=172.18.0.0/16 mynet123, and then run the image: docker run --net mynet123 --ip 172.18.0.22 -it ubuntu bash, however when I tried to access it from the browser using http://172.18.0.22, it didn't work for me. It seems like we can't access the container using its IP from the browser? – RememberLove Dec 09 '20 at 22:17
  • modified answer so you have details on how to exposed containers in docker compose network to host machine – user2932688 Dec 09 '20 at 22:48