Launching RStudio Inside A Container

In this section, we will see how to launch an RStudio server inside a container using the Rocker RStudio image.

Let’s start by pulling the rocker/rstudio image. The image has multiple R version and it supports both amd64 and arm64 architecture. We will pull version 4.4 (e.g., R version 4.4.0):

docker pull rocker/rstudio:4.4

4.4: Pulling from rocker/rstudio
d5a2ad729c09: Pull complete
52afab1e5214: Pull complete
5470254b6bce: Pull complete
31448a39ca0b: Pull complete
d9b801de7b4a: Pull complete
01e5afb0e5ba: Pull complete
c12b8fa2b210: Pull complete
f736bdfdf98f: Pull complete
d43202eea89b: Pull complete
f75471090da0: Pull complete
613fe7476aa4: Pull complete
6131f5b93b09: Pull complete
7e2b0e33d0a7: Pull complete
9ebea9c66865: Pull complete
575f3bda6605: Pull complete
7965aff29d23: Pull complete
a1699199f205: Pull complete
4fa5ea4f10eb: Pull complete
Digest: sha256:b0aab99fef930d2e3ac49eba807bf162bd3887a20f63a99a8e9ce3c8daa82aae
Status: Downloaded newer image for rocker/rstudio:4.4
docker.io/rocker/rstudio:4.4

Let’s inspect the image using the docker inspect command:

docker inspect rocker/rstudio:4.4

[
    {
        "Id": "sha256:becb6182a46d22f7088c703dab581948b0748c7a78667b0bac70be37187f8c66",
        "RepoTags": [
            "rocker/rstudio:4.4"
        ],
        "RepoDigests": [
            "rocker/rstudio@sha256:b0aab99fef930d2e3ac49eba807bf162bd3887a20f63a99a8e9ce3c8daa82aae"
        ],
        "Parent": "",
        "Comment": "buildkit.dockerfile.v0",
        "Created": "2024-05-13T10:18:35.446606479Z",
        "Container": "",
        "ContainerConfig": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": null,
            "Cmd": null,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "DockerVersion": "",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "8787/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "R_VERSION=4.4.0",
                "R_HOME=/usr/local/lib/R",
                "TZ=Etc/UTC",
                "CRAN=https://p3m.dev/cran/__linux__/jammy/latest",
                "LANG=en_US.UTF-8",
                "S6_VERSION=v2.1.0.2",
                "RSTUDIO_VERSION=2024.04.1+748",
                "DEFAULT_USER=rstudio"
            ],
            "Cmd": [
                "/init"
            ],
            "ArgsEscaped": true,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.opencontainers.image.authors": "Carl Boettiger <cboettig@ropensci.org>",
                "org.opencontainers.image.base.name": "docker.io/library/ubuntu:jammy",
                "org.opencontainers.image.description": "RStudio Server with fixed version of R",
                "org.opencontainers.image.licenses": "GPL-2.0-or-later",
                "org.opencontainers.image.ref.name": "ubuntu",
                "org.opencontainers.image.revision": "dfa8e3d174f6bb7aaa1f9a8a7b471bdd6ef50366",
                "org.opencontainers.image.source": "https://github.com/rocker-org/rocker-versioned2",
                "org.opencontainers.image.title": "rocker/rstudio",
                "org.opencontainers.image.vendor": "Rocker Project",
                "org.opencontainers.image.version": "R-4.4.0"
            }
        },
        "Architecture": "arm64",
        "Os": "linux",
        "Size": 2172808764,
        "VirtualSize": 2172808764,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/3729e6d9a3561c260fe454f4838f955d01f813b5eba2cae18e33da2671ca08e8/diff:/var/lib/docker/overlay2/d796c8cd0d3515c1b4f5973d3fc6595efd4841e31cdd4b9de01ab1ea5e952dc9/diff:/var/lib/docker/overlay2/fb601679c363675abb5a4db1dda3dd7d2db5bbf647812f19cb15527e82ddc7a1/diff:/var/lib/docker/overlay2/fba04399d1d6602d370cb888711a86cd673c8f5ab5bf591ec3b1ba20a5f6900d/diff:/var/lib/docker/overlay2/d91fb5888c1bcdf14e4c13980003a971694d96b0c8a4fdffc82865d83f1ca75e/diff:/var/lib/docker/overlay2/a058ada97238094f1d2ccdb362092fadd2f3d565040f1f19e786b92151c7fc34/diff:/var/lib/docker/overlay2/dbc40b2675396cf40a90f2423e561b2f009425c8b14309306a71b8e2366619e2/diff:/var/lib/docker/overlay2/96af8f52d1bacfa8dbff29e00069b2de8f97152de30efaa8c8ff6a73c9574f38/diff:/var/lib/docker/overlay2/90d65f022c6dac58c2e92da93a1842ccc54b7ae86483dc1eac84757ba9756af5/diff:/var/lib/docker/overlay2/2d170fc585e0a11b641479d5bb30c22d8fc06dc985371afa49d426c4ed1ae51e/diff:/var/lib/docker/overlay2/ea5019d71c56a137499ddd6e42cdb7f799ecf31b4559f802b947a51ca7168709/diff:/var/lib/docker/overlay2/282837f9b3df6c4f7f73f7ae593fd6248c212d551892a018c3ff4ec5e43da753/diff:/var/lib/docker/overlay2/1473d9373d0fb6c7ea4261324931190b4d7456e2a4382f30e874c930f7ada22b/diff:/var/lib/docker/overlay2/db0570895f19f40b48af680438e29315b8ca3db510e9bbc36a01076c5d88a189/diff:/var/lib/docker/overlay2/8b192d70a5882f9bf134f52edc4492959f24ca372222481f147db9f5cde4258d/diff:/var/lib/docker/overlay2/020b81a886e5b4f955ba735c1ceec2e64fb2adb6e8094c99e7b2091607187988/diff:/var/lib/docker/overlay2/e67158f4615da29fa5aa82d49fec7f20b9cca47faf79f1171271a89aaa0ad52b/diff",
                "MergedDir": "/var/lib/docker/overlay2/798e3379deda1c96016acffb4f157d346b903bae02c2d9a62c731aa4ab2f5d85/merged",
                "UpperDir": "/var/lib/docker/overlay2/798e3379deda1c96016acffb4f157d346b903bae02c2d9a62c731aa4ab2f5d85/diff",
                "WorkDir": "/var/lib/docker/overlay2/798e3379deda1c96016acffb4f157d346b903bae02c2d9a62c731aa4ab2f5d85/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:842bef16b8fb80459504b4621523ac3d78faf7e01d6d9fa7076ed6d4a8f49830",
                "sha256:2c68b352337ae04a9f7fce80007c19e3f2c874eadffc85199955cfcf44d9061c",
                "sha256:0fad67fdf921f7ed4f040284f6146e8375715129cd4d15bb36b9235242360b10",
                "sha256:2ef96c4a0e68a2c895ff11cf39f69113ceb277044912459a5e0cc24e7d21b790",
                "sha256:f8b9f8602a972d98720bd99f4f51c8379b6b845516214956d7d4d453dc39e683",
                "sha256:2fc954a61b038a031d1d0c5162274d55136b031c5ba8df8b1d6c4ab7ae20bccf",
                "sha256:6ca11d07fecd4bfe9b164c2314d828a3c9fda533735310a3085d22feed7ed9a6",
                "sha256:5b805848d8250ae9142acdec752e1e52ea94770dd36f3a9b54be0535d0a76cb0",
                "sha256:396ebfd3b2cc4e126316d78ed6370bca9a665c075d213926b8c5a3cd4d05cf87",
                "sha256:0ebe277907d2119217f2716a80f22584ce0263337b565159144840ef8b4db697",
                "sha256:0b4b2142410dd85ad91bc0a3001d70ce6a4554b4b50b9cb8becba2d50a7f2e81",
                "sha256:c140f6799583e787451c39b0039c03adff38330a6073adf1aa813b80473266d8",
                "sha256:b800b064a0971dac8abe17ed5a5b7e0239992220e2e7b87e44b42216f4027200",
                "sha256:3a6a4a046836fb006775ccc4feaf98bfd0cb6144ccf13f7aa21477f0894231b2",
                "sha256:696cb3ec82e9bf48c9885319b349334e586ab82d7f1d5661bc6e96d46471d2fb",
                "sha256:a2349bfceca1a0b3f483e7818dbb188494ec7e6125e17bb66fdc3e057c6553ae",
                "sha256:19d70fd56e6657c042bbb86375cd13e6f5d3f14a3d032065200464319f524739",
                "sha256:07fa9e3dc45416e6f0cdf715aaed0e1f3a090d4c312449603762ce7fd4dc5e5a"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

If you thing to notice, the image expose a port 8787 during the run time of the container and we will use to map the container to the browser:

docker inspect rocker/rstudio:4.4 | jq '.[]| .Config.ExposedPorts'

{
  "8787/tcp": {}
}

We will use the port argument (or -p) to expose the container port to the local port:

docker run --rm -ti \
 -v ~/Personal/tutorials/r-medicine-vscode-workshop/scripts/:/home/rstudio/scripts \
 -v ~/Personal/tutorials/r-medicine-vscode-workshop/data/:/home/rstudio/data \
-e PASSWORD=yourpassword \
-p 8787:8787 rocker/rstudio:4.4

Note: The port mapping logic of the port argument use the following logic - local_port:container_port.

We can pair the local RStudio config with the ones on the container by mounting the RStudio config’s folder:

docker run --rm -ti \
-v ~/Personal/tutorials/r-medicine-vscode-workshop/scripts/:/home/rstudio/scripts \
-v ~/Personal/tutorials/r-medicine-vscode-workshop/data/:/home/rstudio/data \
-v ~/.config/rstudio/:/home/rstudio/.config/rstudio \
-e PASSWORD=yourpassword \
-p 8787:8787 rocker/rstudio:4.4