Skip to content

Commit a2bf4f6

Browse files
allow multistage docker (#84)
* feat Add `append` parameter to `write()` to allow appending to file instead of overwriting * feat allow multistage dockerfile
1 parent 8a3df24 commit a2bf4f6

File tree

6 files changed

+58
-18
lines changed

6 files changed

+58
-18
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: dockerfiler
22
Title: Easy Dockerfile Creation from R
3-
Version: 0.2.4
3+
Version: 0.2.4.9000
44
Authors@R: c(
55
person("Colin", "Fay", , "contact@colinfay.me", role = c("cre", "aut"),
66
comment = c(ORCID = "0000-0001-7343-1846")),

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# dockerfiler 0.2.4.9xxx
2+
3+
- allow multistage dockerfile creation
4+
15
# dockerfiler 0.2.4
26

37
- remove native pipe thanks to @HenningLorenzen-ext-bayer, this enable to use of older R versions

R/add.R

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,22 @@ add_add <- function(
3131
glue("ADD {from} {to}")
3232
}
3333

34-
add_copy <- function(
35-
from,
36-
to,
37-
force = TRUE
38-
) {
34+
add_copy <- function(from,
35+
to,
36+
stage = NULL,
37+
force = TRUE) {
3938
if (!force) {
40-
warn_if_not(
41-
normalizePath(from),
42-
file.exists,
43-
"The file `from` doesn't seem to exists"
44-
)
39+
warn_if_not(normalizePath(from),
40+
file.exists,
41+
"The file `from` doesn't seem to exists")
42+
}
43+
44+
if (is.null(stage)) {
45+
glue("COPY {from} {to}")
46+
} else {
47+
glue("COPY --from={stage} {from} {to}")
48+
4549
}
46-
glue("COPY {from} {to}")
4750
}
4851

4952
add_workdir <- function(where) {

R/dockerfile.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ self$Dockerfile <- c(self$Dockerfile, add_add(from, to, force))
3939
#' @param from The source file.
4040
#' @param to The destination file.
4141
#' @param force If TRUE, overwrite the destination file.
42+
#' @param stage Optional. Name of the build stage (e.g., `"builder"`) to copy files from. This corresponds to the `--from=` part in a Dockerfile COPY instruction (e.g., `COPY --from=builder /source /dest`). If `NULL`, the `--from=` argument is omitted.
4243
#' @return the Dockerfile object, invisibly.
43-
COPY = function(from, to, force = TRUE) {
44-
self$Dockerfile <- c(self$Dockerfile, add_copy(from, to, force))
44+
COPY = function(from, to, stage= NULL , force = TRUE) {
45+
self$Dockerfile <- c(self$Dockerfile, add_copy(from, to, stage, force))
4546
},
4647
#' @description
4748
#' Add a WORKDIR command.
@@ -162,9 +163,10 @@ cat(self$Dockerfile, sep = "\n")
162163
#' @description
163164
#' Write the Dockerfile to a file.
164165
#' @param as The file to write to.
166+
#' @param append boolean, if TRUE append to file.
165167
#' @return used for side effect
166-
write = function(as = "Dockerfile") {
167-
base::write(self$Dockerfile, file = as)
168+
write = function(as = "Dockerfile", append = FALSE) {
169+
base::write(self$Dockerfile, file = as, append = append)
168170
},
169171
#' @description
170172
#' Switch commands.

man/Dockerfile.Rd

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-copy-stage.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
test_that("Dockerfile COPY with build stage works correctly", {
2+
# Création des deux étapes du Dockerfile
3+
stage_1 <- Dockerfile$new(FROM = "alpine", AS = "builder")
4+
stage_1$RUN('echo "coucou depuis le builder" > /coucou')
5+
6+
stage_2 <- Dockerfile$new(FROM = "ubuntu")
7+
stage_2$COPY(from = "/coucou", to = "/truc.txt", force = TRUE, stage = "builder")
8+
stage_2$RUN("cat /truc.txt")
9+
10+
tmpfile <- tempfile(fileext = ".Dockerfile")
11+
stage_1$write(as = tmpfile)
12+
stage_2$write(as = tmpfile, append = TRUE)
13+
14+
docker_lines <- readLines(tmpfile)
15+
16+
expect_length(docker_lines, 5)
17+
18+
expect_equal(docker_lines[1], "FROM alpine AS builder")
19+
expect_equal(docker_lines[2], 'RUN echo "coucou depuis le builder" > /coucou')
20+
expect_equal(docker_lines[3], "FROM ubuntu")
21+
expect_equal(docker_lines[4], "COPY --from=builder /coucou /truc.txt")
22+
expect_equal(docker_lines[5], "RUN cat /truc.txt")
23+
24+
expect_true(any(grepl("COPY --from=builder", docker_lines)))
25+
expect_true(any(grepl("/coucou", docker_lines)))
26+
expect_true(any(grepl("/truc.txt", docker_lines)))
27+
})

0 commit comments

Comments
 (0)