I have this Dockerfile:

FROM ubuntu:17.04

# Must have packages
RUN apt-get update && apt-get install -y nano zsh curl git

# Instal Oh my Zsh
RUN bash -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
RUN sed -i -- 's/robbyrussell/sonicradish/g' /root/.zshrc 

# Add none root user
RUN adduser admin
USER admin

I'm connecting with the admin user with the zsh shell.

docker exec -ti linux zsh

I'm adding a non-root user (admin).

I still want to execute a sudo command with this user, but it errors out:

$ sudo apt-get install vim
zsh: command not found: sudo

Same message with bash shell.

How can I run sudo commands with a non-root user?

When I don't use sudo I get a permission error:

$ apt-get install vim
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
  • did you try sshing into the container? – Eliran Malka Apr 18 '17 at 22:18
  • 1
    @EliranMalka What's the point of SSH in a container? exec bash is the "more correct way" to get into a container – cricket_007 Apr 19 '17 at 3:29
  • 1
    @Kevin What command do you actually want to run? You have nano already being installed, so why try to install vim? And if you want vim, then install it – cricket_007 Apr 19 '17 at 3:30
  • vim is just an example. I just want to be able to install stuff as an none root user in my docker container. – Kevin Apr 19 '17 at 20:32

Add sudo to your install list; most Docker containers don't ship with it. I don't know about Ubuntu, but I know that's the case with Fedora and CentOS.

  • it works but now I have this message when I want to install something with the sudo command and the admin user sudo: no tty present and no askpass program specified – Kevin Apr 19 '17 at 20:31
  • That can be fixed by editing /etc/sudoers. And it's another question. Since this answered yours, please check the box and "accept" it. Thanks. – Aaron D. Marasco Apr 19 '17 at 23:33

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.