I need to get the ip address in the network (eth0) of the server on which this container is located from inside the docker container.
inside the container (alpine:3.18) with ifconfig:
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:04
inet addr:172.17.0.4 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:65535 Metric:1
RX packets:49 errors:0 dropped:0 overruns:0 frame:0
TX packets:54 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:6433 (6.2 KiB) TX bytes:5904 (5.7 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
and outside the container, the host has:
eth0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=6460<TSO4,TSO6,CHANNEL_IO,PARTIAL_CSUM,ZEROINVERT_CSUM>
ether 9c:3e:53:92:e9:86
inet6 fe80::fb:c60b:fa61:92bd%en0 prefixlen 64 secured scopeid 0xc
inet 192.168.1.63 netmask 0xffffff00 broadcast 192.168.1.255
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
how can I get it inside the container? is it possible to throw something into a container, such as a docker socket:
-v /var/run/docker.sock:/var/run/docker.sock
This is a continuation of the question "launching new containers from a container"
I understand that there is an option to do it outside the container by reading the file, but I would like to know how to do it through the command
You can grab the host IP address and pass it to the container as an environment variable.
After experimenting a bit, I came up with the command hostname -I | awk '{print $1;}'
to get the IP address. I don't know if that always works because the host might have multiple IP addresses and I'm not certain that this command selects the correct one. But it seems to work on my machine.
To illustrate, if you run
docker run --rm -e HOST_IP=$(hostname -I | awk '{print $1;}') debian env
you should see that the HOST_IP variable is set in the container.