68

How to get container ip address inside this container?

'docker inspect $hostname ...' not suitable, because I don't share /var/run/docker.sock host file to container.

5
  • For what usecase? If it is for a server, you'd usually just look at the local IP for incoming requests. Commented Dec 27, 2014 at 19:03
  • I will send ip to another container (example, nginx) and create upstream (redirect subdomain http request) from nginx to first container. Commented Dec 27, 2014 at 19:20
  • I think normally you'd do that with linking? Like when you start your nginx box, you specify --link firstcontainer:firstcontainer and then in nginx, you can actually just proxy to a hostname firstcontainer because the linking will automatically set up DNS to resolve it to the firstcontainer IP. Commented Dec 27, 2014 at 19:24
  • I can't link, because first I run nginx container. It seems strange, but in my case it's correct action. Commented Dec 27, 2014 at 19:32
  • Cool, just wanted to be sure there was a reason. Commented Dec 27, 2014 at 19:34

10 Answers 10

129

I can find the IP address with

hostname -i 

Of course, that may not be completely accurate if there is more than one interface.

Edit

Note: According to the hostname man page, hostname -i uses DNS to resolve the ip address, where hostname -I displays all the addresses except loopback, does not depend on DNS, and is recommended.

In all my Docker containers, -i and -I return the same results (but this is not the case on my desktop).

Sign up to request clarification or add additional context in comments.

3 Comments

To be more precise: hostname -i | awk '{print $1}' because there is a trailing space (and may be more than one IP)
@RomualdBrunet it should be hostname -I | awk '{print $1}' because -i gives single ip where -I gives multiple (if present)
When I connect a docker container to two networks, hostname -i and hostname -I both return 172.18.0.2 172.19.0.2 (but hostname -I does return a trailing space). I think the behavior of hostname -i depends on how dns is configured.
59

As the IP address is in the first line of /etc/hosts, you can do in a container the awk command that prints the first word of the first line of /etc/hosts

$ awk 'END{print $1}' /etc/hosts 172.17.0.14 

4 Comments

This seems to no longer be true. In a container on docker-engine 1.10.3, /etc/hosts has my ip address as the last line, and the loopback as the first line.
Now awk 'END{print $1}' /etc/hostsseems to do the job
Not necessarily true and completely unsafe
awk is overkill, maybe a simple sed command fits the need (removing spaces I guess?) sed -i 's/[ \t]*$//' "$1"
5

Why not something as simple as:

grep "`hostname`" /etc/hosts|awk '{print $1}' 

or

grep "$HOSTNAME" /etc/hosts|awk '{print $1}' 

1 Comment

This will return multiple IP addresses if your hostname happens to be a substring of some other hostname.
3

Normally you can use the linux program ifconfig to get IP addresses and other networking details. Your container may not have it, in which case you'll need to install it via apt-get or yum or your distro's package manager. A basic pipeline to get the IP address would be

ifconfig eth0 | grep "inet addr:" | cut -d : -f 2 | cut -d " " -f 1 

2 Comments

A container will not necessarily have an eth0 interface.
A container will also not necessarily have ifconfig installed.
1

I found solution to my problem:

/sbin/ip route|awk '/scope/ { print $9 }' 

It's print something like: '172.17.0.135'

Comments

1

You could also look for a line in /etc/hosts that ends with a container id and print the first field:

sed -n 's/^\([0-9\.]*\)[[:blank:]]*[0-9a-f]\{12,\}$/\1/p' /etc/hosts 

I'd use awk, but standard awk in dedian:jessie doesn't support regex quantifiers like {12,}.

Comments

1

If you prefer to use ip rather than ifconfig, on Linux you can do:

ip addr | grep inet | tr -s ' ' | cut -d ' ' -f 3 

This simply gets all of the IP addresses and interfaces, searches only for those starting with inet and returns only the 3rd column.

As a nice side-effect, this includes the subnet mask, too! (e.g. 172.17.0.2/16)

If you don't want the subnet mask, you can use:

ip addr | grep inet | tr -s ' ' | cut -d ' ' -f 3 | tr -d '/' 

NOTE: This shows ALL of the IPs in the container. You can use awk or sed to remove 127.0.0.1/16 and other unwanted IPs. :)

Comments

1

This may work on some containers if it has the "hostname" command.

docker ps to get the (container id)

docker exec -it (container id) hostname -i

1 Comment

Good guess, but no. The question explicitly states that they can't run the docker command inside the container.
1
FROM alpine # Upgrade grep so that it supports -Po flags RUN apk add --no-cache --upgrade grep ENV IPADDRESS "ip a show eth0 | grep -Po 'inet \K[\d.]+'" 

Comments

0

linux ip command is helpful

ip a s eth0 | grep -E -o 'inet [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' | cut -d' ' -f2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.