Using sudo inside a docker container

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.

Here’s a simple Dockerfile for this purpose:

  • Vagrant + Docker - “not ready for rsync”
  • Environment variable in PHP-docker container
  • Override FROM image's ENV in Dockerfile
  • Unable to register docker selenium remote node to selenium hub remote host with docker
  • What is the difference between lmctfy and lxc
  • adding R package for installation from Github in 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
    

    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
    

    This results in Unable to locate package sudo

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

  • How to set subnet for Docker 1.9 overflow network?
  • Is there a way how to run LXD inside Docker?
  • Docker container for Laravel: PHP 7.x on CentOS 7.x
  • Using Laradock, the “display_errors” flag is not working
  • How to edit a .py script inside Docker Quickstart Terminal?
  • MUPX MeteorJS deploy: ssh-copy-id to Docker from Mac : port 22: Operation timed
  • One Solution collect form web for “Using sudo inside a docker container”

    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 will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.