如何编写docker脚本,以便sudo在Docker用户中工作?

时间:2017-04-07 07:56:51

标签: linux bash ubuntu docker sudo

我使用Ubuntu运行了一个空的Docker容器并创建了一个sudo用户:

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

当我以ubiwan身份登录时,密码似乎不是mosesdocker指定的useradd -m -p mosesdocker -s /bin/bash ubiwan。我输入了正确的密码,但我得到了:

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

为什么会这样?

可以在Docker中使用sudo吗?什么是#34;建议&#34;在Docker中执行sudo个操作?

如何编写docker脚本,以便sudo在Docker用户中工作?

1 个答案:

答案 0 :(得分:1)

您需要使用passwd以root身份为用户设置密码。

如果您以ubiwan身份登录,首先输入exit。 然后作为根类型:

passwd ubiwan

为此用户提供密码。 然后您可以使用sudo作为具有此密码的ubiwan用户