skip to Main Content

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


  1. 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), use sed and redirect it output to a temporary file, then cat temporary file into work/rpi/cmdline.txt

    something like this:

    sed 's[ ]root=[^ ]*/ root=${mender_kernel_root}/' work/rpi/cmdline.txt > /tmp/this
    
    cat /tmp/this > work/rpi/cmdline.txt
    
    
    

    do not redirect sed directly to work/rpi/cmdline.txt since redirects starts before executing the command and it will remove content in work/rpi/cmdline.txt before executing sed

    Login or Signup to reply.
  2. You can change the permissions for read, write and execute using the chmod command in shell as:

    sudo chmod +r work/rpi/cmdline.txt
    

    And similarly for write permissions u can add +w in the above command:

    sudo chmod +w work/rpi/cmdline.txt
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search