I am using Docker for Mac. I am running a nodejs based microservice in a Docker container. I want to test node microservice through the browser. How to get IP address of running docker container?
-
3Does this answer your question? [How to get a Docker container's IP address from the host?](https://stackoverflow.com/questions/17157721/how-to-get-a-docker-containers-ip-address-from-the-host) – sanyassh Nov 18 '19 at 14:27
9 Answers
If you don't want to map ports from your host to the container you can access directly to the docker range ip for the container. This range is by default only accessed from your host. You can check your container network data doing:
docker inspect <containerNameOrId>
Probably is better to filter:
docker inspect <containerNameOrId> | grep '"IPAddress"' | head -n 1
Usually, the default docker ip range is 172.17.0.0/16
. Your host should be 172.17.0.1
and your first container should be 172.17.0.2
if everything is normal and you didn't specify any special network options.
EDIT Another more elegant way using docker features instead of "bash tricking":
docker inspect -f "{{ .NetworkSettings.IPAddress }}" <containerNameOrId>
EDIT2 For modern docker engines, now it is this way (thanks to the commenters!):
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <containerNameOrId>

- 5,384
- 4
- 27
- 51
-
9You are talking about the internal IP, and it has nothing to do with the question at hand. Your answer provide just confusion to the reader. Since the IP is the IP of the machine where the Docker is running on. Which is just `localhost` :) – David Gatti Apr 29 '17 at 12:16
-
You are wrong. The OP didn't specify anything about the microservice must be accesible from outside. He only said "test node microservice through the browser" and this can be done with my proposed method using `http://172.17.0.2:anyPort` from the host. Of course if you want to access it using localhost on host, you must map ports. Will see what the OP wants. – OscarAkaElvis Apr 29 '17 at 13:26
-
4You are saying that you can access the internal IP inside the Container from the host? That is physical impossible, that is a completely a different network. Not only I just checked and yes, I can't access the 172.17.0.7 of my docker container, then I should be able to see all the docker contender if I scan my host machine with a network scanner, and this is not the case again. I know that it should not work, I tested what you say (you never know) and it is not working. So maybe your explanation is missing a key part? – David Gatti Apr 29 '17 at 15:26
-
The containers are accessible from the host. Try it yourself... I'll give you some hints. 1. Download an run container: `docker run --name struts --rm -i tomcat` 2. Get your container's ip (if you have only one): `docker inspect struts | grep '"IPAddress"' | head -n 1` 3. Open it on host's browser `http://x.x.x.x:8080` . You can see Apache Tomcat default's page. – OscarAkaElvis Apr 29 '17 at 20:57
-
Exactly how I say it and how the documentation says it of Tomcat on https://hub.docker.com/_/tomcat/: `http://localhost:8888 or http://host-ip:8888`. Because this is how the Tomcat Docker is configured, and is configured to pass 8080 to port 8888. I recommend reading - https://docs.docker.com/engine/userguide/networking/ and building some container yourself to understand how it all works. You can't access the docker form the internal IP. It is an impossibility. That is not how network works. – David Gatti Apr 29 '17 at 21:40
-
I know about `-p` use. How do you explain this example I put is working without using `-p` on docker run? It's accesible from host. Is tomcat container special for some reason? – OscarAkaElvis Apr 30 '17 at 07:38
-
I investigated further and you are wrong David. default iptables rules for docker unless you specify something different make containers accessible only from host by default. The `-p`option is to map ports to your host but what I proposed is to access directly to container's ip which is 100% correct too. – OscarAkaElvis May 05 '17 at 09:29
-
2I wasn't able to access the web app in the container without mapping port using -p – bilal.haider Aug 15 '17 at 15:46
-
The command should be docker inspect
| grep '"IPAddress"' | tail -n 1 – Kartik Chauhan Jan 23 '19 at 06:07 -
@OscarAkaElvis - I don't know anything about this, but https://docs.docker.com/config/containers/container-networking/ says "By default, when you create a container, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network, use the --publish or -p flag." If that is true, then the setup for tomcat must be doing something beyond the default Docker config. What reference did you find that describes "default iptables rules for docker"? – ToolmakerSteve Mar 29 '19 at 20:12
-
You can check the iptables rules... to access to a container from the host is not accesing "from outside world" and it can be done as default. – OscarAkaElvis Mar 31 '19 at 01:34
-
1The format option is outdated and no longer works with newer docker engines, check answer by Nima Ghoroubi. – Jesus H Oct 30 '19 at 14:35
-
While the latest edit works, it, sadly, only shows the IPv4 address of the container, not its IPv6. – Roovy May 30 '22 at 10:08
Use --format
option to get only the IP address instead whole container info:
sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' <CONTAINER ID>

- 72,056
- 11
- 123
- 141

- 557
- 5
- 8
-
4In windows host use double quotes instead of single quote. E.g. sudo docker inspect --format "{{ .NetworkSettings.IPAddress }}"
Reference: [link](https://github.com/docker/toolbox/issues/433) – Vineet Sajwan Jun 21 '19 at 11:22 -
1new format of info about container [here](https://docs.docker.com/engine/reference/commandline/inspect/#get-an-instances-ip-address) – Igor Jan 22 '20 at 15:30
For modern docker engines use this command :
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
and for older engines use :
docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_name_or_id

- 652
- 6
- 11
-
As mentioned and linked in the comment by @sanyash up in the original OP area. – Jesse Chisholm Jan 24 '20 at 20:10
-
Template parsing error: template: :1: unexpected unclosed action in command – GML-VS May 29 '20 at 14:54
if you want to obtain it right within the container, you can try
ip a | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | grep 172.17

- 4,188
- 4
- 41
- 85
You can start your container with the flag -P
. This "assigns" a random port to the exposed port of your image.
With docker port <container id>
you can see the randomly choosen port. Access is then possible via localhost:port
.

- 61
- 2
For my case, below worked on Mac:
I could not access container IPs directly on Mac. I need to use localhost
with port forwarding, e.g. if the port is 8000, then http://localhost:8000
See https://docs.docker.com/docker-for-mac/networking/#known-limitations-use-cases-and-workarounds
The original answer was from: https://github.com/docker/for-mac/issues/2670#issuecomment-371249949

- 744
- 8
- 20
If you want to view the IP address from within the running container, /etc/hosts
file is a great place to look at. Now, to uniquely identify the entry within the hosts
file, it is a good practise to run the container with the -h
option. Sample commands are given below:
- Run the container with
-h
set:
docker run -td -h guju <image name>
- Log in to the running container and view the
/etc/hosts
file. It will show an entry like this:
172.17.0.5 guju

- 5,239
- 1
- 43
- 24
this will list all containers' IP addresses
while read ctr;do
sudo docker inspect --format "$ctr "'{{.Name}}{{ .NetworkSettings.IPAddress }}' $ctr
done < <(docker ps -a --filter status=running --format '{{.ID}}')

- 1,321
- 10
- 11
-
Code dumps do not make for good answers. You should explain *how* and *why* this solves their problem. I recommend reading, "[How do I write a good answer?"](//stackoverflow.com/help/how-to-answer). This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive feedback/upvotes from users, when the code is explained. – John Conde Feb 27 '21 at 00:05
You can not access the docker's IP from outside of that host machine.
If your browser is on another machine better to map the host port to container port by passing -p 8080:8080
to run command.
Passing -p
you can map host port to container port and a proxy is set to forward all traffix for said host port to designated container port.

- 721
- 3
- 6