You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 02-Launching-Docker.Rmd
+35-13Lines changed: 35 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
title: "Launching RStudion in Docker"
2
+
title: "Launching RStudio in Docker"
3
3
output:
4
4
dcTemplate::dc_lesson_template:
5
5
fig_width: 6
@@ -36,13 +36,13 @@ First things first: **install Docker**: on [Linux](https://docs.docker.com/linux
36
36
37
37
The first thing we need to do to launch Docker is to open a Unix Shell. If you're on Mac or Windows, in the last step you installed something called the *Docker Quickstart Terminal*; open that up now - it should look like just a plain shell prompt (`~$`), but really it's pointing at a linux virtual machine that Docker likes to run in, and this is where you should do everything for the rest of this tutorial unless otherwise noted. If you're on a linux machine, then you can just use a plain old terminal prompt.
38
38
39
-
Next, we will ask Docker to run an image that already exists, we will use the rstudio Docker image from [Rocker](https://github.com/rocker-org/rocker/wiki) which will allow us to run RStudio inside the container.
39
+
Next, we will ask Docker to run an image that already exists, we will use the hadleyverse Docker image from [Rocker](https://github.com/rocker-org/rocker/wiki) which will allow us to run RStudio inside the container and has many useful R packages already installed.
40
40
41
41
**call-out box**
42
42
`p` and `--rm` are flags that allow you to customize how you run the container. `p` tells Docker that you will be using a port (the location of which we specify afterwards as `8787:8787`). This is necessary to run RStudio in the container. Finally, --rm ensures that when we quit the container, the container is deleted. If we did not do this, everytime we run a container, a version of it will be saved to our local computer. This can lead to the eventual wastage of a lot of disk space until we manually remove these containers. Later we will show you how to save your container if you want to do so.
43
43
44
44
~~~
45
-
docker run --rm -p 8787:8787 rocker/rstudio
45
+
docker run --rm -p 8787:8787 rocker/hadleyverse
46
46
~~~
47
47
48
48
[//]: #(**TODO: call-out box**)
@@ -82,24 +82,25 @@ file -> open file. You will see that there are actually no files. The reason for
82
82
this is that this image came with no files.
83
83
Next, open a new R Script, e.g. by going to file -> New file -> R Script.
84
84
Enter the following code in the script, run it and save it.
85
+
85
86
```{r, eval=FALSE}
87
+
# make x the numbers from 1 to 5, and y the numbers from 6-10
86
88
x <- 1:5
87
89
y <- 6:10
88
90
89
-
pdf("firstplot.pdf")
91
+
# plot x against y
90
92
plot(x, y)
91
-
dev.off()
92
93
```
93
-
If you look at your files again now, you will see two files (the plot and the
94
-
script).
94
+
95
+
If you look at your files again now, you will see the script file.
95
96
96
97
Now, given that we used the `--rm` flag when we launched the Docker container, anything we create on the machine will be gone. Let's verify this. First, close the browser tab where you have RStudio open, and then go to your terminal window from where you launched the Docker container and type Contol+C. This shuts down the
97
98
Docker container.
98
99
99
-
Now relaunch your a Docker container using the RStudio image as you did previously, e.g., `docker run --rm -p 8787:8787 rocker/rstudio` in the terminal and typing `http://192.168.99.100:8787` in your browser window and see if the rscript and plot you saved is still there.
100
+
Now relaunch your a Docker container using the RStudio image as you did previously, e.g., `docker run --rm -p 8787:8787 rocker/hadleyverse` in the terminal and typing `http://192.168.99.100:8787` in your browser window and see if the rscript and plot you saved is still there.
100
101
101
102
102
-
## Linking a volume to a Docker container
103
+
## Linking a volume to a Docker container to access data and save files
103
104
104
105
That leads us to the question of, how can we save our work if the container is deleted when we exit the container? One solution is to link a volume (for example your local hard drive) to the container so that you can access the data there as well as being able to save things there.
105
106
@@ -108,17 +109,38 @@ This time when we launch our container we will use the `-v` flag along with the
108
109
[//]: #(**TODO: Explain how it can differ? /Users/<yourusername>/...?**)
109
110
110
111
~~~
111
-
docker run --rm -p 8787:8787 -v /Users/tiffanytimbers/Documents/DC/r-docker-tutorial:/home/rstudio/r-docker-tutorial rocker/rstudio
112
+
docker run --rm -p 8787:8787 -v /Users/tiffanytimbers/Documents/DC/r-docker-tutorial:/home/hadleyverse/r-docker-tutorial rocker/hadleyverse
112
113
~~~
113
114
114
115
Again, you will have to enter something like `http://192.168.99.100:8787` in your browser as the url to get RStudio to run.
115
116
116
-
This time when you launch RStudio and
117
+
This time when you launch RStudio in a Docker container and you try to open a file you should be able to see some files and directories. Now set the working directory to the directory called `r-docker-tutorial` and load the `gapminder-FiveYearData.csv` into R via `read.table`.
Now lets plot GDP per capita against life expectancy.
119
125
126
+
```{r knitr_init, echo = FALSE, cache = FALSE}
127
+
# load ggplot library
128
+
library(ggplot2)
129
+
130
+
# plot GDP against life expectancy
131
+
qplot(gap5yr$lifeExp, gap5yr$gdpPercap)
132
+
133
+
# save the plot
134
+
ggsave(filename = 'data/GDP_LifeExp.pdf')
135
+
```
136
+
137
+
Let's also save the script as `plot_GDP_LifeExp.R` in the `r-docker-tutorial` directory. Now close the RStudio browser and exit your Docker container via the terminal using Control+C. Then look inside the `r-docker-tutorial` and `r-docker-tutorial/data` directories on your laptop to see if you can see the two files you created.
120
138
121
139
## Summary
122
140
141
+
In this lesson we learned how to launch a Docker container that allows us to run RStudio in a browser. We learned that using the `--rm` flag when we run Docker makes the container ephemeral; meaning that it is automatically deleted after we close the container. We do this as to not build up a large collection of containers on our machine and waste space. We also learned that we can link a volume of our laptop to the Docker container if we want to be able to access and save data, scripts and any other files.
142
+
143
+
The container we used already had R, RStudio and several useful R packages installed. In later lessons we will learn how to modify this container to install new packages, and where we can find other Docker containers that might be useful for our work.
0 commit comments