通常,docker容器使用root用户运行。我想使用一个不同的用户,这是没有问题使用docker的user指令。但是这个用户应该能够在容器内使用sudo。该命令缺失。
下面是一个简单的Dockerfile:
FROM ubuntu:12.04
RUN useradd docker && echo "docker:docker" | chpasswd
RUN mkdir -p /home/docker && chown -R docker:docker /home/docker
USER docker
CMD /bin/bash
运行这个容器,我以用户“docker”登录。当我尝试使用sudo时,没有找到该命令。所以我尝试在我的Dockerfile中使用sudo包安装
RUN apt-get install sudo
这将导致无法定位包sudo
主要思想是,您需要根据容器创建一个根用户。
主要命令:
RUN echo "bot:bot" | chpasswd
RUN adduser bot sudo
第一个将字面值bot:bot发送给chpasswd, chpasswd创建了用户bot,密码为bot, chpasswd做的是:
The chpasswd command reads a list of user name and password pairs from standard input and uses this information to update a group of existing users. Each line is of the format:
user_name:password
By default the supplied password must be in clear-text, and is encrypted by chpasswd. Also the password age will be updated, if present.
我假设第二个命令将用户bot添加为sudo。
完整的docker容器来玩:
FROM continuumio/miniconda3
# FROM --platform=linux/amd64 continuumio/miniconda3
MAINTAINER Brando Miranda "me@gmail.com"
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ssh \
git \
m4 \
libgmp-dev \
opam \
wget \
ca-certificates \
rsync \
strace \
gcc \
rlwrap \
sudo
# https://github.com/giampaolo/psutil/pull/2103
RUN useradd -m bot
# format for chpasswd user_name:password
RUN echo "bot:bot" | chpasswd
RUN adduser bot sudo
WORKDIR /home/bot
USER bot
#CMD /bin/bash
主要思想是,您需要根据容器创建一个根用户。
主要命令:
RUN echo "bot:bot" | chpasswd
RUN adduser bot sudo
第一个将字面值bot:bot发送给chpasswd, chpasswd创建了用户bot,密码为bot, chpasswd做的是:
The chpasswd command reads a list of user name and password pairs from standard input and uses this information to update a group of existing users. Each line is of the format:
user_name:password
By default the supplied password must be in clear-text, and is encrypted by chpasswd. Also the password age will be updated, if present.
我假设第二个命令将用户bot添加为sudo。
完整的docker容器来玩:
FROM continuumio/miniconda3
# FROM --platform=linux/amd64 continuumio/miniconda3
MAINTAINER Brando Miranda "me@gmail.com"
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ssh \
git \
m4 \
libgmp-dev \
opam \
wget \
ca-certificates \
rsync \
strace \
gcc \
rlwrap \
sudo
# https://github.com/giampaolo/psutil/pull/2103
RUN useradd -m bot
# format for chpasswd user_name:password
RUN echo "bot:bot" | chpasswd
RUN adduser bot sudo
WORKDIR /home/bot
USER bot
#CMD /bin/bash