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.