I’m encountering a permission issue while trying to execute a script within a Docker container that modifies the host operating system’s file system. Specifically, I’m getting a "sed: couldn’t open a temporary file" error, as shown below:
sed -i 's[ ]root=[^ ]*/ root=${mender_kernel_root}/' work/rpi/cmdline.txt
sed: couldn't open temporary file work/rpi/sedqWUbSX: Permission denied
The problem exists only with sed
command and its temporary files; other commands create/read/write/delete files in the work
directory with no errors.
I suspect it’s related to permissions, but I’m not sure how to resolve this issue. Can anyone provide guidance on how to troubleshoot and fix this "sed" command permission denied error when running the script in Docker? Any insights or solutions would be greatly appreciated.
Environment: Mac OS Sonoma as host OS, Docker Desktop 4.24.0, Docker Engine 24.0.6, container uses Ubuntu 20.04
P.S. Unfortunately, replacement of sed
with other approaches is not an option for me. Switching to a different host OS would also be desirable to avoid.
2
Answers
I believe its permission problem since
sed
creates a temporary file. What You might like to try and check if it works or not:instead of inplace editing with
sed
(-i
), usesed
and redirect it output to a temporary file, thencat
temporary file intowork/rpi/cmdline.txt
something like this:
do not redirect
sed
directly towork/rpi/cmdline.txt
since redirects starts before executing the command and it will remove content inwork/rpi/cmdline.txt
before executingsed
You can change the permissions for read, write and execute using the chmod command in shell as:
And similarly for write permissions u can add +w in the above command: