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/


Paste the following content:



# Define the entries you want to add

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

Leave a Reply

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