executar script quando o dockercontainer for iniciado?


2

Se eu criar um arquivo docker com base no Debian, posso adicionar a seguinte linha para executar um script quando estou iniciando o contêiner:

COPY userconf.sh /etc/cont-init.d/userconf

Qual seria o equivalente a um arquivo docker baseado no Ubuntu 16.04? Caso seja relevante, esse é o script que eu quero executar.


Tentei implementar as alterações recomendadas pela @AB, mas as coisas ainda não estão funcionando. Este é o meu 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"]

Este é o 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

e este é o 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

o que estou perdendo?

Respostas:


3
  1. Substitua a linha

    #!/usr/bin/with-contenv bash
    

    com

    #!/usr/bin/env bash
    

    Ubuntu não sabe /usr/bin/with-contenv.

  2. Use um Dockerfilecomo este

    FROM 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"]
    
  3. Seu start.sh

    #!/usr/bin/env bash
    
    […]
    
    /usr/local/bin/userconf.sh
    
    […]
    
    exec your_script_to_start_a_foreground_process.sh
    

Bem vindo de volta, amigo!
Sergiy Kolodyazhnyy

Obrigado @AB Tentei implementar suas sugestões, mas as coisas não estão funcionando. o que estou perdendo?
Ignacio

@Ignacio por favor defina "coisas"
AB

A senha padrão (rstudio) não muda, mesmo que eu execute nvidia-docker run -d --restart=always -p 8787:8787 --name="rstudio-gpu" -e PASSWORD='MYSUPERSECRETPASSWORD!!' rstudio-gpu. userconf.shdeve permitir que eu mude a senha.
Ignacio

Minha culpa. Eu só tinha que fazer `chmod + x * .sh` #
Ignacio
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.