在 Alpine Docker 容器中创建用户的正确方法,以便 sudo 正常工作



当尝试使用alpine 3.8在 docker 容器中执行sudo时,我得到以下输出。

我已使用docker exec -i -t MYIMAGE /bin/bash登录到容器

bash-4.4$ whoami
payara
bash-4.4$ sudo -s
bash-4.4$ whoami
payara
bash-4.4$ su root
su: incorrect password
bash-4.4$

我的 docker 文件包含以下与用户相关的命令,用于尝试专门为 payara设置用户。如果可能的话,我希望 sudo 正常工作。

DockerFile

FROM "alpine:latest"
ENV LANG C.UTF-8
ENV http_proxy 'http://u:p@160.48.234.129:80'
ENV https_proxy 'http://u:p@160.48.234.129:80'
RUN apk add --no-cache bash gawk sed grep bc coreutils git openssh-client libarchive libarchive-tools busybox-suid sudo            
RUN addgroup -S payara && adduser -S -G payara payara
RUN echo "payara ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
# CHANGE TO PAYARA USER
USER payara
... rest of setup.

man sudo

-s, --shell
Run the shell specified by the SHELL environment variable if it is set or the shell specified by the invoking user's password database entry.

您既没有设置SHELL变量,也没有在/etc/passwd中为用户payara设置正确的(交互式(默认 shell。这是因为您正在创建一个系统用户(-S( - 该用户有一个默认的 shell/bin/false(它只是以退出代码退出1- 您可以在不成功后与echo $?sudo -s进行检查(。

您可以通过不同的方式克服此问题:

a( 指定SHELL变量:

bash-4.4$ SHELL=/bin/bash sudo -s
bed662af470d:~# 

b( 使用su,它将使用默认的root的 shell:

bash-4.4$ sudo su -
bed662af470d:~# 

c( 只需直接使用sudo运行所需的特权命令,而无需生成交互式 shell。

相关内容

最新更新