Using Docker for R: MacOS

Over winter break, I’ve made it a goal of mine to buckle down and teach myself GIS in R. Unfortunately, the package rgdal is a bit difficult to install on Mac. For a cleaner, easier way to use R for GIS on my Mac I decided to use Docker, a tool for creating containers in which to use R. I don’t pretend to be a Docker expert (far from it) but the essence of the tool is that there are “images” which you can pull down and create “instances” of. It’s a lot like first installing a program and then calling it into your library.

What’s great about Docker is that you can run something independent of your own operating system, if you have a Mac (like me) and have noticed that some aspects of the OS just make working with certain R packages difficult, you can run a version of R Studio in your browser that thinks it’s running on a Linux or other system. Since a container is operating outside of your local computer, I’m told it can be great for collaborative purposes and there’s a whole community known as “Rocker” that has great tutorials and tips on how to use Docker for R.

As I was learning it myself, I was having a bit of trouble as I’m not incredibly Terminal-saavy. So, I wanted to share a “dumb” version of what I did to create a container from the image rocker/geospatial.

1. Download Docker, if you haven’t already. It’s pretty self-explanatory and easy, you create an account and download a desktop app.

2. Go ahead and go through the brief tutorial; it helps, and makes the couple of lines of code below much easier to explain.

3. Open up a Terminal window.

4. In Terminal, type the following command: docker pull rocker/geospatial

5. Wait for everything to get pulled down. This shouldn’t take too long, and you can watch the loading progress.

6. Run the following:

docker run -it -e PASSWORD=<insert_password_here> -p 8787:8787 rocker/geospatial

Where –it refers to “interactive,” and the numbers “8787” refer to the standard port for where you will be using RStudio. This is the line of code you will want to run each time you want to open the container. Then, you can open a browser and navigate to http://localhost:8787/.

Note: Exiting out of the RStudio window does NOT remove the container; as long as the Terminal window is running, you should be good to go.

Leave a comment