Dockerfile won't run script as user with sudo rights

I have this Docker file…

FROM ubuntu:14.04 MAINTAINER Sindhu S
# 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 /home/scrollback/ 
RUN chown scrollback:scrollback
RUN chmod +x
USER scrollback RUN ["/bin/bash", "-c", "/home/scrollback/"]

As you can see its supposed to run the 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.

  • Bash Command Templating For Docker
  • Docker file to call PS1 file with parameters
  • Empty directory when mounting volume using windows for docker
  • Erlang garbage collection
  • Nginx proxy, linked to other docker containers
  • Getting list of running docker containers using docker-py
  • 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/"]

    in the script there is a line:

    $(sudo echo $apt_source > /etc/apt/sources.list.d/pgdg.list)

    and I get…

    Adding Postgres' deb trusty-pgdg main source to your software sources list (need root user rights)..
    ./ line 136: /etc/apt/sources.list.d/pgdg.list: Permission denied

    How can I fix this?

  • Python3 in DockerFile running ubuntu::latest
  • How to Install Private Python Package as Part of Build
  • Sporadic Robolectric test failures when running inside Docker container
  • Share data between 2 containers
  • Which approach is better for discovering container readiness?
  • Is there a performance impact to having docker containers with different OS base boxes?
  • 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.