I'm new to Docker and trying to understand what is the best way to insert docker parent host ip into container hosts file.
I'm using the following command in my Dockerfile
RUN /sbin/ip route|awk '/default/ { print $3,"\tdockerhost" }' >> /etc/hosts
but sometimes hosts ip get change so its non relevant anymore...
The reason to do that, if you ask yourself, is that i need to access another 2 dockers containers (and link not offer this feature).
Thanks,
When you connect an existing container to a different network using docker network connect , you can use the --ip or --ip6 flags on that command to specify the container's IP address on the additional network. In the same way, a container's hostname defaults to be the container's ID in Docker.
The answer is: you can configure it. Create the container with --network host and it will use the host ip. There are other modes. Refer to the documentation above.
In order to make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, we can use the -P or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.
The --add-host
option is made for this. So, in your docker run
command, do something like:
docker run --add-host dockerhost:`/sbin/ip route|awk '/default/ { print $3}'` [my container]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With