In my ubuntu docker container, if I run su - root
and input my password i’m successfully logged in as root but if I run a command for instance sudo apt install <package>
it tells me my password is invalid. I’m using docker-compose and in my Dockerfile file I have RUN echo 'root:mypassword' | chpasswd
, another thing that might be worth pointing out is that sudo wasn’t preinstalled on the ubuntu image, I had to install it in the Dockerfile. In my docker-compose.yml I have a custom user set user: myuser
.
My question is, why does my password work when doing su - root
but not prepending sudo such as in sudo apt install <package>
and how do I fix this?
Something I had tried was adduser root sudo
(as root) but it didn’t work. Also, I know I can log in to docker as root but I’m looking for the ability to log in as a normal user and use sudo
as needed. To give an example of the problem:
myuser@d8b8e2c9c5dd:~/store$ su - root
Password:
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
root@d8b8e2c9c5dd:~# exit
logout
myuser@d8b8e2c9c5dd:~/store$ sudo su
[sudo] password for myuser: // same password used here but it says it's incorrect
Sorry, try again.
2
Answers
As someone else pointed out, it's not the root password that you enter but the password of your current user. Something else required is to add your user to the sudo group
RUN adduser myuser sudo
and it's fixed.when you use
su - root
you have to put the root password. because it’s a login process to the root user.but when you use
sudo
make sure you enter themyuser
password not theroot
one. because althoughsudo su
logs you to theroot
user. it usesmyuser
permission to do so.