“I am going to assume you have probably heard about docker, this post is if you want to just get your R Studio and Jupyter Notebook”
R Studio
Getting R Studio running in a container is relatively straight forward.
docker run -d -p 8787:8787 -e PASSWORD=yourpassword -v /"$PWD":/home/rstudio/name_of_folder rocker/rstudio
Let’s breakdown what each step is doing,
docker run - does and docker create and docker start at the same time. If the image does not exist it will pull.
-d - will run the docker container in the background. Allows you to close your terminal.
-p - assigns the port to connect to your container localhost:container
-e - Allows us to pass environment variables to the container. In this case we are passing the password.
-v - Mounting a volume to persist the results of our analysis. (remember containers are ephemeral). In this case full_path_locally:path_in_container. If the path in container does not exist it will be created automatically.
When you run this code you can visit your browser and type localhost:8787. Enter user as rstudio and your password.
Jupyter Notebook
Since you’ve got R Studio running already I will make this one short. The benefit with the Jupyer Notebook is that you can run both python 3 and R kernels. Makes them an amazing choice for writing a markdown document.
docker run -d -p 8888:8888 -e JUPYTER_TOKEN="yourpassword" -v "$PWD":/home/jovyan/work jupyter/datascience-notebook
When you run this code you can visit your browser and type localhost:8888. Enter token as your password.
To check all installed packages. Start a new python 3 kernel in the work folder and run:
import pkg_resources
dists = [d for d in pkg_resources.working_set]
dist
A better way : Docker Compose
I know what you’re thinking. “There must be a better way to run both containers at the same time.” You’re right. There is. It’s docker compose.
Docker compose uses a YAML configuration file to spin up multiple containers at the same time.
There’s just a bit more work to it but it’s all worth it.
Your working directory should look like this.
datafolder containing the data you plan on reading in. This will also be the folder used to mount to the container.configfolder containing your .env files to pass variables to the container. Like the token and password.servicesfolder - inside you should have a folder for each service in our caserstudioandjupyterin each of the folders you can put in a Dockerfile. (We’ll talk about that some other time)docker-compose.yml- This is the main file that will be run. The configuration will glue all the parts together.
Lets get started
- Pip install Docker Compose
pip install docker-compose
- Clone the Repo
git clone https://github.com/zanalytics/docker-rstudio-jupyter.git cd docker-rstudio-jupyter - Spin up both containers
docker-compose up -d --build - Stoping all containers
docker stop $(docker ps -a -q) docker rm $(docker ps -a -q) - Deleting all images
docker rmi $(docker images -a -q)
Hope you enjoyed see you next time.