I use a bash script to update the /etc/hosts
file of a pod with static hostname and default IP of other pods. when ever pod restarts or manually forced to delete. the new pod created will not have the updated /etc/hosts
file? how to handle this
You can use hostAliases in pod template to add static host entries in pods /etc/hosts file, you can check here Adding host entries in pods
Do absolutely nothing; don't even try to manage /etc/hosts
files. Docker provides internal DNS resolution between containers (Networking in Compose has a clearer description) so you can generally use other containers' --name
as host names directly.
If you're using docker run
commands you need to manually docker network create
a network (to create a "user-defined bridge" for the first link).
# Create a non-default network
docker network create some_network
# Run a server on that network
docker run -d --net some_network --name server nginx
# On the same network; uses the other container's name
# "server" as a host name
docker run --rm --net some_network \
busybox \
wget -O- http://server
If you're running this under Docker Compose, it does all of this setup for you (again, see Networking in Compose; the Compose default network is named default
but is not "the default bridge"). If you're running in another cluster environment (Kubernetes, Nomad) it will have its own similar DNS resolution system.