Skip to content

Commit ee0f5ea

Browse files
committed
Finished 1st draft of 02-Launching-Docker.Rmd
1 parent febc132 commit ee0f5ea

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

02-Launching-Docker.Rmd

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Launching RStudion in Docker"
2+
title: "Launching RStudio in Docker"
33
output:
44
dcTemplate::dc_lesson_template:
55
fig_width: 6
@@ -36,13 +36,13 @@ First things first: **install Docker**: on [Linux](https://docs.docker.com/linux
3636

3737
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.
3838

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.
4040

4141
**call-out box**
4242
`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.
4343

4444
~~~
45-
docker run --rm -p 8787:8787 rocker/rstudio
45+
docker run --rm -p 8787:8787 rocker/hadleyverse
4646
~~~
4747

4848
[//]: # (**TODO: call-out box**)
@@ -82,24 +82,25 @@ file -> open file. You will see that there are actually no files. The reason for
8282
this is that this image came with no files.
8383
Next, open a new R Script, e.g. by going to file -> New file -> R Script.
8484
Enter the following code in the script, run it and save it.
85+
8586
```{r, eval=FALSE}
87+
# make x the numbers from 1 to 5, and y the numbers from 6-10
8688
x <- 1:5
8789
y <- 6:10
8890
89-
pdf("firstplot.pdf")
91+
# plot x against y
9092
plot(x, y)
91-
dev.off()
9293
```
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.
9596

9697
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
9798
Docker container.
9899

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.
100101

101102

102-
## Linking a volume to a Docker container
103+
## Linking a volume to a Docker container to access data and save files
103104

104105
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.
105106

@@ -108,17 +109,38 @@ This time when we launch our container we will use the `-v` flag along with the
108109
[//]: # (**TODO: Explain how it can differ? /Users/<yourusername>/...?**)
109110

110111
~~~
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
112113
~~~
113114

114115
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.
115116

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`.
118+
119+
```{r knitr_init, echo = FALSE, cache = FALSE}
120+
# load gapminder data from a csv on your computer
121+
gap5yr <- read.csv(file = 'data/gapminder-FiveYearData.csv')
122+
```
117123

118-
## Loading into the container plotting it
124+
Now lets plot GDP per capita against life expectancy.
119125

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.
120138

121139
## Summary
122140

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.
123144

124-
## Challenge Questions
145+
## Challenge Questions
146+
TBD

0 commit comments

Comments
 (0)