问题描述
通常,docker容器使用用户 root 运行.我想使用其他用户,使用docker的USER指令没问题.但是此用户应该能够在容器内使用 sudo .该命令丢失.
Normally, docker containers are run using the user root. I'd like to use a different user, which is no problem using docker's USER directive. But this user should be able to use sudo inside the container. This command is missing.
这是一个用于此目的的简单Dockerfile:
Here's a simple Dockerfile for this purpose:
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时,找不到命令.所以我尝试使用
Running this container, I get logged in with user 'docker'. When I try to use sudo, the command isn't found. So I tried to install the sudo package inside my Dockerfile using
RUN apt-get install sudo
这将导致无法找到软件包sudo
推荐答案
就可以了.正如regan所指出的,我必须将用户添加到sudoers组.但是主要原因是我忘了更新存储库缓存,所以apt-get找不到sudo包.现在正在工作.这是完成的代码:
Just got it. As regan pointed out, I had to add the user to the sudoers group. But the main reason was I'd forgotten to update the repositories cache, so apt-get couldn't find the sudo package. It's working now. Here's the completed code:
FROM ubuntu:12.04
RUN apt-get update && \
apt-get -y install sudo
RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo
USER docker
CMD /bin/bash
这篇关于如何在Docker容器中使用sudo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!