Linux – How to Run Sudo Commands with a System User

linuxsusudosystem-userUbuntu

I want to run a minecraft server in Docker, so I started following this guide.
In the guide, it is advised to create a system user with a home directory (/opt/minecraft), but without a password (you should not be able to "login" with this user) to run the server on. It uses the following command: sudo useradd -r -m -U -d /opt/minecraft -s /bin/bash minecraft.

I switched to the newly created minecraft "system user" –> sudo su - minecraft.

The problem
So, I want to use docker or docker-compose, and this is where the problem arises:

  • When I try to run docker-compose using sudo, it asks me for a password (which, obviously, this user does not have).
  • When I try it without sudo, I get cannot create user data directory: /opt/minecraft/snap/docker/1458: Permission denied.

I'm not really sure what to do here. I can't run docker. I guess I don't want to remove the password from the user (would be a security risk), and I don't want to add a password for the user (then you can login via ssh or something). Am I approaching this the wrong way?

Best Answer

Look into adding a privileged docker group https://docs.docker.com/engine/install/linux-postinstall/

This will allow you to run docker commands without requiring root or sudo