To change the default user in a running Docker container without specifying the user in each command, you can modify the container's entrypoint script to switch users after starting the main process. Here's a general approach:
Start Main Process as Root: Ensure the main process that requires root privileges starts first in the entrypoint script.
Switch to Non-Root User: After the main process initiates, use a command like su -user to switch to the desired non-root user within the script.
The following commands in the entrypoint script will run as the non-root user, and any Docker exec commands will default to this user unless otherwise specified.
Example Entrypoint Script:
#!/bin/bash # Start main process as root /root_process & # Switch to non-root user su - user # Keep the container running tail -f /dev/null
In this script, /root_process starts with root privileges, and then switches to the non-root user for all subsequent operations. This setup ensures that any Docker exec commands default to the non-root user without explicit user specification.