2017-04-07 31 views
-2

我已經跑了Ubuntu的空泊塢窗容器並創建了一個用戶sudo這樣:如何編寫docker腳本,以便sudo在Docker用戶中工作?

[email protected]$ docker run -it ubuntu bash 

[email protected]:/# apt-get update 
[email protected]:/# apt-get -y install sudo 

[email protected]:/# useradd -m -p mosesdocker -s /bin/bash ubiwan 
[email protected]:/# usermod -aG sudo ubiwan # add user to sudo list 
[email protected]:/# su - ubiwan # login to the ubiwan user 

當我登錄的ubiwan,密碼似乎並沒有被mosesdockeruseradd -m -p mosesdocker -s /bin/bash ubiwan規定。我輸入了正確的密碼,但我得到:

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

[email protected]:~$ 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?什麼是「建議」在Docker中執行sudo操作?

如何編寫docker腳本以便sudo在Docker用戶中工作?

+3

Stack Overflow是編程和開發問題的網站。這個問題似乎與題目無關,因爲它不涉及編程或開發。請參閱幫助中心的[我可以詢問哪些主題](http://stackoverflow.com/help/on-topic)。也許[請求Ubuntu](http://askubuntu.com/)或[Unix&Linux Stack Exchange](http://unix.stackexchange.com/)會是一個更好的地方。另請參閱[我在哪裏發佈有關Dev Ops的問題?](http://meta.stackexchange.com/q/134306) – jww

回答

1

您需要使用passwd將用戶的密碼設置爲root。

如果您以ubiwan身份登錄,首先輸入exit。 然後,當根型:

passwd ubiwan 

給該用戶的密碼。 然後,您可以使用sudo作爲具有此密碼的ubiwan用戶

+0

但是需要在Docker腳本中非交互式地設置密碼。 – alvas

+1

RUN echo「user ALL =(root)NOPASSWD:ALL」> /etc/sudoers.d/user&&\ chmod 0440 /etc/sudoers.d/user 也許你需要類似上面的東西?取自http://gbraad.nl/blog/non-root-user-inside-a-docker-container.html –

+0

謝謝!任何想法爲什麼'-p'對'useradd'沒有影響? – alvas

相關問題