dockerfiler is now on CRAN

[This article was first published on Colin Fay, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

{dockerfiler} is a package that comes with a simple, object oriented API for Dockerfile creation, straight from your R session. Now on CRAN.

Installation

You can install {dockerfiler} from GitHub with:

<span class="c1"># install.packages("remotes")</span><span class="w">
</span><span class="n">remotes</span><span class="o">::</span><span class="n">install_github</span><span class="p">(</span><span class="s2">"colinfay/dockerfiler"</span><span class="p">)</span><span class="w">
</span>

Or from CRAN with :

<span class="n">install.packages</span><span class="p">(</span><span class="s2">"dockerfiler"</span><span class="p">)</span><span class="w">
</span>

Basic worflow

By default, Dockerfiles are created with FROM "rocker/r-base". You can
set another FROM in new()

<span class="n">library</span><span class="p">(</span><span class="n">dockerfiler</span><span class="p">)</span><span class="w">
</span><span class="c1"># Create a dockerfile template</span><span class="w">
</span><span class="n">my_dock</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">Dockerfile</span><span class="o">$</span><span class="n">new</span><span class="p">()</span><span class="w">
</span><span class="n">my_dock</span><span class="o">$</span><span class="n">MAINTAINER</span><span class="p">(</span><span class="s2">"Colin FAY"</span><span class="p">,</span><span class="w"> </span><span class="s2">"[email protected]"</span><span class="p">)</span><span class="w">
</span>

Wrap your raw R Code inside the r() function to turn it into a bash
command with R
-e
.

<span class="n">my_dock</span><span class="o">$</span><span class="n">RUN</span><span class="p">(</span><span class="n">r</span><span class="p">(</span><span class="n">install.packages</span><span class="p">(</span><span class="s2">"attempt"</span><span class="p">,</span><span class="w"> </span><span class="n">repo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"http://cran.irsn.fr/"</span><span class="p">)))</span><span class="w">
</span>

Classical Docker stuffs:

<span class="n">my_dock</span><span class="o">$</span><span class="n">RUN</span><span class="p">(</span><span class="s2">"mkdir /usr/scripts"</span><span class="p">)</span><span class="w">
</span><span class="n">my_dock</span><span class="o">$</span><span class="n">RUN</span><span class="p">(</span><span class="s2">"cd /usr/scripts"</span><span class="p">)</span><span class="w">
</span><span class="n">my_dock</span><span class="o">$</span><span class="n">COPY</span><span class="p">(</span><span class="s2">"plumberfile.R"</span><span class="p">,</span><span class="w"> </span><span class="s2">"/usr/scripts/plumber.R"</span><span class="p">)</span><span class="w">
</span><span class="n">my_dock</span><span class="o">$</span><span class="n">COPY</span><span class="p">(</span><span class="s2">"torun.R"</span><span class="p">,</span><span class="w"> </span><span class="s2">"/usr/scripts/torun.R"</span><span class="p">)</span><span class="w">
</span><span class="n">my_dock</span><span class="o">$</span><span class="n">EXPOSE</span><span class="p">(</span><span class="m">8000</span><span class="p">)</span><span class="w">
</span><span class="n">my_dock</span><span class="o">$</span><span class="n">CMD</span><span class="p">(</span><span class="s2">"Rscript /usr/scripts/torun.R "</span><span class="p">)</span><span class="w">
</span>

See your Dockerfile :

<span class="n">my_dock</span><span class="w">
</span>
## FROM rocker/r-base
## MAINTAINER Colin FAY <[email protected]>
## RUN R -e 'install.packages("attempt", repo = "http://cran.irsn.fr/")'
## RUN mkdir /usr/scripts
## RUN cd /usr/scripts
## COPY plumberfile.R /usr/scripts/plumber.R
## COPY torun.R /usr/scripts/torun.R
## EXPOSE 8000
## CMD Rscript /usr/scripts/torun.R

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 :

<span class="c1"># Switch line 8 and 7</span><span class="w">
</span><span class="n">my_dock</span><span class="o">$</span><span class="n">switch_cmd</span><span class="p">(</span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">7</span><span class="p">)</span><span class="w">
</span><span class="n">my_dock</span><span class="w">
</span>
## FROM rocker/r-base
## MAINTAINER Colin FAY <[email protected]>
## RUN R -e 'install.packages("attempt", repo = "http://cran.irsn.fr/")'
## RUN mkdir /usr/scripts
## RUN cd /usr/scripts
## COPY plumberfile.R /usr/scripts/plumber.R
## EXPOSE 8000
## COPY torun.R /usr/scripts/torun.R
## CMD Rscript /usr/scripts/torun.R

You can also remove a cmd with remove_cmd:

<span class="n">my_dock</span><span class="o">$</span><span class="n">remove_cmd</span><span class="p">(</span><span class="m">8</span><span class="p">)</span><span class="w">
</span><span class="n">my_dock</span><span class="w">
</span>
## FROM rocker/r-base
## MAINTAINER Colin FAY <[email protected]>
## RUN R -e 'install.packages("attempt", repo = "http://cran.irsn.fr/")'
## RUN mkdir /usr/scripts
## RUN cd /usr/scripts
## COPY plumberfile.R /usr/scripts/plumber.R
## EXPOSE 8000
## CMD Rscript /usr/scripts/torun.R

This also works with a vector:

<span class="n">my_dock</span><span class="o">$</span><span class="n">remove_cmd</span><span class="p">(</span><span class="m">5</span><span class="o">:</span><span class="m">7</span><span class="p">)</span><span class="w">
</span><span class="n">my_dock</span><span class="w">
</span>
## FROM rocker/r-base
## MAINTAINER Colin FAY <[email protected]>
## RUN R -e 'install.packages("attempt", repo = "http://cran.irsn.fr/")'
## RUN mkdir /usr/scripts
## CMD Rscript /usr/scripts/torun.R

Save your Dockerfile:

<span class="n">my_dock</span><span class="o">$</span><span class="n">write</span><span class="p">()</span><span class="w">
</span>

Contact

Questions and feedbacks welcome!

You want to contribute ? Open a
PR 🙂 If you encounter
a bug or want to suggest an enhancement, please open an
issue
.

To leave a comment for the author, please follow the link and comment on their blog: Colin Fay.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)