0

If I use the --user argument of docker run, changes (like a simple mkdir for example) will appear as if performed by that (non-root) user. However, I won't be able to run commands like apt inside the container then, because they require root privileges. By default, there is no pre-installed sudo in debian or ubuntu Docker images.

What's an elegant way to overcome this issue?

2
0

If you know when and where the changes will be written, you could just use chown on those files afterwards:

  • Pass user and group ID (id -u, id -g) of the host (non-root) user to the Docker container with the --env argument
  • Run your commands/script (inside the container) as root
  • Finally, run chown (also still inside the container) to make the changes look like they've been performed by the host user
0
0

I don't think, this is a particularly elegant solution, but it's possible to create a dummy user inside the container whose user and group ID match those of the (non-root) host user:

  • Pass user and group ID (id -u, id -g) of the host (non-root) user to the Docker container with the --env argument
  • Create a new group matching the group ID of the host (non-root) user

    addgroup --gid $DUMMY_GID dummy
    
  • Create a new user matching the user ID of the host (non-root) user

    adduser --uid $DUMMY_UID --gid $DUMMY_GID --disabled-password --gecos "" dummy
    
  • Commands which require root privileges will work

  • Run your other commands/script with runuser

    runuser -u dummy -- command
    
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.