How to override hosts files from a docker instance with /etc/rc.local on debian 11/ 12 or ubuntu variants

Create a file /etc/rc.local with the following content:


#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0

Let’s make it executable:


chmod +x /etc/rc.local

Reload the systemd manager configuration:


systemctl daemon-reload

Then we start the rc-local daemon:


systemctl start rc-local

And then we check the status of rc-local to confirm it ran OK:


systemctl status rc-local
Let’s create the file:

nano /root/startup.sh

 

Paste the following content:

#!/bin/sh

container_id=”25f17a3706d2″

# Define the entries you want to add
entries="192.168.1.11 test1.exemple.com
192.168.1.12 test2.exemple.com
192.168.1.13 test3.exemple.com
192.168.1.14 test4.exemple.com"

# Add entries to /etc/hosts
echo "$entries" | while read entry; do
docker exec "$container_id" /bin/sh -c "echo '$entry' >> /etc/hosts"
done
————————
 chmod +x /root/startup.sh
edit the /etc/rc.local
paste:
sh /root/startup.sh
before
exit 0

Leave a Reply

Your email address will not be published. Required fields are marked *