Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: how to use rstudio with r2u? #1

Closed
MatthieuStigler opened this issue May 8, 2023 · 20 comments
Closed

Question: how to use rstudio with r2u? #1

MatthieuStigler opened this issue May 8, 2023 · 20 comments

Comments

@MatthieuStigler
Copy link

Hi Dirk

I would like to use rstudio with the r2u docker and was wondering if you could share some advice on how to do this (sorry, I just stared exploring the docker functionalities). Basically, it seems like I am interested in combining rocker/rstudio with rocker/r2u:focal.

How would you advise to go? Should I go for one image and seek to install the other component interactively, or rather seek to merge both functionalities in a custom image (though calling RUN /rocker_scripts/install_rstudio.sh seems difficult for a beginner)?

Thanks!

@eitsupi

This comment was marked as off-topic.

@eddelbuettel
Copy link
Member

Docker works in layers. If you start from rocker/rstudio you inherit all its layers, and this includes what it does to 'versioned and dated' installations. Which is fundamentally at a conflict with using r2u.

So you have to turn the layers upside down: I would start from rocker/r2u and add the .deb package for rstudio. I even provide a quasi-PPA with scripts of how to fetch it (which cron calls twice a week for me).

Let us know how that goes. I will close this as there is no deficiency in r2u itself we need to address. You simply have a desire for a different use case that can be addressed.

@eddelbuettel

This comment was marked as off-topic.

@eitsupi

This comment was marked as off-topic.

@eddelbuettel
Copy link
Member

eddelbuettel commented May 8, 2023

As I recall, at the very r2u beginning it even failed in a running rstudio but IIRC there was a simple, basic change Inaki made so it works from inside RStudio too. We used it with great success in the server backing my class too.

But fundamentally there are limits: r2u uses and adjust the main mechanics R uses: install.packages(). Anything that departs from that and uses its own ways instead of base R's will have to do so without the r2u benefits. It's all choices we make. What we have here works for me and many others, other people want different things and get them elsewhere.

@MatthieuStigler
Copy link
Author

Thanks @eddelbuettel and @eitsupi for your fast answer!

I see about not mixing r2u and rocker/rstudio. Now this seems to mean I also need to add many lines to install & configure rstudio, which seems a bit to reinvent the wheel?

Anyway, here is what I did to the Dockerfile:

RUN wget --no-verbose https://download2.rstudio.org/server/bionic/amd64/rstudio-server-2023.03.0-386-amd64.deb \
        && apt-get install -y --no-install-recommends ./rstudio-server-2023.03.0-386-amd64.deb

Having started the docker, I then need to do:

rstudio-server start
useradd abc
passwd abc 
mkdir /home/abc
chown -R abc /home/abc

I get then the message:

Warning message:
D-Bus service not found!
If you are in a container environment, please consider adding the
following to your configuration to silence this warning:
options(bspm.sudo = TRUE)

Where would you advise to do set options(bspm.sudo = TRUE)? It seems just doing it interactively in the Rstudio session does not work well?

Thanks!

@eddelbuettel
Copy link
Member

Maybe look at the existing Dockerfile(s) for inspiration?

&& echo "suppressMessages(bspm::enable())" >> /etc/R/Rprofile.site \

Maybe there are interactions with rstudio on uid=1000 vs root. I haven't tried this myself.

@MatthieuStigler
Copy link
Author

Thanks Dirk!

I forgot that /etc/R/Rprofile.site won't necessarily be read by Rstudio Server, one should rather edit a rstudio specific file.

Anyway, this does the trick, I was able to use rstudio and read/link a folder from my host computer into the container. I also realized that I didn't need to create another user as long as one knows/assigns the password of user docker.

Thanks for your help on that!

@eddelbuettel
Copy link
Member

If you can / want to document this for another user it may be helpful for someone looking for this.

Also, your sentence about

/etc/R/Rprofile.site won't necessarily be read by Rstudio Server

is unclear to me. On this machine that I am typing I have both RStudio Desktop and RStudio server and when I query getOption("repos") I get the proper setting from my /etc/R/Rprofile.site on both. If something is different in a container setting by choice of those container settings you may have to address it in those very container settings.

Every R sessions read Rprofile, Rprofile.site (if it exists), ~/.Rprofile (ditto) and so on. See help(Startup).

@MatthieuStigler
Copy link
Author

Actually, I wasn't correct stating this: the /etc/R/Rprofile.site site is read, option options()$bspm.sudo is TRUE, yet there is the warning message:

D-Bus service not found!
If you are in a container environment, please consider adding th e following to your configuration to silence this warning: options(bspm.sudo = TRUE)

So I am not so sure how one would handle that?

@eddelbuettel
Copy link
Member

Right. Can you carry it over to the bspm repo and ask Inaki there?

@MatthieuStigler
Copy link
Author

And happy to share my Dockerfile, although I feel it is really basic and probably makes several mistakes, and the install_rstudio.sh contains probably much better code. Problems that I am aware of with my scripts are:

  • just writing RUN rstudio-server start doesn't work, so I ran it interactively within the console
  • the line RUN echo "docker:pass" | chpasswd is probably a bad idea but that's the trick I found to login to rstudio with docker/pass

Here are the lines added on top of the Dockerfile in this repo:

##
RUN wget --no-verbose https://download2.rstudio.org/server/bionic/amd64/rstudio-server-2023.03.0-386-amd64.deb \
        && apt-get install -y --no-install-recommends ./rstudio-server-2023.03.0-386-amd64.deb 
RUN echo "options(bspm.sudo = TRUE)" >> /etc/R/Rprofile.site
RUN echo "docker:pass" | chpasswd
RUN rstudio-server start

ENV DEFAULT_USER=docker
        
EXPOSE 8787

@eddelbuettel
Copy link
Member

Hm, so maybe the answer is to ... not do options(bspm.sudo=TRUE) ? I think I learned (slowly, eventually) from Inaki that I don't need it / should not have it.

Mostly because I am root in my containers.

I still have that also in my default CI setup (via r-ci so I still have something to learn here too. So thanks for asking over there.

@MatthieuStigler
Copy link
Author

Inaki quickly helped me realize that I just had to put options(bspm.sudo = TRUE) before bspm::enable() in /etc/R/Rprofile.site , that did the trick!

Thanks!

Adding below the corrected Dockerfile, steps to run it are:

  1. docker build Dockerfile_rocker_r2u_mat --tag myrock/r2u_v2
  2. docker run --rm -ti -p 8787:8787 myrock/r2u_v2
  3. Run rstudio-server start from within docker
  4. Open http://localhost:8787/, connect with docker/pass

Dockerfile:

## Emacs, make this -*- mode: sh; -*-

FROM ubuntu:focal

LABEL org.label-schema.license="GPL-2.0" \
      org.label-schema.vcs-url="https://github.com/rocker-org/" \
      org.label-schema.vendor="Rocker Project" \
      maintainer="Dirk Eddelbuettel <edd@debian.org>"

## Set a default user. Available via runtime flag `--user docker` 
## Add user to 'staff' group, granting them write privileges to /usr/local/lib/R/site.library
## User should also have & own a home directory (for rstudio or linked volumes to work properly). 
RUN useradd -s /bin/bash -m docker \
	&& usermod -a -G staff docker \
## Refresh apt, install minimal tools
        && apt-get update \
	&& apt-get install -y --no-install-recommends \
		ca-certificates \
		locales \
		wget \
## Install key and setup R repo at CRAN 
        && wget -q -O - https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc \
                | tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc  \
        && echo "deb [arch=amd64 signed-by=/etc/apt/trusted.gpg.d/cran_ubuntu_key.asc] https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/" \
                > /etc/apt/sources.list.d/cran.list \
## Install key and setup r2u repo, also set 'pin preference' 
        && wget -q -O - https://r2u.stat.illinois.edu/ubuntu/dirk_eddelbuettel_pubkey.asc \
                | tee -a /etc/apt/trusted.gpg.d/dirk_eddelbuettel_pubkey.asc \
        && echo "deb [arch=amd64 signed-by=/etc/apt/trusted.gpg.d/dirk_eddelbuettel_pubkey.asc] https://r2u.stat.illinois.edu/ubuntu focal main" \
                > /etc/apt/sources.list.d/r2u.list \
        && echo "Package: *" > /etc/apt/preferences.d/99r2u \
        && echo "Pin: release o=CRAN-Apt Project" >> /etc/apt/preferences.d/99r2u \
        && echo "Pin: release l=CRAN-Apt Packages" >> /etc/apt/preferences.d/99r2u \
        && echo "Pin-Priority: 700"  >> /etc/apt/preferences.d/99r2u \
## Configure default locale, see https://github.com/rocker-org/rocker/issues/19
        && echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen \
	&& locale-gen en_US.utf8 \
	&& /usr/sbin/update-locale LANG=en_US.UTF-8

## Set some variables
ENV LC_ALL en_US.UTF-8
ENV LANG en_US.UTF-8
ENV DEBIAN_FRONTEND noninteractive
ENV TZ UTC

## Now install R and littler, and create a link for littler in /usr/local/bin
## Default CRAN repo is now set by R itself, and littler knows about it too
RUN apt-get update \
        && apt-get install -y --no-install-recommends \
 		 r-base \
 		 r-base-dev \
 		 r-recommended \
## Install bspm for r2u as well as remotes and docopt used in littler script
                 r-cran-bspm \
                 r-cran-docopt \
                 r-cran-littler \
                 r-cran-remotes \
## Install python support for bspm                 
                 python3-dbus \
                 python3-gi \
                 python3-apt \
## Support user-level installation of R packages                 
	&& chown root:staff "/usr/local/lib/R/site-library" \
	&& chmod g+ws "/usr/local/lib/R/site-library" \
## Install a number littler scripts
  	&& ln -s /usr/lib/R/site-library/littler/examples/install.r /usr/local/bin/install.r \
 	&& ln -s /usr/lib/R/site-library/littler/examples/install2.r /usr/local/bin/install2.r \
	&& ln -s /usr/lib/R/site-library/littler/examples/installBioc.r /usr/local/bin/installBioc.r \
 	&& ln -s /usr/lib/R/site-library/littler/examples/installDeps.r /usr/local/bin/installDeps.r \
 	&& ln -s /usr/lib/R/site-library/littler/examples/installGithub.r /usr/local/bin/installGithub.r \
 	&& ln -s /usr/lib/R/site-library/littler/examples/testInstalled.r /usr/local/bin/testInstalled.r \
  	&& ln -s /usr/lib/R/site-library/littler/examples/update.r /usr/local/bin/update.r \
## Configure bspm and set one helpful apt default
        && echo "options(bspm.sudo = TRUE)" >> /etc/R/Rprofile.site \
        && echo "suppressMessages(bspm::enable())" >> /etc/R/Rprofile.site \
        && echo "options(bspm.version.check=FALSE)" >> /etc/R/Rprofile.site \
        && echo 'APT::Install-Recommends "false";' > /etc/apt/apt.conf.d/90local-no-recommends \
	&& rm -rf /tmp/downloaded_packages/ /tmp/*.rds \
 	&& rm -rf /var/lib/apt/lists/*
 	

## Now install Rstudio
RUN wget --no-verbose https://download2.rstudio.org/server/bionic/amd64/rstudio-server-2023.03.0-386-amd64.deb \
        && apt-get update \
        && apt-get install -y --no-install-recommends ./rstudio-server-2023.03.0-386-amd64.deb

# I wasn't sure what the password is, so setting one as we are logging as root by default on this docker
RUN echo "docker:pass" | chpasswd
RUN rstudio-server start # does'nt seem to work...

ENV DEFAULT_USER=docker
        
EXPOSE 8787

@GMFranceschini
Copy link

Hi, I am following up on a post I did a few days ago. I had the same aim to add RStudio to r2u. Premise: for the moment, I found a simple solution to my needs (namely, getting the container ready with the libraries required for most R packages). However, I gave it a shot nonetheless.
Compared to OP, I took a different route, probably worse: I took the install scripts and Dockerfile from rocker/rstudio and applied them starting from r2u. The file looks like this:

## Emacs, make this -*- mode: sh; -*-

FROM ubuntu:jammy

LABEL org.label-schema.license="GPL-2.0" \
      org.label-schema.vcs-url="https://github.com/rocker-org/" \
      org.label-schema.vendor="Rocker Project" \
      maintainer="Dirk Eddelbuettel <edd@debian.org>"

## Set a default user. Available via runtime flag `--user docker` 
## Add user to 'staff' group, granting them write privileges to /usr/local/lib/R/site.library
## User should also have & own a home directory (for rstudio or linked volumes to work properly). 
RUN useradd -s /bin/bash -m docker \
	&& usermod -a -G staff docker \
## Refresh apt, install minimal tools
        && apt-get update \
	&& apt-get install -y --no-install-recommends \
		ca-certificates \
		locales \
		wget \
## Install key and setup R repo at CRAN
        && wget -q -O - https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc \
                | tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc  \
        && echo "deb [arch=amd64 signed-by=/etc/apt/trusted.gpg.d/cran_ubuntu_key.asc] https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/" \
                > /etc/apt/sources.list.d/cran.list \
## Install key and setup r2u repo, also set 'pin preference'
        && wget -q -O - https://r2u.stat.illinois.edu/ubuntu/dirk_eddelbuettel_pubkey.asc \
                | tee -a /etc/apt/trusted.gpg.d/dirk_eddelbuettel_pubkey.asc \
        && echo "deb [arch=amd64 signed-by=/etc/apt/trusted.gpg.d/dirk_eddelbuettel_pubkey.asc] https://r2u.stat.illinois.edu/ubuntu jammy main" \
                > /etc/apt/sources.list.d/r2u.list \
        && echo "Package: *" > /etc/apt/preferences.d/99r2u \
        && echo "Pin: release o=CRAN-Apt Project" >> /etc/apt/preferences.d/99r2u \
        && echo "Pin: release l=CRAN-Apt Packages" >> /etc/apt/preferences.d/99r2u \
        && echo "Pin-Priority: 700"  >> /etc/apt/preferences.d/99r2u \
## Configure default locale, see https://github.com/rocker-org/rocker/issues/19
        && echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen \
	&& locale-gen en_US.utf8 \
	&& /usr/sbin/update-locale LANG=en_US.UTF-8

## Set some variables
ENV LC_ALL en_US.UTF-8
ENV LANG en_US.UTF-8
ENV DEBIAN_FRONTEND noninteractive
ENV TZ UTC
## RStudio install
ENV S6_VERSION=v2.1.0.2
ENV RSTUDIO_VERSION=2023.09.1+494
ENV DEFAULT_USER=rstudio
ENV PANDOC_VERSION=default
ENV QUARTO_VERSION=default


# Now install R and littler, and create a link for littler in /usr/local/bin
# Default CRAN repo is now set by R itself, and littler knows about it too
RUN apt-get update \
        && apt-get install -y --no-install-recommends \
 		 r-base \
 		 r-base-dev \
 		 r-recommended \
## Install bspm for r2u as well as remotes and docopt used in littler script
                 r-cran-bspm \
                 r-cran-docopt \
                 r-cran-littler \
                 r-cran-remotes \
## Install python support for bspm
                 python3-dbus \
                 python3-gi \
                 python3-apt \
## Support user-level installation of R packages
	&& chown root:staff "/usr/local/lib/R/site-library" \
	&& chmod g+ws "/usr/local/lib/R/site-library" \
## Install a number littler scripts
  	&& ln -s /usr/lib/R/site-library/littler/examples/build.r /usr/local/bin/build.r \
  	&& ln -s /usr/lib/R/site-library/littler/examples/check.r /usr/local/bin/check.r \
  	&& ln -s /usr/lib/R/site-library/littler/examples/install.r /usr/local/bin/install.r \
 	&& ln -s /usr/lib/R/site-library/littler/examples/install2.r /usr/local/bin/install2.r \
	&& ln -s /usr/lib/R/site-library/littler/examples/installBioc.r /usr/local/bin/installBioc.r \
 	&& ln -s /usr/lib/R/site-library/littler/examples/installDeps.r /usr/local/bin/installDeps.r \
 	&& ln -s /usr/lib/R/site-library/littler/examples/installGithub.r /usr/local/bin/installGithub.r \
 	&& ln -s /usr/lib/R/site-library/littler/examples/installRub.r /usr/local/bin/installRub.r \
 	&& ln -s /usr/lib/R/site-library/littler/examples/testInstalled.r /usr/local/bin/testInstalled.r \
  	&& ln -s /usr/lib/R/site-library/littler/examples/update.r /usr/local/bin/update.r \
## Configure bspm and set one helpful apt default
        && echo "options(bspm.version.check=FALSE)" >> /etc/R/Rprofile.site \
        && echo "suppressMessages(bspm::enable())" >> /etc/R/Rprofile.site \
        && echo 'APT::Install-Recommends "false";' > /etc/apt/apt.conf.d/90local-no-recommends \
	&& rm -rf /tmp/downloaded_packages/ /tmp/*.rds \
 	&& rm -rf /var/lib/apt/lists/*

COPY . .

## Add RSTUDIO and related packages
RUN ./scripts/install_rstudio.sh
RUN ./scripts/install_pandoc.sh
RUN ./scripts/install_quarto.sh

EXPOSE 8787

CMD ["/init"]

This indeed works and gets me an RStudio instance running. However, I cannot install any package, as I get an error referring to policikit or a cryptic An error occurred: string. I guess I messed up somehow the R installation.

@eitsupi
Copy link
Member

eitsupi commented Nov 19, 2023

Note that the install script of this rstudio-server Dev Container Feature should be able to install RStudio Server on rocker/r2u (although I have hardly tried it)
https://github.com/rocker-org/devcontainer-features/tree/main/src/rstudio-server

@eddelbuettel
Copy link
Member

eddelbuettel commented Nov 19, 2023

Thanks for following up in the existing issue! That's how it should be.

First feedback: Why do you not use FROM rocker/r2u:jammy ? Docker excels at layering, and I think you may be better off taking advantage of the existing layer. Your builds will also be simpler and faster that way.

@eddelbuettel
Copy link
Member

And @eitsupi is fully correct: We probably want to take advantage of the existing infrastructure, and lessons learned (!!) about which ports to open where, adjust the s6 process if needed etc. So please try you added command on the simpler r2u container and then if possible show us a) the command(s) you run and b) the errors you saw. It if quite likely that one or both of us have seen the same errors before and can help.

@GMFranceschini
Copy link

Thank you, and I am sorry for my ingenuity. Indeed, what you suggested seems to work; my Dockerfile now is:

## Emacs, make this -*- mode: sh; -*-

FROM rocker/r2u:jammy

COPY . .

EXPOSE 8787

Next, I opened the folder in VSCode, manually created the .devcontainer folder, and added this devcontainer.json:

{
    "image": "mcr.microsoft.com/devcontainers/base:ubuntu",
    "features": {
        "ghcr.io/rocker-org/devcontainer-features/rstudio-server": {}
    },
    "postAttachCommand": {
        "rstudio-start": "rserver"
    },
    "forwardPorts": [
        8787
    ],
    "portsAttributes": {
        "8787": {
            "label": "RStudio IDE"
        }
    }
}

Do you think this makes sense? I have never used this before, and from my understanding, this is a VScode feature, right? After that, I open the folder as a container, and RStudio is executed.

I can listen to 8787 and install packages successfully. However, it is taking minutes to install tidyverse, and I see g++ compilation messages so I might have misstepped somewhere.

@eddelbuettel
Copy link
Member

eddelbuettel commented Nov 19, 2023

I would suggest we disentangle this. You started this conversation about adding rstudio to an r2u container. Let's solve that.

What happens in a devcontainer is a completely separate issue that we should maybe start to look into once we established that we have a working rstudio container. Also, if I may, can I ask to post complete and verifiable snippets. Puting ... in the middle of a few lines is not helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants