Docker Community Forums

Share and learn in the Docker community.

How to use "localhost" instead of ""?

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

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

If you’re seeing an IP address of 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 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 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…


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

I want to reiterate what I said about localhost and being magical: if you docker-machine ssh and get a shell in the Docker Machine VM, it will see as itself; and if you docker exec into a container, it will see 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

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 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 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:


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.