knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
dockr
0.8.6 is now available on CRAN. dockr
is a minimal toolkit to build a
lightweight Docker container image for your R package, in which the package
itself is available. The Docker image seeks to mirror your R session as close as
possible with respect to R specific dependencies. Both dependencies on CRAN
R packages as well as local non-CRAN R packages will be included in the Docker
container image.
If you want to know, how Docker works, and why you should consider using Docker, please take a look at the Docker website.
Install the development version of dockr
with:
remotes::install_github("smaakage85/dockr")
Or install the version released on CRAN:
install.packages("dockr")
When you work on an R project, it is often desirable to organize the code in the
R package structure. dockr
facilitates easy creation of a Docker container
image that mirrors your current R session and includes all of the R dependencies
needed to run your R package.
First, load the dockr
package.
library(dockr)
options(repos = c(CRAN = "https://cran.rstudio.com/"))
In order do create the files, that constitute the Docker image, simply invoke
the prepare_docker_image()
function and point to the folder with your package.
The workflow of prepare_docker_image()
is summarized below:
Now, I will let dockr
do its magic and create the files for a Docker image
container, in which dockr
is installed together with all of the R package
dependencies, dockr
needs to run.
Beware that the files are created as side-effects of the function call.
image_dockr <- prepare_docker_image(".")
Great, all necessary files for the Docker image have been created, and you can build the Docker image right away by following the instructions. It is as easy as that! Yeah!
Let us just take a quick look into the folder with the files for the Docker
image to see the works of dockr
.
list.files(image_dockr$paths$dir_image)
It contains a Dockerfile and a folder named 'source_packages'.
The resulting Dockerfile can be printed with the print_file()
function, that
comes with dockr
:
print_file(image_dockr$paths$path_Dockerfile)
As you see, the versions of the R packages, that will be installed in the Docker container image, are all given explicitly. They will mirror the versions of the dependencies, that are in fact loaded or installed on your system. In this way, the Docker container image seeks to reflect your current R session as close as possible and by doing so create an environment, where you will be able to reproduce results from your current R session.
Also note, that CRAN R packages will be installed from relevant
MRAN snapshots - using the
remotes::install_version()
function.
The 'source_packages' folder contains the local (non-CRAN) packages, that have
to be installed in the Docker container image in order for dockr
to run.
Since dockr
does not depend on any local (non-CRAN) packages,
source_packages
only contains a source package version of dockr
itself,
i.e.:
list.files(image_dockr$paths$dir_source_packages)
If there is need for adding additional lines to/editing the Dockerfile (e.g.
if you have to install any non-R dependencies, this can
be achieved with the write_lines_to_file()
function. write_lines_to_file()
enables you to add new lines to the beginning or the end of the Dockerfile.
Let us try it out and write a couple of additional lines to the Dockerfile.
# write three lines to beginning of file. write_lines_to_file(c("# set maintainer", "MAINTAINER Lars KJELDGAARD <lars_kjeldgaard@hotmail.com>", ""), image_dockr$paths$path_Dockerfile, prepend = TRUE, print_file = FALSE) # write lines to the end of the file. write_lines_to_file(c("# check out smaakage85.netlify.com >:-]~~"), image_dockr$paths$path_Dockerfile, prepend = FALSE, print_file = FALSE)
Take a look at the resulting Dockerfile.
print_file(image_dockr$paths$path_Dockerfile)
If your package depends on local non-CRAN R packages, dockr
will also include
these packages in the Docker container image. Local non-CRAN R packages must be
available as source packages ([packageName]_[packageVersion].tar.gz) in one
or more user specified local directories. These paths have to be specified in the
'dir_src' argument, when invoking the prepare_docker_image()
, e.g.:
# image for my package 'recorder'. image_recorder <- prepare_docker_image("~/recorder", dir_src = "~/src")
dockr
does not deal with any non-R dependencies what so ever at this point.
In case that, for instance, your package has any Linux specific dependencies,
you will have to install them yourself in the Docker container image.
I hope, that you will find dockr
useful.
Please direct any questions and feedbacks to me!
If you want to contribute, open a PR.
If you encounter a bug or want to suggest an enhancement, please open an issue.
Best, smaakagen
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.