如何在 docker 容器内使用 sudo? [英] How to use sudo inside a docker container?

查看:58
本文介绍了如何在 docker 容器内使用 sudo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆