How to get IP address of running docker container

DockerContainersDocker for-Mac

Docker Problem Overview


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?

Docker Solutions


Solution 1 - Docker

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>

Solution 2 - Docker

Use --format option to get only the IP address instead whole container info:

sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' <CONTAINER ID>

Solution 3 - Docker

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

Solution 4 - Docker

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

Solution 5 - Docker

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.

Solution 6 - Docker

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

Solution 7 - Docker

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:

  1. Run the container with -h set:

docker run -td -h guju <image name>

  1. Log in to the running container and view the /etc/hosts file. It will show an entry like this:

172.17.0.5 guju

Solution 8 - Docker

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}}')

Solution 9 - Docker

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3067875View Question on Stackoverflow
Solution 1 - DockerOscarAkaElvisView Answer on Stackoverflow
Solution 2 - DockerNilesh PatelView Answer on Stackoverflow
Solution 3 - DockerTiGoView Answer on Stackoverflow
Solution 4 - DockerIlya YevlampievView Answer on Stackoverflow
Solution 5 - DockergSchtView Answer on Stackoverflow
Solution 6 - DockerLavandeView Answer on Stackoverflow
Solution 7 - DockerBinita BharatiView Answer on Stackoverflow
Solution 8 - Docker张馆长View Answer on Stackoverflow
Solution 9 - DockerAkash SharmaView Answer on Stackoverflow