在码头容器内使用sudo [英] Using sudo inside a docker container

查看:111
本文介绍了在码头容器内使用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.

这是一个简单的Docker文件,用于此目的:

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

我不知道如何管理这个。有什么建议么? Thx很多!

I've no idea how to manage this. Any suggestions? Thx a lot!

推荐答案

刚刚得到它。正如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

这篇关于在码头容器内使用sudo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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