All prerequisites, links to material and slides for this course can be found on github.
Or can be downloaded as a zip archive from here.
Once the zip file in unarchived. All presentations as HTML slides and pages, their R code and HTML practical sheets will be available in the directories underneath.
Something works on your computer (e.g. bioinformatics analysis or software deployment), and you want to make sure that it will work on another computer.
Docker allows for the creation of an isolated environment that can be shipped across different users, machines, or operating systems, and to virtual machines or the cloud.
Once installed on your computer, Docker runs a process called the Docker daemon. A daemon is a program that runs as a background process and is not under direct control of the computer user, and the Docker daemon is the engine that manages Docker services and objects by communicating with the client.
The Docker client, typically the command line interface, communicates with the Docker daemon based on user commands.
The 'docker image build' command uses a Dockerfile to create an image.
A Docker image is a read-only, isolated file system that contains all software, dependencies, scripts, and metadata required to run a container.
Once an image is built, an instance of this image can be launched as a stand-alone application, also known as a container.
There are public repositories of Docker images (e.g. Docker Hub), and typically you start with an existing image and build on top of this.
Use this link to install Docker.
Check Docker version to make sure Docker is installed and running.
Code (terminal):
docker --version
Output:
If previous command isn't found check the Docker Desktop advanced settings and make sure CLI tools are available system-wide.
Rocker is a very useful source of images on Docker Hub for R and RStudio. We can pull these images immediately after installing Docker. Here we pull an image containing RStudio and a specific version of R.
Code (terminal):
docker image pull rocker/rstudio:4.4.0# alias of 'docker pull' from older Docker versions
After pulling, the image is now available on our system to run.
Images have names, tags, and image IDs as shown in the output. The ID is a hash of the metadata and filesystem of the Docker image.
Code (terminal):
docker images
Output:
After pulling, the image is now available on our system to run.
Images have names, tags, and image IDs as shown in the output. The ID is a hash of the metadata and filesystem of the Docker image.
We can also view the image in Docker desktop:
Once the image is on our system, we can launch a container with the 'docker container run' command.
Components of the run command:
Code (terminal):
docker container run --rm \ -p 8787:8787 \ -e PASSWORD=password123 \ rocker/rstudio:4.4.0# alias of 'docker run' from older Docker versions
While the container is running, we can go to 'http://localhost:8787' in a browser and log in with the the user 'rstudio' and the password from 'docker container run'.
To see all containers running in the local environment, use the 'docker container ls' command
Code (terminal):
docker container ls# alias of 'docker ps' from older Docker versions
Output:
To stop the container currently running, if you are in the terminal tab where it was launched press Ctrl+C.
Or open up another tab and the 'docker stop' command can be used with the ID listed from 'docker container ls'
Code (terminal):
docker container stop 8b1619f9189d # this is the ID from 'docker container ls'docker container ls
Output:
The docker container has it's own file system, and we can mount a local directory onto that file system with the '-v' flag for the 'docker container run' command.
Code (terminal):
# navigate to 'r_course' directory in downloaded material cd ~/Downloads/RU_reproducibleR-master/r_course # launch docker container docker container run --rm \ -v ./data:/home/rstudio \ -p 8787:8787 \ -e PASSWORD=password123 \ rocker/rstudio:4.4.0
The RStudio interface now shows the files in the 'data' directory
These files can be read into R, and also files can be written to the local environment
Code (R in docker image):
dataIn <- read.csv("readThisTable.csv")head(dataIn, 2)# add gene IDs and write to new file on local computerdataIn$Gene_ID <- seq(nrow(dataIn))write.csv(dataIn, "rnaseq_table_withIDs.csv")
These files can be read into R, and also files can be written to the local environment
Output:
In addition to the files deliberately written to the local directory, the R environment files from this RStudio session are written to the working directory in the container, and therefore are copied to the local directory as hidden folders (.config and .local).
This R environment will then be loaded the next time you launch an RStudio container with this volume mounted. While this is normally okay, if desired a fresh RStudio session can be launched with the same mounted volume by removing these hidden directories.
Code (terminal):
# For windows use: dir /als -a datarm -r data/.local data/.config
Output:
The image we pull from Rocker contains base R and its associated packages. To customize the image, we will need to make a Dockerfile that adds to the Rocker image.
A Dockerfile provides the recipe to make the image. Using specialized commands, this file provides instructions to install the R packages and its dependencies.
Some examples:
Here we start with the same RStudio base image we used previously, and then add some key R packages.
The first RUN command installs system dependencies that are common to R packages. This command looks for updates, installs, and cleans up unnecessary files.
This is a list of libraries is not comprehensive and adding more R packages could result in missing dependencies.
These can be identified in the log for the build command and manually added to the apt-get command, or alternatively, dependencies for a specific R or Python package can be found using the Posit package manager. This resource can also be used to install only those libraries that are necessary, which can help to limit the size of the image.
Then the R packages are installed using 'install.packages' or 'BiocManager::install' for Bioconductor packages.
Note: The 'options(warn=2)' at the beginning of the R command will stop the installation when there is a warning, making it easier to debug.
The port 8787 is exposed and the 'init' script that is included with the base RStudio image. These commands are specific to this image and will vary depending on what the Docker container is meant to do when launched.
Code (terminal):
cd ~/Downloads/RU_reproducibleR-master/r_coursedocker image build -t rocker/rstudio:4.4.0_v2 ./data
Output:
Use the docker images command to see image
Code (terminal):
docker images
Output:
As done previously, use the docker container run command to launch a container with our customized RStudio session
Code (terminal):
docker container run --rm \ -v ./data:/home/rstudio \ -p 8787:8787 \ -e PASSWORD=password123 \ rocker/rstudio:4.4.0_v2
As done previously, use the docker container run command to launch a container with our customized RStudio session
Output:
The directory that contains the Dockerfile is the last argument
This Dockerfile is not named 'Dockerfile', so we specify the exact path with '-f' argument
Code (terminal):
docker image build -t rocker/rstudio:4.4.0_samtools -f ./data/Dockerfile_samtools ./data/
Output:
Code (terminal):
docker images
Output:
Code (terminal):
docker container run --rm \ -v ./data:/home/rstudio \ -p 8787:8787 \ -e PASSWORD=password123 \ rocker/rstudio:4.4.0_samtools
Code (R in docker image):
library(Herper)# the environment name and miniconda path set in the DockerfileHerper::local_CondaEnv(new = "pipe_env", pathToMiniConda = "/home/miniconda")# test out samtoolssystem("samtools --help")
Output:
We can also run containers from Docker Desktop
We can also run containers from Docker Desktop
Oftentimes Docker is used to run a specific set of software interactively within the terminal. This allows for running custom scripts contained within the Docker container as well.
So far we have only been using the R Studio Docker image which provides it's own user interface. Here we will use Docker to run a specific version of R and R packages in the terminal interactively. The Rocker suite has images that just contain R, without R studio.
All we have to do is change the base Docker image that we pull, and change the command at the end of the Dockerfile to 'R' so it will run R once we launch a container.
After building this image as we have done previously, it should be run with the '-it' flag, which will run this container within an interactive terminal. Using these flags, when the 'R' command at the end of the Dockerfile is run upon launch, it will open the version of R in the image.
First we build this image from the Dockerfile like we've done before.
Code (terminal):
docker image build -t rocker/r-ver:4.4.0_cust -f ./data/Dockerfile_R ./data/
Then we run a container with the data directory mounted and the '-it' flag.
Code (terminal):
cd ~/Downloads/RU_reproducibleR-master/r_coursedocker container run -it -v ./data:/data rocker/r-ver:4.4.0_cust
*output on next slide
Output (terminal):
Python also has images available on Docker Hub and we can make a Dockerfile that has a specific version of Python and any other packages we want available in that environment.
Here we pull the python image, use pip to install a Python package (scanpy), then end with the 'python' command to open a Python session in the terminal.
Dockerfile:
Code (terminal):
docker image build -t python_scanpy -f ./data/Dockerfile_scanpy ./data/cd ~/Downloads/RU_reproducibleR-master/r_coursedocker container run -it -v ./data:/data python_scanpy
If we then want to share our images with someone else, or simply store them elsewhere for future use, we can push to Docker Hub.
Make sure you have an account on Docker Hub.
Then create a repository with the same name as the image we want to push. Let's share the R Studio image with samtools, so it would be 'rstudio_4.4.0_samtools'. A repository must exist on Docker Hub before pushing to it.
Once the repository is created on Docker Hub, we do the following steps to push the image:
Code (terminal):
# log in and provide credentials used to sign into Docker Hub# include the username you used for dockerhub and this will prompt you to enter the passworddocker login -u (enter your username) # tag the image you want to push with your Docker Hub username and a tag name after the colon# the ID is from the 'docker images' commanddocker image tag 292c85d1812f rubrc/rstudio_4.4.0_samtools:topush# push to Docker Hubdocker image push rubrc/rstudio_4.4.0_samtools:topush
If we then want to share our images with someone else, or simply store them elsewhere for future use, we can push to Docker Hub.
Make sure you have an account on Docker Hub.
renv and Docker can be used in tandem to easily recreate and R environment.
There is a renv lock file in the 'r_course/data/renv_docker' folder within the course materials. This lock file generated by renv shows the versions of R and the loaded packages that were part of my project.
We will now use renv within a Dockerfile. R still needs to be installed to use renv, so we use Rocker again to install a specific version of R to match the renv lock file.
When building the image the lock file is copied to the image into a directory that is created and set as the working directory with the WORKDIR command.
Build the image with the build context (last argument) set to the directory containing the Dockerfile and the lock file, then launch a container.
cd ~/Downloads/RU_reproducibleR-master/r_course# build the image docker image build -t rocker/rstudio:4.4.0_renv ./data/renv_docker# launch a containerdocker container run --rm \ -v ./data/renv_docker:/home/rstudio \ -p 8787:8787 \ -e PASSWORD=password123 \ rocker/rstudio:4.4.0_renv
Docker is generally not allowed on the HPC due to the need to have root access. You can use Docker images by using another containerization software called Apptainer.
Apptainer was designed to play nice with Docker and the Apptainer manual goes into detail about how to use Apptainer with Docker images.
Apptainer is not installed on the head nodes, so you can run interactively if your lab has it's own node, or you can submit jobs to the HPC scheduler.
# login to your lab node# pull image from Docker Hubapptainer pull docker://rocker/r-ver:4.4.0# open an R console using the resulting Apptainer imageapptainer exec r-ver_4.4.0.sif R
Any suggestions, comments, edits or questions (about content or the slides themselves) please reach out to our GitHub and raise an issue.
All prerequisites, links to material and slides for this course can be found on github.
Or can be downloaded as a zip archive from here.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |