How to use "localhost" instead of "192.168.99.100"?

How to use “localhost” instead of “192.168.99.100”? Can you please point me to the answer?
Thanks!

You don’t, they’re different. (“localhost” always resolves to 127.0.0.1 and it’s a very magical IP address.)

If you’re seeing an IP address of 192.168.99.100 you’re probably using Docker Toolbox or Docker Machine, which is running Docker on a Linux VM, and that’s the default IP address of that VM. If you’re using one of these options to run Docker then you have to use the $(docker-machine ip) address, usually 192.168.99.100. If you really want it to be “localhost” then you need to switch to the desktop-based Docker for Mac, or Docker for Windows, or just running Docker directly on your Linux host, as appropriate.

1 Like

Thanks for reply! Yes, I’m using Docker Toolbox on Windows. So, can I set default docker machine IP to be 127.0.0.1 and be able to use “localhost” in my browser URL or it’s not possible under Windows?
I want to be able to use localhost:8080 for example, same way what I used with Tomcat server…

Thanks!

No; if you’re using Docker Toolbox you have to use 192.168.99.100.

I want to reiterate what I said about localhost and 127.0.0.1 being magical: if you docker-machine ssh and get a shell in the Docker Machine VM, it will see 127.0.0.1 as itself; and if you docker exec into a container, it will see 127.0.0.1 as the container. Be careful using “localhost” in a Docker environment; if you ever start qualifying it (“on my localhost”, “the localhost of…”) then it’s not what you’re looking for.

OK. Thanks! It’s not very convenient…
Let’s pretend that you have Java Web Application and you are testing it locally on Tomcat or just using Spring Boot and embedded Tomcat server.
Usually, you would use localhost:8080 for your testing on your local machine.
Now, you decide to dockerize it. And instead of localhost:8080 you have to specify 192.168.99.100:8080???

Here is my real world example and I’m a little bit confused what is the best practice and best architecture for my case…

  1. I have the following microservices architecture built on Spring Clouds:
    a). Eureka Service - for discovery and registry of microservices
    b). API Gateway Service - based on zuul
    c). 4-5 microservices - just Spring Boot applications
  2. Without docker - I’m using locally localhost:8070 - default for Eureka and localhost:8081 - 8085 for other microservices modules
  3. When I deploy all my systems to Linux - it’s just using something like abc.com with the same corresponding ports
  4. If I want to dockerize separately all my above modules, how should I specify hosts:ports?
  5. I know that on Windows 10 EE and Pro - it can be used localhost as I used to do, and it’s working.
  6. But for Windows Home edition - the only option was to use Docker Toolbox (and 192.168.99.100 is default!!!). So I need to change the code base and rebuild before using locally and then change code again and rebuild before deploying to Linux QA/PROD…
  7. What is a trick to avoid host changes and make it universal: using/testing locally or using in other environments?

Thanks again!

Well, if someone still needs to do something as the guy asking for it …

Read this: https://www.jhipster.tech/tips/020_tip_using_docker_containers_as_localhost_on_mac_and_windows.html

3 Likes

Oh this is nice. Thank you for sharing this.

It was very helpful. many thanks mate

when starting the container, you can specify port mapping.
example: docker run -p 8080:8080 IMAGENAME

the first 8080 is whatever port you want to connect to from your local machine (they don’t have to match)
the second 8080 is whatever port you have set up in your container.