A step-by-step guide to customizing and starting RStudio Server inside a container with Docker Compose
In this post, we will review the steps to set up a Docker-Compose workflow to start an RStudio server inside a container. We'll introduce the Docker Compose setup process and discuss when you should consider using it. Last but not least, we will demonstrate how to make the transition docker run
workflow that was introduced in the Previous post and launch the Image Rocker RStudio with the Docker Compose process.
Related Posts:
At the end of this tutorial, you will be able to transition your docker run
adjustments to a docker-compose.yml
file and seamlessly start your RStudio container with the docker-compose
domain.
Motivation
Before we begin, let's explain what Docker Compose is and when you should consider using it.
Starting with the that – Docker Compose is a simplistic framework for launching single or multiple containers. It is a wrapper of docker run
command and manages the container startup configuration using a YAML file. It is recommended to configure a Docker Compose workflow instead of using the docker run
domain when:
- The number of arguments of the
docker run
The command grows and becomes complicated to manage through the CLI. - Use and work with the container regularly.
- The level of complexity is high (e.g. launching multiple containers in parallel, etc.)
We use the docker-compose.yml
file to configure a Docker Compose framework and run it with the docker-compose
domain. This process includes mapping the docker run
arguments in YAML format. Simple docker-compose.yml
The file configuration will include the following two arguments:
- Version — Docker Compose version, which is currently 3.9
- Services — a list of containers to launch and their corresponding arguments
Let's illustrate the process of mapping the docker run
command arguments for the docker-compose.yml
file with the following 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 next 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"
He version
and services
The arguments, as mentioned above, define the version of Docker Compose and the list of images to be launched during runtime, respectively. In this case, we use the latest version, 3.9, and define a single container under the services
named argument my_service
. Under the my_service
section, we define the runtime arguments corresponding to the above docker run
command arguments following the standard YAML format.
It is important to note that assigning naming conventions between docker run
command arguments and their settings in the docker-compose.yml
The file is not always one to one. The Docker Compose documentation is a great resource for identifying argument settings.
In the next section, we will connect the dots and map the docker run
command that we configured in the previous tutorial to a docker-compose.yml
archive.
Configuring RStudio with Docker Compose
Remember that in the previous tutorial, we used the following docker run
command to start 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 summary, the run command above 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
map between local and container ports
The following YAML file represents the mapping of the above. docker run
domain:
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 configure a single service called rstudio
under the services
argument and defined the corresponding execution arguments:
image
— defines the name of the image, in this case, using the RStudio Rocker imagerocker/rstudio
ports
— sets the port mapping between the local machine and the containervolumes
— assign folder mounting, using thetype
argument to define the mount type and thesource
andtarget
arguments to define the local folder and container path mapping. More details on the volume's arguments can be found. here.environment
— defines environment variables, in this case, we configurePASSWORD
variable to define the RStudio server password
Once the YAML file is configured, we can use the docker-compose
command in the CLI to start the RStudio container:
docker-compose up
Where he up
The argument is used to start the container. You should expect the following result:
(+) 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 starting the container, you can access the RStudio server from your browser using the localhost address with port number, in this case: http://localhost:8787:
Note: Once the container with the docker-compose up
command, keeps the CLI connected to the terminal until you stop it. Alternatively, you can add the d
argument to run it in offline mode:
docker-compose up -d
Also, the docker-compose down
The command stops the container's runtime.
Summary
In this tutorial, we review how to configure a Docker Compose framework to start your RStudio container. This includes establishing a docker-compose.yml
file and using the docker-compose
command to start the container concisely.
The motivations to wrap your docker run
The command with Docker Compose are:
- Efficient and concise: required a one-time setup and then startup time is simple with the
docker-compose
command (as opposed to a longdocker run
domain) - Increased complexity: Simplifies the process of launching one or more containers without problems. For example, a good use case would be to run the RStudio and Postgres database together. In this case, you can configure the Docker Compose process to start the two containers so that they run side by side.