A step-by-step guide for customizing and launching RStudio Server inside a container with Docker Compose
In this post, we will review the steps of setting up a Docker-Compose workflow to launch an RStudio Server inside a container. We will introduce the Docker Compose settings process and discuss when you should consider using it. Last but not least, we will demonstrate how to transition the docker run workflow that was introduced in the previous post and launch the Rocker RStudio image with the Docker Compose process.
Related articles:
Running RStudio Inside a Container
By the end of this tutorial, you will be able to transition your docker run settings to a docker-compose.yml file and seamlessly launch your RStudio container with the docker-compose command.
Motivation
Before getting started, let’s explain what Docker Compose is and when you should consider using it.
Starting with the what — Docker Compose is a simplistic framework for launching single or multiple containers. It is a wrapper of the docker run command, and it manages the container launch settings using a YAML file. It is recommended to set a Docker Compose workflow instead of using the docker run command when:
- The number of arguments of thedocker run command increases, and it becomes convoluted to manage it via the CLI
- You regularly use and work with the container
- The level of complexity is high (e.g., launching multiple containers in parallel, etc.)
We use the docker-compose.yml file to set up a Docker Compose framework and launch it with the docker-compose command. This process includes mapping the docker run arguments into YAML format. Simple docker-compose.yml file settings will include the following two arguments:
- Version — the Docker Compose version, which is currently 3.9
- Services — a list of containers to launch and their corresponding arguments
Let’s illustrate the mapping process of the docker run command arguments to the docker-compose.yml file with the below example:
docker run -argument_a argument_a_values
-argument_b argument_b_values
-argument_c argument_c_values
IMAGE_NAME/IMAGE_TAG
Where this command has the following three arguments — argument_a, argument_b, argument_c, and their corresponding values are argument_a_values, argument_b_values, argument_c_values, and calling the following image — IMAGE_NAME/IMAGE_TAG.
The below docker-compose.yml represents the mapping of the above docker run arguments:
version: "3.9"
services:
my_service:
image: "IMAGE_NAME/IMAGE_TAG"
argument_a:
- "argument_a_values"
argument_b:
- "argument_b_values"
argument_c:
- "argument_c_values"
The version and services arguments, as mentioned above, define the Docker Compose version and the list of images to launch during the run time, respectively. In this case, we use the most recent version, 3.9, and define a single container under the services argument named my_service. Under the my_service section, we define the run time arguments corresponding to the above docker run command arguments following the standard YAML format.
It is important to note that naming convention mapping between the docker run command arguments and their settings in the docker-compose.yml file is not always one-to-one. The Docker Compose documentation is a great resource for identifying the argument settings.
In the next section, we will connect the dots and map the docker run command we set in the previous tutorial to a docker-compose.yml file.
Setting RStudio with Docker Compose
Recall that in the previous tutorial, we used the below docker run command to launch the RStudio server inside a container:
docker run --rm -ti
-v .:/home/rstudio
-v $HOME/.config/rstudio:/home/rstudio/.config/rstudio
-v $HOME/.Renviron:/home/rstudio/.Renviron
-e PASSWORD=yourpassword
-p 8787:8787 rocker/rstudio
In short, the above run command uses the following arguments:
- Volume or v to mount local folders with the container file system
- Environment or e to set the RStudio server password as an environment variable
- Port or p to map between the local and container ports
The below YAML file represents the mapping of the above docker run command:
version: "3.9"
services:
rstudio:
image: "rocker/rstudio"
ports:
- "8787:8787"
volumes:
- type: "bind"
source: "."
target: "/home/rstudio"
- type: "bind"
source: "$HOME/.config/rstudio"
target: "/home/rstudio/.config/rstudio"
- type: "bind"
source: "$HOME/.Renviron"
target: "/home/rstudio/.Renviron"
environment:
- PASSWORD=yourpassword
Where we set a single service named rstudio under the services argument and defined the corresponding run arguments:
- image — defines the image name, in this case, using the RStudio Rocker image rocker/rstudio
- ports — sets the port mapping between the local machine and the container
- volumes — maps the folders mount, using the type argument to define the type of mount and the source and target arguments to define the local and container folder path mapping. More details on the volume arguments can be found here.
- environment — defines environment variables, in this case, we set PASSWORD variable to define the RStudio server password
Once the YAML file is set, we can use the docker-compose command on the CLI to launch the RStudio container:
docker-compose up
Where the up argument is used to launch the container. You should expect the following output:
[+] Running 2/2
✔ Network rstudio-docker_default Created 0.1s
✔ Container rstudio-docker-rstudio-1 Created 0.1s
Attaching to rstudio-docker-rstudio-1
rstudio-docker-rstudio-1 | [s6-init] making user provided files available at /var/run/s6/etc...
rstudio-docker-rstudio-1 | exited 0.
rstudio-docker-rstudio-1 | [s6-init] ensuring user provided files have correct perms...
rstudio-docker-rstudio-1 | exited 0.
rstudio-docker-rstudio-1 | [fix-attrs.d] applying ownership & permissions fixes...
rstudio-docker-rstudio-1 | [fix-attrs.d] done.
rstudio-docker-rstudio-1 | [cont-init.d] executing container initialization scripts...
rstudio-docker-rstudio-1 | [cont-init.d] 01_set_env: executing...
rstudio-docker-rstudio-1 | skipping /var/run/s6/container_environment/HOME
rstudio-docker-rstudio-1 | skipping /var/run/s6/container_environment/PASSWORD
rstudio-docker-rstudio-1 | skipping /var/run/s6/container_environment/RSTUDIO_VERSION
rstudio-docker-rstudio-1 | [cont-init.d] 01_set_env: exited 0.
rstudio-docker-rstudio-1 | [cont-init.d] 02_userconf: executing...
rstudio-docker-rstudio-1 | [cont-init.d] 02_userconf: exited 0.
rstudio-docker-rstudio-1 | [cont-init.d] done.
rstudio-docker-rstudio-1 | [services.d] starting services
rstudio-docker-rstudio-1 | [services.d] done.
After launching the container, you can access the RStudio server from your browser using the local host address with the port number, in this case — http://localhost:8787:
Note: Once launching the container with the docker-compose up command, it keeps the CLI attached to the terminal until stopping it. Alternatively, you can add the d argument to run it in a detach mode:
docker-compose up -d
Likewise, the docker-compose down command stops the container run time.
Summary
In this tutorial, we reviewed how to set up a Docker Compose framework to launch your RStudio container. This includes setting a docker-compose.yml file and using the docker-compose command to launch the container concisely.
The motivations for wrapping your docker run command with Docker Compose are:
- Efficient and concise — required a one-time setting and, afterward, the launch time is simple with thedocker-compose command (as opposed to a long docker run command)
- Higher complexity — it simplifies the process of launching a single or multiple containers seamlessly. For example, one good use case would be running the RStudio and Postgres database together. In this case, you can set the Docker Compose process to launch the two containers to work side-by-side
Resources
- Running RStudio Inside a Container — https://towardsdatascience.com/running-rstudio-inside-a-container-e9db5e809ff8
- Docker Compose — https://docs.docker.com/compose/
- The Rocker Project — https://rocker-project.org/
- Docker Hub — https://hub.docker.com/
- RStudio Docker Compose template — https://github.com/RamiKrispin/rstudio-docker-template
Customizing RStudio Container with Docker Compose was originally published in Towards Data Science on Medium, where people are continuing the conversation by highlighting and responding to this story.
Originally appeared here:
Customizing RStudio Container with Docker Compose
Go Here to Read this Fast! Customizing RStudio Container with Docker Compose