Tutorial 4
Virtual environments and docker containers. We’ll practice creating virtual environments again (just like last tutorial), so we can compare it with creating docker containers.
R Environments
- Create a new RStudio Project
- Note the currently installed packages
- Install the
renvlibrary if you do not have it installed - Initialize the current project as an
renvproject - Note the new packages in the environment.
- Install an R package
- Snapsnot the package
- Note how the
renv.lockfile does not change - Create and save an R script that uses the package you just installed
- Re-snapshot the project
- Note how the
renv.lockfile updates now - Close the current RStudio project
- Note how you have your old packages back
- Re-open the RStudio project you just created and see how t5he packages change.
Python environment
When using Python, conda environments have a slightly different workflow when compared to renv.
- Check that you are currently in the
baseconda environment:
conda info- Note the packages in the current conda environment and their versions
conda list- Create a new conda environment (we are going to name it
myenv) with python 3.12
conda create -n myenv -c conda-forge python=3.12- Activate this environment
- Run python and compare your python versions and see if you are able to import
pandas condainstall another package into the environment,pandasand import it from the new environment.
Run a docker container
Let’s get some practice with running docker containers
Docker Desktop needs to be running in the background.
Your first container
We did this in the Computer Setup process, but let’s run the hello-world container with docker run
$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
$This command runs a container that executes a script that prints out the hello docker text. When this script runs, it exits and the container exits to give you your prompt back
An R Container
- Compare the version of R you have right now on your computer. You can open up and launch
Ror runR --versionto get your current R version. - Run the R 4.3.2 container:
docker run --rm -it rocker/r-ver:4.3.2- Compare the version from the version you have on your computer
- Try to load a package you have installed on your computer in the container (it should fail)
- Exit out of this docker container
A Python Container
- Compare the version of Python you have right now on your computer. You can run
python --versionto get your current Python version. - Run the latest python:
docker run --rm -it python- Compare the version from the version you have on your computer, once the container starts, Python should display its version above the prompt.
- Try to load a package you have installed on your computer in the container (it should fail)
- Exit out of this docker container
- Run the Python container with the tag “3.12”
docker run --rm -it python:3.12- Check the version of Python this prompt shows, and then exit the container.
- What comes after the
:appended to the image name is a user-chosen tag that has no direct connection to the contents of the Docker image. But it should provide a semantically meaningful addition to the name. In this instance, why did the developers choose to tag this version of thepythondocker image with the text3.12?