Can I get ip address inside my docker container?

DockerIp Address

Docker Problem Overview


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.

Docker Solutions


Solution 1 - Docker

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).

Solution 2 - Docker

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

Solution 3 - Docker

Why not something as simple as:

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

or

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

Solution 4 - Docker

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

Solution 5 - Docker

I found solution to my problem:

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

It's print something like: '172.17.0.135'

Solution 6 - Docker

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,}.

Solution 7 - Docker

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

Solution 8 - Docker

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

Solution 9 - Docker

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.]+'"

Solution 10 - Docker

The older answers have been very helpful.

This worked for me; it appends the export command into the .bashrc file in the home (~/) directory inside the container

# Add this line to Dockerfile
RUN echo 'export CONTAINER_IP="$(hostname -i)"' >> ~/.bashrc

Now you can access the computed value of CONTAINER_IP environment variable inside the container.

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
QuestionKroidView Question on Stackoverflow
Solution 1 - DockerVictor RoetmanView Answer on Stackoverflow
Solution 2 - Dockeruser2915097View Answer on Stackoverflow
Solution 3 - DockerRafalView Answer on Stackoverflow
Solution 4 - DockerPeter LyonsView Answer on Stackoverflow
Solution 5 - DockerKroidView Answer on Stackoverflow
Solution 6 - DockerCole TierneyView Answer on Stackoverflow
Solution 7 - DockerXtraSimplicityView Answer on Stackoverflow
Solution 8 - Dockeruser11231877View Answer on Stackoverflow
Solution 9 - DockerQuinn VissakView Answer on Stackoverflow
Solution 10 - Dockermbao01View Answer on Stackoverflow