Blog
How to add sudo access for the docker command?
Letâs imagine a hypothetical situation where the admins give you access to the docker command, but not to sudo or other critical resources your app needs to run properly. In Ubuntu, sudo permissions are stored in the /etc/sudoers file, and thatâs the one weâll need to modify.
By default, it belongs to the root user, so you canât change it directly.
Mount the hostâs /etc directory into the container. The file youâre interested in will be in the /tmp/etc directory.
docker run --rm -it -v /etc:/tmp/etc ubuntu:latest bash
Make a backup of the /etc/sudoers file so that if something goes wrong, we can quietly roll back.
cp /tmp/etc/sudoers /tmp/etc/sudoers.bak
Change the username in the command below to your own.
echo "your-username ALL=(ALL) ALL" >> /etc/sudoers
Log back into the machine and do something that only root could do before, for example, after running the command below, the system will prompt you for a password and create a file.
sudo touch /etc/test.txt
Of course, you have more room to maneuver, like modifying users, groups, and so on.