Docker sed: cannot rename /etc/sedl8ySxL: Device or resource busy
Hello,
Today I am talking about a dirty fix I had to use when trying to modify /etc/hosts
in a Docker container.
I wrote my sed
script to alias localhost and add a couple additional hostname mappings and run it from the Dockerfile
during the build.
However…
Docker sed: cannot rename /etc/sedl8ySxL: Device or resource busy
Docker treats /etc/hosts
differently. It overwrites it whenever it wants without any regard for your modifications.
To solve this you can run this workaround:
cp /etc/hosts ~/hosts.new
sed ... ~/hosts.new
cp -f ~/hosts.new /etc/hosts
You can change the sed command with that you want.
Example “sed” to add 127.0.0.1 `hostname` to your /etc/hosts.
sed -i s/^172.*$// ~/hosts.new
Enjoy
Unfortunately this didn’t help me, but the site header graphics gave me a good chuckle in the middle of me banging my head against a wall.
Hi,
cp -f ~/hosts.new /etc/hosts
does fail: cp: can’t create ‘/etc/hosts’: File exists
I’m however able to solve that issue thanks to the following command:
echo “$(cat ~/hosts.new)” > /etc/hosts
Best regards
Thanks for your comment!
Just
cat ~/hosts.new > /etc/hosts
is good enough
cp ~/hosts.new /etc/hosts,
will still work if you run the script using sudo
i.e $sudo ./{scriptname}
Yes of course
Solved my problems. Thank you
It’s worth noting that this must be done only on ENTRYPOINT or CMD, or yet manually running an exec command. It should not work in a RUN clause, since Docker will rewrite /etc/hosts in every layer it creates in your dockerfile.
Just `sed -i` with `-c` option
“`
-c, –copy
use copy instead of rename when shuffling files in -i mode
“`
Hello!
Thank you for this fix.
It works with Laradock.
By the way, the cool header caught me off guard.
Regards
Thanks 😀
Thanks a lot.