Dockerfile won't run script as user with sudo rights
I have this Docker file…
FROM ubuntu:14.04 MAINTAINER Sindhu S email@example.com # RUN apt-get update && apt-get -y install sudo wget bash git && rm -rf /var/cache/apt-get/archives/* RUN adduser --disabled-password --gecos "" scrollback RUN adduser scrollback sudo RUN echo 'scrollback ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers RUN mkdir -p /home/scrollback && chown -R scrollback:scrollback /home/scrollback WORKDIR /home/scrollback/ ADD https://raw.githubusercontent.com/sindhus/scrollback/install-script/tools/install.sh /home/scrollback/install.sh RUN chown scrollback:scrollback install.sh RUN chmod +x install.sh USER scrollback RUN ["/bin/bash", "-c", "/home/scrollback/install.sh"]
As you can see its supposed to run the install.sh as scrollback user (this user has sudo rights). Basically the script shouldn’t run as root but should run as user with sudo rights because there are some system files we need to modify.
I have tried with:
RUN ["/bin/bash", "-c", "sudo touch /etc/apt/sources.list.d/pgdg.list"]
and this works!
Yet when I have this line:
RUN ["/bin/bash", "-c", "/home/scrollback/install.sh"]
in the script there is a line:
$(sudo echo $apt_source > /etc/apt/sources.list.d/pgdg.list)
and I get…
Adding Postgres' deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main source to your software sources list (need root user rights)..
./install.sh: line 136: /etc/apt/sources.list.d/pgdg.list: Permission denied
How can I fix this?
One Solution collect form web for “Dockerfile won't run script as user with sudo rights”
It is not a good practice using
sudo inside a bash script. Instead use the command directly and run your script with
sudo. Thus, your script (and the programs launched inside) will be launched with root privileges.