5

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?
  • I'm not that much familiar with docker, aren't you able to use root in image? can you add the output of which sudo to your question? – Ravexina Apr 18 '17 at 20:14
5

This is what I would do

FROM ubuntu:17.04

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

# Install 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  useradd admin && echo "admin:admin" | chpasswd && adduser admin sudo
USER admin
  • You can not run sudo in Dockerfile. It does not work :( ... even when I remove the sudo command – Kevin Apr 18 '17 at 14:14
  • Kevin, sorry I wasn't able to test docker before. Can you retry with the new example? – Grayson Kent Apr 18 '17 at 14:48
  • Still the same messages :( – Kevin Apr 18 '17 at 15:10
  • The command not found or that Docker can't run sudo? – Grayson Kent Apr 18 '17 at 15:12
  • 1
    Sorry I did not see you install also sudo before. it works that way – Kevin Apr 19 '17 at 20:34
0

Just create your non root user and add it to the sudoers group:

FROM ubuntu:17.04

RUN apt-get update
RUN apt-get install sudo

RUN adduser --disabled-password --gecos '' admin
RUN adduser admin sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

USER admin
0

Here's how I setup a non-root user with the base image of ubuntu:18.04:

RUN \
    groupadd -g 999 foo && useradd -u 999 -g foo -G sudo -m -s /bin/bash foo && \
    sed -i /etc/sudoers -re 's/^%sudo.*/%sudo ALL=(ALL:ALL) NOPASSWD: ALL/g' && \
    sed -i /etc/sudoers -re 's/^root.*/root ALL=(ALL:ALL) NOPASSWD: ALL/g' && \
    sed -i /etc/sudoers -re 's/^#includedir.*/## **Removed the include directive** ##"/g' && \
    echo "foo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers && \
    echo "Customized the sudoers file for passwordless access to the foo user!" && \
    echo "foo user:";  su - foo -c id

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.