Updating /etc/hosts without container restart

Hi,

In a compose file I have a section that provides extra_hosts. These entries go into the /etc/hosts file of the containers started by the compose up. If the IP address of any of the external hosts changes, I would like to get the /etc/hosts files of all containers updated. Currently I have to update the extra_hosts section and then restart the containers using docker-compose down and up.

Is it possible to get the compose file changes reflected in the container’s /etc/hosts file without restarting the containers?

Regards,
Yash

Hi Yash,

It isn’t possible, and even if it was, it probably wouldn’t be a good idea - if that container restarts after a failure for some reason, any changes to your /etc/hosts would be lost as well.

Note that you don’t have to bring your whole stack down when you make changes ; when you run docker-compose up, Compose will automatically detect what changed in your configuration and only recreate the containers that need it. If your main concern is downtime, this would reduce it significantly, provided your architecture is reasonably failure resilient.

It’s possible if you can re-build docker image as a strict. For example, docker run /entrypoint.sh file with content below:
#!/bin/bash
ln -s /data/drivers/hosts /etc/hosts
app_command
and docker run with command:
docker run -d --name update-hosts -v /data/drivers:/data/drivers
Then any update /data/drivers/hosts will change hosts files.

Or you can change /etc/hosts to /etc/drivers/hosts folder on Linux. It’s possible :slight_smile: