Skip to content
Advertisement

How to write the docker script such that sudo works in Docker user? [closed]

I’ve ran an empty Docker container with Ubuntu and created a sudo user as such:

user@server$ docker run -it ubuntu bash

root@6da1c5bc7f93:/# apt-get update
root@6da1c5bc7f93:/# apt-get -y install sudo

root@6da1c5bc7f93:/# useradd -m -p mosesdocker -s /bin/bash ubiwan
root@6da1c5bc7f93:/# usermod -aG sudo ubiwan  # add user to sudo list
root@6da1c5bc7f93:/# su - ubiwan  # login to the ubiwan user

And when I logged in as ubiwan, the password doesn’t seem to be mosesdocker as specified by useradd -m -p mosesdocker -s /bin/bash ubiwan. I’ve typed the correct password but I get:

To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubiwan@6da1c5bc7f93:~$ sudo su
[sudo] password for ubiwan: 
Sorry, try again.
[sudo] password for ubiwan: 
Sorry, try again.
[sudo] password for ubiwan: 
sudo: 3 incorrect password attempts

Why is that so?

Is it possible to use sudo in Docker? What is the “suggested” to perform sudo actions in Docker?

How to write the docker script such that sudo works in Docker user?

Advertisement

Answer

You need to set the password for the user as root using passwd.

First of all type exit if you are logged in as ubiwan. Then as root type:

passwd ubiwan

Give a password for this user. Then you can use sudo as the ubiwan user with this password

Advertisement