How to run app inside Docker as non-root user?

Sometimes, we want to run app inside Docker as non-root user.

In this article, we’ll look at how to run app inside Docker as non-root user.

How to run app inside Docker as non-root user?

To run app inside Docker as non-root user, we can use the adduser command to add a non-root user.

For instance, in our Dockerfile, we add

adduser --disabled-password --gecos '' r
adduser r sudo
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
su -m r -c /home/r/script.sh

to add the user r which is a non-root user.

We then run su with user r to run the /home/r/script.sh script.

Conclusion

To run app inside Docker as non-root user, we can use the adduser command to add a non-root user.