run script when dockercontainer starts?
30,959
Replace the line
#!/usr/bin/with-contenv bash
with
#!/usr/bin/env bash
Ubuntu doesn't know
/usr/bin/with-contenv
.Use a
Dockerfile
like thisFROM ubuntu:16.04 […] COPY userconf.sh /usr/local/bin/userconf.sh COPY start.sh /usr/local/bin/start.sh […] CMD ["/bin/bash", "/usr/local/bin/start.sh"]
Your
start.sh
#!/usr/bin/env bash […] /usr/local/bin/userconf.sh […] exec your_script_to_start_a_foreground_process.sh
Related videos on Youtube
Author by
Ignacio
Updated on September 18, 2022Comments
-
Ignacio almost 2 years
If I create a dockerfile based on Debian I can add the following line to execute a script when I'm initiating the container:
COPY userconf.sh /etc/cont-init.d/userconf
What would be the equivalent with for a dockerfile based on Ubuntu 16.04? In case it is relevant, this is the script that I want to run.
I tried implementing the changes that @A.B. recommended but things are still not working. This is my dockerfile:
FROM r-gpu ARG RSTUDIO_VERSION ## Comment the next line to use the latest RStudio Server version by default #ENV RSTUDIO_VERSION=${RSTUDIO_VERSION:-1.1.447} ENV PATH=/usr/lib/rstudio-server/bin:$PATH ## Download and install RStudio server & dependencies ## Attempts to get detect latest version, otherwise falls back to version given in $VER ## Symlink pandoc, pandoc-citeproc so they are available system-wide RUN apt-get update \ && apt-get install -y --no-install-recommends \ git \ libedit2 \ psmisc \ python-setuptools \ sudo \ wget \ && RSTUDIO_LATEST=$(wget --no-check-certificate -qO- https://s3.amazonaws.com/rstudio-server/current.ver) \ && [ -z "$RSTUDIO_VERSION" ] && RSTUDIO_VERSION=$RSTUDIO_LATEST || true \ && wget -q http://download2.rstudio.org/rstudio-server-${RSTUDIO_VERSION}-amd64.deb \ && dpkg -i rstudio-server-${RSTUDIO_VERSION}-amd64.deb \ && rm rstudio-server-*-amd64.deb \ ## Symlink pandoc & standard pandoc templates for use system-wide && ln -s /usr/lib/rstudio-server/bin/pandoc/pandoc /usr/local/bin \ && ln -s /usr/lib/rstudio-server/bin/pandoc/pandoc-citeproc /usr/local/bin \ && git clone https://github.com/jgm/pandoc-templates \ && mkdir -p /opt/pandoc/templates \ && cp -r pandoc-templates*/* /opt/pandoc/templates && rm -rf pandoc-templates* \ && mkdir /root/.pandoc && ln -s /opt/pandoc/templates /root/.pandoc/templates \ && apt-get clean \ && rm -rf /var/lib/apt/lists/ \ && mkdir -p /usr/local/lib/R/etc \ ## RStudio wants an /etc/R, will populate from $R_HOME/etc && mkdir -p /etc/R \ ## Write config files in $R_HOME/etc && echo '\n\ \n# Configure httr to perform out-of-band authentication if HTTR_LOCALHOST \ \n# is not set since a redirect to localhost may not work depending upon \ \n# where this Docker container is running. \ \nif(is.na(Sys.getenv("HTTR_LOCALHOST", unset=NA))) { \ \n options(httr_oob_default = TRUE) \ \n}' >> /usr/local/lib/R/etc/Rprofile.site \ && echo "PATH=${PATH}" >> /usr/local/lib/R/etc/Renviron \ ## Prevent rstudio from deciding to use /usr/bin/R if a user apt-get installs a package && echo 'rsession-which-r=/usr/local/bin/R' >> /etc/rstudio/rserver.conf \ ## use more robust file locking to avoid errors when using shared volumes: && echo 'lock-type=advisory' >> /etc/rstudio/file-locks \ ## configure git not to request password each time && git config --system credential.helper 'cache --timeout=3600' \ && git config --system push.default simple \ ## Set up S6 init system && wget -P /tmp/ https://github.com/just-containers/s6-overlay/releases/download/v1.11.0.1/s6-overlay-amd64.tar.gz \ && tar xzf /tmp/s6-overlay-amd64.tar.gz -C / \ && mkdir -p /etc/services.d/rstudio \ && echo '#!/usr/bin/with-contenv bash \ \n exec /usr/lib/rstudio-server/bin/rserver --server-daemonize 0' \ > /etc/services.d/rstudio/run \ && echo '#!/bin/bash \ \n rstudio-server stop' \ > /etc/services.d/rstudio/finish COPY userconf.sh /usr/local/bin/userconf.sh ## running with "-e ADD=shiny" adds shiny server COPY add_shiny.sh /usr/local/bin/add_shiny.sh COPY pam-helper.sh /usr/local/bin/pam-helper.sh COPY start.sh /usr/local/bin/start.sh RUN ln /usr/bin/R /usr/local/bin/R EXPOSE 8787 CMD ["/bin/bash", "/usr/local/bin/start.sh"]
This is the
userconf.sh
:#!/usr/bin/env bash ## Set defaults for environmental variables in case they are undefined USER=${USER:=rstudio} PASSWORD=${PASSWORD:=rstudio} USERID=${USERID:=1000} GROUPID=${GROUPID:=1000} ROOT=${ROOT:=FALSE} UMASK=${UMASK:=022} if [ "$USERID" -lt 1000 ] # Probably a macOS user, https://github.com/rocker-org/rocker/issues/205 then echo "$USERID is less than 1000" check_user_id=$(grep -F "auth-minimum-user-id" /etc/rstudio/rserver.conf) if [[ ! -z $check_user_id ]] then echo "minumum authorised user already exists in /etc/rstudio/rserver.conf: $check_user_id" else echo "setting minumum authorised user to 499" echo auth-minimum-user-id=499 >> /etc/rstudio/rserver.conf fi fi if [ "$USERID" -ne 1000 ] ## Configure user with a different USERID if requested. then echo "deleting user rstudio" userdel rstudio echo "creating new $USER with UID $USERID" useradd -m $USER -u $USERID mkdir /home/$USER chown -R $USER /home/$USER usermod -a -G staff $USER elif [ "$USER" != "rstudio" ] then ## cannot move home folder when it's a shared volume, have to copy and change permissions instead cp -r /home/rstudio /home/$USER ## RENAME the user usermod -l $USER -d /home/$USER rstudio groupmod -n $USER rstudio usermod -a -G staff $USER chown -R $USER:$USER /home/$USER echo "USER is now $USER" fi if [ "$GROUPID" -ne 1000 ] ## Configure the primary GID (whether rstudio or $USER) with a different GROUPID if requested. then echo "Modifying primary group $(id $USER -g -n)" groupmod -g $GROUPID $(id $USER -g -n) echo "Primary group ID is now custom_group $GROUPID" fi ## Add a password to user echo "$USER:$PASSWORD" | chpasswd # Use Env flag to know if user should be added to sudoers if [ "$ROOT" == "TRUE" ] then adduser $USER sudo && echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers echo "$USER added to sudoers" fi ## Change Umask value if desired if [ "$UMASK" -ne 022 ] then echo "server-set-umask=false" >> /etc/rstudio/rserver.conf echo "Sys.umask(mode=$UMASK)" >> /home/$USER/.Rprofile fi ## add these to the global environment so they are avialable to the RStudio user echo "HTTR_LOCALHOST=$HTTR_LOCALHOST" >> /etc/R/Renviron.site echo "HTTR_PORT=$HTTR_PORT" >> /etc/R/Renviron.site
and this is the start.sh
#!/usr/bin/env bash /usr/local/bin/userconf.sh /usr/local/bin/add_shiny.sh /usr/local/bin/pam-helper.sh exec /usr/lib/rstudio-server/bin/rserver --server-daemonize=0 --server-app-armor-enabled=0
What am I missing?
-
Sergiy Kolodyazhnyy almost 6 yearsWelcome back, friend !
-
Ignacio almost 6 yearsThanks @A.B. I tried to implement your suggestions but things are not working. What am I missing?
-
A.B. almost 6 years@Ignacio please define "things"
-
Ignacio almost 6 yearsThe default password (rstudio) does not change even if i run
nvidia-docker run -d --restart=always -p 8787:8787 --name="rstudio-gpu" -e PASSWORD='MYSUPERSECRETPASSWORD!!' rstudio-gpu
.userconf.sh
should allow me to change the password. -
Ignacio almost 6 yearsMy bad. I just had to do ` chmod +x *.sh`