A step-by-step guide for customizing and launching RStudio Server inside a container with Docker Compose
On this post, we are going to review the steps of organising a Docker-Compose workflow to launch an RStudio Server inside a container. We’ll introduce the Docker Compose settings process and discuss when it is best to think about using it. Last but not least, we are going to display easy methods to transition the docker run workflow that was introduced within the previous post and launch the Rocker RStudio image with the Docker Compose process.
Related articles:
By the tip of this tutorial, you’ll have the ability 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 began, let’s explain what Docker Compose is and when it is best to think about using it.
Starting with the what — Docker Compose is a simplistic framework for launching single or multiple containers. It’s a wrapper of the docker run command, and it manages the container launch settings using a YAML file. It’s endorsed to set a Docker Compose workflow as a substitute of using the docker run command when:
- The variety of arguments of the
docker runcommand increases, and it becomes convoluted to administer it via the CLI - You commonly use and work with the container
- The extent of complexity is high (e.g., launching multiple containers in parallel, etc.)
We use the docker-compose.yml file to establish a Docker Compose framework and launch it with the docker-compose command. This process includes mapping the docker run arguments into YAML format. Easy docker-compose.yml file settings will include the next two arguments:
- Version — the Docker Compose version, which is currently 3.9
- Services — an inventory of containers to launch and their corresponding arguments
Let’s illustrate the mapping technique 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 next 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 next 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 throughout the run time, respectively. On this case, we use essentially 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 usual YAML format.
It’s important to notice that naming convention mapping between the docker run command arguments and their settings within the docker-compose.yml file isn’t all the time one-to-one. The Docker Compose documentation is an important resource for identifying the argument settings.
In the subsequent section, we are going to connect the dots and map the docker run command we set within the previous tutorial to a docker-compose.yml file.
Setting RStudio with Docker Compose
Recall that within 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 brief, the above run command uses the next arguments:
- Volume or
vto mount local folders with the container file system - Environment or
eto set the RStudio server password as an environment variable - Port or
pto 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: "."
goal: "/home/rstudio"
- type: "bind"
source: "$HOME/.config/rstudio"
goal: "/home/rstudio/.config/rstudio"
- type: "bind"
source: "$HOME/.Renviron"
goal: "/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, on this case, using the RStudio Rocker imagerocker/rstudioports— sets the port mapping between the local machine and the containervolumes— maps the folders mount, using thetypeargument to define the variety of mount and thesourceandgoalarguments to define the local and container folder path mapping. More details on the amount arguments might be found here.environment— defines environment variables, on this case, we setPASSWORDvariable to define the RStudio server password
Once the YAML file is ready, we will 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. It’s best to expect the next 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 possibly can access the RStudio server out of your browser using the local host address with the port number, on 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 possibly 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
On this tutorial, we reviewed easy methods to arrange 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 straightforward with the
docker-composecommand (versus a protracteddocker runcommand) - Higher complexity — it simplifies the technique of launching a single or multiple containers seamlessly. For instance, one good use case could be running the RStudio and Postgres database together. On this case, you possibly can set the Docker Compose process to launch the 2 containers to work side-by-side