在 Docker 容器中使用非 root 用户运行 sudo 命令

在 Docker 容器中使用非 root 用户运行 sudo 命令

我有这个Dockerfile:

FROM ubuntu:17.04

# Must have packages
RUN apt-get update && apt-get install -y nano zsh curl git

# Instal Oh my Zsh
RUN bash -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
RUN sed -i -- 's/robbyrussell/sonicradish/g' /root/.zshrc 

# Add none root user
RUN adduser admin
USER admin

我正在使用 zsh shell 连接管理员用户。

docker exec -ti linux zsh

我正在添加非 root 用户(管理员)。

我仍然想sudo用这个用户执行命令,但是出现错误:

$ sudo apt-get install vim
zsh: command not found: sudo

与 bash shell 相同的消息。

如何以sudo非 root 用户身份运行命令?

当我不使用时,sudo我会收到权限错误:

$ apt-get install vim
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

答案1

这就是我会做的

FROM ubuntu:17.04

# Must have packages
RUN apt-get update && apt-get install -y vim nano zsh curl git sudo

# Install Oh my Zsh
RUN bash -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
RUN sed -i -- 's/robbyrussell/sonicradish/g' /root/.zshrc 

# Add none root user
RUN  useradd admin && echo "admin:admin" | chpasswd && adduser admin sudo
USER admin

答案2

只需创建非 root 用户并将其添加到sudoers团体:

FROM ubuntu:17.04

RUN apt-get update
RUN apt-get install sudo

RUN adduser --disabled-password --gecos '' admin
RUN adduser admin sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

USER admin

相关内容