Dockerfile won't run script as user with sudo rights

I have this Docker file…

FROM ubuntu:14.04 MAINTAINER Sindhu S sindhu@alum.hackerschool.org
# 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.

  • Why the directory created after WORKDIR disappear
  • SkyDNS does not work with Kubernetes 1.1.2
  • Docker container isolation, does it care about underlying Linux OS?
  • Docker: deploying a new version of an app that uses external resources
  • How do I execute commands on Karaf running inside Docker?
  • PHP pg_connect slow time with Docker
  • 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?

  • Running Docker pull command in Dockerfile
  • Docker daemon memory leak due to logs from long running process
  • Docker run failed with Error response from daemon
  • Can I use LXD image to create Docker container and/or vice versa?
  • Dockerfile: $HOME is not working with ADD/COPY instructions
  • Docker: container can't find domain on private network
  • 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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.