Using sed on a file that was binded to docker breaks the binding

Steps taken to reproduce the issue

Step 1: create a file in the host

host: echo "old value" > .env_container1

Step 2: Create a container binding host’s ./env_container1 to container’s /.env

host: docker run -ti -v $(pwd)/.env_container1:/.env alpine sh

Step 3:

host: sed -i 's/old/new/g' .env_container1

Step 4:
The expected behaviour would be that the old will be substituted to new. However, when I run cat test it outputs the old value

container: cat .env

May I know why will sed break the binding between the host and the container? This will not be an issue if i mount a directory and sed the file from the directory. But at the same time i would like the file in the host filesystem and the container filesystem to have a different name

sed renames the original file and then creates a new with the name of the old one. It is clearer what happens if you use sed -i.old ....
On my system (Docker Desktop + WSL) I get with both Alpine and Debian a ‘Device or resource busy’ error when I try to edit in-place.

1 Like

Cool! Thank you. Do you know have any source for

Blockquote
sed renames the original file and then creates a new with the name of the old one.

Sorry but i dont understand what you are trying to do with this command sed -i.old ... ? Is there some typo here?

Of course, here. :wink:

From the sed manpage:

-i[SUFFIX], --in-place[=SUFFIX]
  edit files in place (makes backup if SUFFIX supplied)

This means if you type

sed -i.old 's/old/new/g' .env_container1

sed will rename .env_container1 to .env_container1.old and create a new file with name .env_container1.

Hmm… haha I think this is suffice to prove your point :rofl:

image

sed will rename .env_container1 to .env_container1.old and create a new file with name .env_container1 .

Nice! alright thank you so much. I understand now