Skip to content

Commit 0d33669

Browse files
authored
Merge pull request #12 from grst/podman
Podman version
2 parents c5723d8 + f9f236b commit 0d33669

File tree

7 files changed

+179
-46
lines changed

7 files changed

+179
-46
lines changed

README.md

Lines changed: 118 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,137 @@
1-
# Howto run Rstudio Server in a Conda Environment
1+
# Running Rstudio Server in a Conda Environment
22

3-
I usually rely on the [conda package manager]() to manage my environments during development. Thanks to [conda-forge](https://conda-forge.org/) and [bioconda](https://bioconda.github.io/) most R packages are now also available through conda.
3+
I usually rely on the [conda package manager](https://docs.conda.io/en/latest/) to manage my environments during development. Thanks to [conda-forge](https://conda-forge.org/) and [bioconda](https://bioconda.github.io/) most R packages are now also available through conda. For production,
4+
I [convert them to containers](https://github.com/grst/containerize-conda) as these are easier to share.
45

5-
Unfortunately, there seems to be [no straightforward way](https://community.rstudio.com/t/start-rstudio-server-session-in-conda-environment/12516/15) to use conda envs in Rstudio server. This is why I came up with the two scripts in this repo.
6+
Unfortunately, there seems to be [no straightforward way](https://community.rstudio.com/t/start-rstudio-server-session-in-conda-environment/12516/15) to use conda envs in Rstudio server. This repository provides two approaches to make rstudio server work with conda envs.
67

7-
## Installation and usage
8-
### 1. Prerequisites
9-
* installed [rstudio server](https://www.rstudio.com/products/rstudio/download-server/)
10-
* installed [conda](https://docs.conda.io/en/latest/miniconda.html)
8+
## Running Rstudio Server in a Container
119

12-
### 2. Disable rstudio server service.
13-
You might need to disable the system-wide Rstudio server service.
14-
Due to licensing restrictions of rstudio server community edition, only one rstudio process
15-
can run for each user simultaneously.
10+
With this approach Rstudio Server runs in a Docker container (based on [rocker/rstudio](https://hub.docker.com/r/rocker/rstudio)).
11+
The conda environment gets mounted into the container - like that there's no need to rebuild the container to add a package and
12+
`install.packages` can be used without issues. The container-based approach has the following benefits:
1613

17-
This is how it works on systemd-based systems:
14+
* Authentication works (#3)
15+
* Several separate instances of Rstudio server can run in parallel, even without the *Pro* version.
1816

19-
```bash
20-
sudo systemctl disable rstudio-server.service
21-
sudo systemctl stop rstudio-server.service
22-
```
17+
### Prerequisites
2318

24-
### 3. Clone this repo
25-
```
26-
git clone https://github.com/grst/rstudio-server-conda.git
27-
```
19+
* [Docker](https://www.docker.com/) or [Podman](https://podman.io/)
20+
* [docker-compose](https://github.com/docker/compose) or [podman-compose](https://github.com/containers/podman-compose)
21+
* [conda](https://docs.conda.io/en/latest/miniconda.html) or [mamba](https://github.com/conda-forge/miniforge#mambaforge)
2822

29-
### 4. Run rstudio server in the conda env
30-
```
31-
conda activate my_project
32-
./start_rstudio_server.sh 8787 # use any free port number here.
33-
```
23+
### Usage
3424

35-
You should now be able to connect to rstudio server on the port you specify. **If an R Session has previously been running, you'll need to rstart the Rsession now**.
25+
1. Clone this repository
3626

37-
Obviously, if your env does not have a version of `R` installed, this will either not
38-
work at all, or fall back to the system-wide R installation.
27+
```bash
28+
git clone git@github.com:grst/rstudio-server-conda.git
29+
```
3930

31+
2. Build the rstudio container (fetches the latest version of [rocker/rstudio](https://hub.docker.com/r/rocker/rstudio) and adds some custom scripts)
4032

33+
```bash
34+
cd rstudio-server-conda/docker
35+
docker-compose build # or podman-compose
36+
```
4137

42-
## How it works
38+
3. Copy the docker-compose.yml file into your project directory and adjust the paths.
39+
40+
You may want to add additional volumes with your data.
41+
42+
```yml
43+
[...]
44+
ports:
45+
# port on the host : port in the container (the latter is always 8787)
46+
- "8889:8787"
47+
volumes:
48+
# mount conda env into exactely the same path as on the host system - some paths are hardcoded in the env.
49+
- /home/sturm/anaconda3/envs/R400:/home/sturm/anaconda3/envs/R400
50+
# Share settings between rstudio instances
51+
- /home/sturm/.local/share/rstudio/monitored/user-settings:/root/.local/share/rstudio/monitored/user-settings
52+
# mount the working directory containing your R project.
53+
- /home/sturm/projects:/projects
54+
environment:
55+
# password used for authentication
56+
- PASSWORD=notsafe
57+
# repeat the path of the conda environment (must be identical to the path in "volumes")
58+
- CONDAENV=/home/sturm/anaconda3/envs/R400
59+
```
60+
61+
4. Run your project-specific instance of Rstudio-server
62+
63+
```bash
64+
docker-compose up
65+
```
66+
67+
5. Log into Rstudio
68+
69+
* Open your server at `https://localhost:8889` (or whatever port you specified)
70+
* Login with the user `rstudio` (when using Docker) or `root` (when using Podman) and the password you specified
71+
in the `docker-compose.yml`. If you are using Podman and login with `rstudio` you won't have permissions to
72+
access the mounted volumes.
73+
74+
75+
## Running Locally
76+
77+
With this approach a locally installed Rstudio server is ran such that it uses the conda env.
78+
A known limitation of this approch is that the Rstudio authentication is bypassed (see #3).
79+
Therefore, only use this approach in a secure network!
80+
81+
### Prerequisites
82+
* [rstudio server](https://www.rstudio.com/products/rstudio/download-server/) installed locally
83+
* [conda](https://docs.conda.io/en/latest/miniconda.html) or [mamba](https://github.com/conda-forge/miniforge#mambaforge)
84+
85+
### Usage
86+
87+
1. Clone this repo
88+
89+
```
90+
git clone https://github.com/grst/rstudio-server-conda.git
91+
```
92+
93+
2. Run rstudio server in the conda env
94+
95+
```
96+
cd rstudio-server-conda/local
97+
conda activate my_project
98+
./start_rstudio_server.sh 8787 # use any free port number here.
99+
```
100+
101+
3. Connect to Rstudio
102+
103+
You should now be able to connect to rstudio server on the port you specify.
104+
**If an R Session has previously been running, you'll need to rstart the Rsession now**.
105+
106+
Obviously, if your env does not have a version of `R` installed, this will either not
107+
work at all, or fall back to the system-wide R installation.
108+
109+
110+
### How it works
43111
* Rstudio server, can be started in non-daemonized mode by each user individually on a custom port (similar to a jupyter notebook). This instance can then run in a conda environment:
44-
```
45-
> conda activate my_project
46-
> /usr/lib/rstudio-server/bin/rserver \
47-
--server-daemonize=0 \
48-
--www-port 8787 \
49-
--rsession-which-r=$(which R) \
50-
--rsession-ld-library-path=$CONDA_PREFIX/lib
51-
```
112+
113+
```
114+
> conda activate my_project
115+
> /usr/lib/rstudio-server/bin/rserver \
116+
--server-daemonize=0 \
117+
--www-port 8787 \
118+
--rsession-which-r=$(which R) \
119+
--rsession-ld-library-path=$CONDA_PREFIX/lib
120+
```
121+
52122
* To avoid additional problems with library paths, also `rsession` needs to run within the conda environment. This is achieved by wrapping `rsession` into the [rsession.sh](https://github.com/grst/rstudio-server-conda/blob/master/rsession.sh) script. The path to the wrapped `rsession` executable can be passed to `rserver` as command line argument.
53-
```
54-
rserver # ...
55-
--rsession-path=rsession.sh
56-
```
123+
124+
```
125+
rserver # ...
126+
--rsession-path=rsession.sh
127+
```
57128

58129

59130
* When using multiple users a unique `secret-cookie-key` has to be generated for each user. The path to the secret cookie key can be passed to `rserver` as a command line parameter.
60-
```
61-
uuid > /tmp/rstudio-server/${USER}_secure-cookie-key
62-
rserver # ...
63-
--secure-cookie-key-file /tmp/rstudio-server/${USER}_secure-cookie-key
64-
```
131+
132+
```
133+
uuid > /tmp/rstudio-server/${USER}_secure-cookie-key
134+
rserver # ...
135+
--secure-cookie-key-file /tmp/rstudio-server/${USER}_secure-cookie-key
136+
```
65137

docker/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM rocker/rstudio
2+
3+
ENV ROOT=TRUE
4+
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
5+
6+
ADD init2.sh /init2.sh
7+
8+
CMD ["/init2.sh"]

docker/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Rstudio server conda (podman version)
2+
3+
## Usage:
4+
5+
* copy the docker-compose.yml file into your project directory
6+
* update the conda env paths and port in docker-compose.yml
7+
* `podman-compose up`
8+
9+
## Note:
10+
* when using docker, login with the `rstudio` user as described on the "rocker"
11+
page.
12+
* when using podman, login with the `root` user, as root within the container
13+
corresponds to your user outside the container.

docker/docker-compose.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: "3.9"
2+
services:
3+
rstudio:
4+
build: .
5+
image: grst/rstudio-server-conda:latest
6+
ulimits:
7+
nofile: 10000
8+
ports:
9+
- "8889:8787"
10+
volumes:
11+
# mount conda env into exactely the same path as on the host system - some paths are hardcoded in the env.
12+
- /home/sturm/anaconda3/envs/R400:/home/sturm/anaconda3/envs/R400
13+
# Share settings between rstudio instances
14+
- /home/sturm/.local/share/rstudio/monitored/user-settings:/root/.local/share/rstudio/monitored/user-settings
15+
# mount the working directory containing your R project.
16+
- /home/sturm/projects:/root/projects
17+
environment:
18+
- PASSWORD=notsafe
19+
# repeat the path of the conda environment
20+
- CONDAENV=/home/sturm/anaconda3/envs/R400
21+

docker/init2.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
# Settings required for conda+rstudio
4+
export RSTUDIO_WHICH_R=${CONDAENV}/bin/R
5+
export RETICULATE_PYTHON=${CONDAENV}/bin/python
6+
echo rsession-which-r=${RSTUDIO_WHICH_R} > /etc/rstudio/rserver.conf
7+
echo rsession-ld-library-path=${CONDAENV}/lib >> /etc/rstudio/rserver.conf
8+
echo "R_LIBS_USER=${CONDAENV}/lib/R/library" > /home/rstudio/.Renviron
9+
# Set root password (with podman, we need to login as root rather than "rstudio")
10+
echo "root:$PASSWORD" | chpasswd
11+
echo "auth-minimum-user-id=0" >> /etc/rstudio/rserver.conf
12+
13+
# Custom settings
14+
echo "session-timeout-minutes=0" > /etc/rstudio/rsession.conf
15+
echo "auth-timeout-minutes=0" >> /etc/rstudio/rserver.conf
16+
echo "auth-stay-signed-in-days=30" >> /etc/rstudio/rserver.conf
17+
18+
# Run original rocker launcher script
19+
/init
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)