在 alpine docker 容器中创建用户的正确方法,以便 sudo 正常工作
correct method to create user in alpine docker container so that sudo works correctly
当尝试使用 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.
当尝试使用 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.