Saturday, November 17, 2018

bash - Run sudo command with non-root user in Docker container



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?



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

No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...