Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Docker parent host ip into container's hosts file

Tags:

docker

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,

like image 851
Idan Gozlan Avatar asked Nov 11 '14 11:11

Idan Gozlan


People also ask

How do I assign an IP address to a Docker container?

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.

Can a Docker container have its own IP address?

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.

How do you expose a Docker host?

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.


1 Answers

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]
like image 70
Bryan Avatar answered Nov 16 '22 02:11

Bryan