-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathREADME.Rmd
More file actions
219 lines (161 loc) · 4.74 KB
/
README.Rmd
File metadata and controls
219 lines (161 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
eval = FALSE,
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
<!-- badges: start -->
[](https://github.com/ThinkR-open/dockerfiler/actions)
[](https://app.codecov.io/github/ThinkR-open/dockerfiler?branch=master)
<!-- badges: end -->
# `{dockerfiler}`
The goal of `{dockerfiler}` is to provide an easy way to create Dockerfiles from R.
## About
You're reading the doc about version :
```{r eval = TRUE}
desc::desc_get_version()
```
## Installation
You can install dockerfiler from GitHub with:
```{r gh-installation, eval = FALSE}
# install.packages("remotes")
remotes::install_github("ThinkR-open/dockerfiler")
```
Or from CRAN with :
```{r, eval = FALSE}
install.packages("dockerfiler")
```
## Basic worflow
By default, Dockerfiles are created with `FROM "rocker/r-base"`.
You can set another FROM in `new()`
```{r}
library(dockerfiler)
# Create a dockerfile template
my_dock <- Dockerfile$new()
my_dock$MAINTAINER("Colin FAY", "contact@colinfay.me")
```
Add comments to your Dockerfile
```{r}
my_dock$COMMENT("Install required R package.")
```
Wrap your raw R Code inside the `r()` function to turn it into a bash command with `R -e`.
```{r}
my_dock$RUN(r(install.packages("attempt", repo = "http://cran.irsn.fr/")))
```
Classical Docker stuffs:
```{r}
my_dock$COMMENT("Copy Plumber API and main script to container.")
my_dock$RUN("mkdir /usr/scripts")
my_dock$RUN("cd /usr/scripts")
my_dock$COPY("plumberfile.R", "/usr/scripts/plumber.R")
my_dock$COPY("torun.R", "/usr/scripts/torun.R")
my_dock$COMMENT("Expose the API port and run the main script when the container starts.")
my_dock$EXPOSE(8000)
my_dock$CMD("Rscript /usr/scripts/torun.R ")
```
See your Dockerfile :
```{r}
my_dock
```
If you've made a mistake in your script, you can switch lines with the `switch_cmd` method. This function takes as arguments the positions of the two cmd you want to switch :
```{r}
# Switch line 8 and 7
my_dock$switch_cmd(8, 7)
my_dock
```
You can also remove a cmd with `remove_cmd`:
```{r}
my_dock$remove_cmd(8)
my_dock
```
This also works with a vector:
```{r}
my_dock$remove_cmd(5:7)
my_dock
```
`add_after` add a command after a given line.
```{r}
my_dock$add_after(
cmd = "RUN R -e 'remotes::install_cran(\"rlang\")'",
after = 3
)
```
Save your Dockerfile:
```{r eval = FALSE}
my_dock$write()
```
## Multi-stage dockerfile
Here is an example of generating a multi-stage Dockerfile directly from R: we create two Dockerfile objects, one for the build stage (builder) and one for the final stage (final), and then merge them into a single file.
```{r}
stage_1 <- Dockerfile$new(
FROM = "alpine",AS ="builder"
)
stage_1$RUN('echo "Hi from builder" > /coucou.txt')
stage_2 <- Dockerfile$new(
FROM = "ubuntu", AS = "final"
)
stage_2$COMMENT("copy /coucou.txt from builder to /truc.txt in final")
stage_2$COPY(from = "/coucou",to = "/truc.txt",force = TRUE, stage ="builder")
stage_2$RUN( "cat /truc.txt")
stage_1$write()
stage_2$write(append = TRUE)
#file.edit("Dockerfile")
```
## Create a Dockerfile from a DESCRIPTION
You can use a DESCRIPTION file to create a Dockerfile that installs the dependencies and the package.
```{r}
my_dock <- dock_from_desc("DESCRIPTION")
my_dock
my_dock$CMD(r(library(dockerfiler)))
my_dock$add_after(
cmd = "RUN R -e 'remotes::install_cran(\"rlang\")'",
after = 3
)
my_dock
```
## Create a Dockerfile from renv.lock
- Create renv.lock
```{r, message=FALSE, results='hide'}
dir_build <- tempfile(pattern = "renv")
dir.create(dir_build)
# Create a lockfile
the_lockfile <- file.path(dir_build, "renv.lock")
custom_packages <- c(
# attachment::att_from_description(),
"renv",
"cli",
"glue",
"golem",
"shiny",
"stats",
"utils",
"testthat",
"knitr"
)
renv::snapshot(
packages = custom_packages,
lockfile = the_lockfile,
prompt = FALSE
)
```
- Build Dockerfile
```{r}
my_dock <- dock_from_renv(
lockfile = the_lockfile,
distro = "focal",
FROM = "rocker/verse"
)
my_dock
```
## Contact
Questions and feedbacks [welcome](mailto:contact@colinfay.me)!
You want to contribute ? Open a [PR](https://github.com/ThinkR-open/dockerfiler/pulls) :) If you encounter a bug or want to suggest an enhancement, please [open an issue](https://github.com/ThinkR-open/dockerfiler/issues).
Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md).
By participating in this project you agree to abide by its terms.