Ubuntu n00b here. I am tinkering with a Docker container that I created using the following simple Dockerfile:

FROM ubuntu
CMD ["tail","-f","/dev/null"]

Inside the container, as the root user, I try running visudo and get the following response:

bash: visudo: command not found

I ran ls /etc and it seems I don't have a sudoers file either.

Is there something special I should have put in my Dockerfile to make these exist?

  • Why would you want to use sudo in a docker container ? – pLumo Dec 19 '18 at 8:53
  • @RoVo My next step is going to be setting up other users in the container. – Shaul Behr Dec 19 '18 at 8:54
  • 1
    Just asking, as normal use case would be one service per container => only a single user needed ... But of course you're free to do whatever ;-) – pLumo Dec 19 '18 at 8:56
  • visudo is meant to interactively edit the sudoers file. If you really need to change sudo privs in a container, you only need to COPY at container build time a sudoers file that you have prepared outside the container. – xenoid Dec 19 '18 at 9:32

I worked it out from another answer: sudo is not installed by default. To install sudo:

apt-get update
apt-get install sudo

When you built the container, you are root so you can create users without sudo.

If you are deriving a container image that already has a USER directive, you can still become root again with USER root before using privileged commands. You can later issue another USER directive to become the execution user again. In other words:

An image that sets a non privilege user:

# "plainuser" image
FROM debian
RUN groupadd -g 1000 appgroup && useradd -g appgroup -u 1000 appuser
USER appuser
ENTRYPOINT ["/usr/bin/id"]

Run with docker run --rm plainuser to check that it indeed runs with user appuser.

Let's derive it:

# "otheruser" image
FROM plainuser
USER root  # Become root again
RUN groupadd -g 1001 appgroup2 && useradd -g appgroup2 -u 1001 appuser2
USER appuser2 # Set new execution user

Run with docker run --rm otheruser.

  • Thanks, this is all very useful info. Doesn't directly answer my question (which my answer does), but adds useful info for a n00b like me :-) – Shaul Behr Dec 19 '18 at 13:15
  • Still can't figure out why you need sudoers in a container, and even less visudo. IMHO you're doing something the wrong way. – xenoid Dec 19 '18 at 16:24
  • To satisfy your curiosity, I'm working on a Windows computer. We are creating a package that needs to be installed on a clean Ubuntu device, including setup of different users. So I need the Docker container to be able to test and tinker with our installation script. – Shaul Behr Dec 20 '18 at 8:37
  • 1
    Best test that with a VM, containers and VM are different animals, VMs are much closer to individual devices (for instance you wouldn't need to install sudo in a VM, nor in your target system...). This doesn't explain the visudo requirement, even in that case you can copy a sudoers file or edit it with a script or add your stuff in /etc/sudoers.d. – xenoid Dec 20 '18 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.