Skip to content

Instantly share code, notes, and snippets.

@davidtedfordholt
Created April 24, 2020 16:55
Show Gist options
  • Save davidtedfordholt/06b5ffa6862f59e98dac0a6021a24584 to your computer and use it in GitHub Desktop.
Save davidtedfordholt/06b5ffa6862f59e98dac0a6021a24584 to your computer and use it in GitHub Desktop.
Simple Dockerfile for R
## Base image
FROM rocker/r-base
# Copy R scripts to container
COPY / ./
# Execute when container is BUILT:
# sudo docker build -t example_container .
RUN Rscript setup.R
# Execute when container is RUN:
# sudo docker run --rm example_container
CMD Rscript run.R
print("I built a container!")
print("I am building a container.")
@davidtedfordholt
Copy link
Author

This Dockerfile will execute setup.R during the building of the image and run.R during the running of the container.

Each time you run the container, it will start from a state of having already executed everything above the CMD line. This means that, if you had defined objects or variables in the container's environment in earlier lines, they are available in the container's environment. This includes system variables and files created.

Remember, however, that the R environment of the script in the CMD line is not the same as the R environment of the script in the RUN line.

@davidtedfordholt
Copy link
Author

davidtedfordholt commented Apr 24, 2020

My first swag was just

FROM rocker/r-base
RUN R -e 'print("I am building a container.")'
CMD R -e 'print("I built a container!")

but that seems a bit hard to extrapolate from. :)

@davidtedfordholt
Copy link
Author

Or, for my very favorite @jtbaker:
echo -e "FROM rocker/r-base\n COPY / ./\n CMD Rscript run.R" >> Dockerfile && echo "print('I love you, Jason')" >> run.R && docker build -t ex . && docker run --rm ex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment