Docker, Dockerfile
For this blog post, I plan on discussing the following topics:
- What is Docker and why it should be used
- Writing a Dockerfile for a R Shiny application
- What I will be working on next
- References
Before discussing the first topic, I just want to mention that the this website that I used for the Dockerfile, is a bit outdated because I could not get the Docker container working at all. I got like an error saying “No such file or directory” for the server.sh
What is Docker and why it should be used
Let’s say you and your friend are working and sharing R code. However, when you run your friend’s code, you don’t get the same outputs as he/she does and vice versa.
Now, the reasons that this could be happening is that you and your friend could have different operating system(Windows, Macos, Linux, etc.) different R version, different R packages, etc.
Docker is essentially trying to solve those type of issues.
Docker Container
A Docker container can be thought of as a computer(Ex: virtual machine) inside of your computer. What makes a docker container so great is that you can send the docker container to your friend and when your friend runs the docker container, he/she will get the same results as you did on your local machine.
Overall, you would want to use Docker because it allows you to resolve dependencies issues from the beginning from the operating system up to the R packages and Docker make sure that your code’s output is the same.
Other things why Docker is good:
- Portability - can send Docker container easilty to different machines
- Sharability - can send the Docker container to anyone
Writing a Dockerfile for a R shiny application
Dockerfiles are a set of instrunctions on how to add things to the base image. Dockerfiles build custom images up in a series of layers just like a cake.
Now in order to create a Dockerfile, create a text file in R studio called Dockerfile
.
The Dockerfile should contain something like this:
# get shiny serves plus tidyverse packages image
FROM rocker/shiny:4.0.5
# system libraries of general use
RUN apt-get update && apt-get install -y \
make \
zlib1g-dev\
libcurl4-openssl-dev \
libssl-dev
# install R packages required
# (change it dependeing on the packages you need)
RUN R -e "install.packages('shiny', repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('shinydashboard', repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('DT', repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('plotly', repos='http://cran.rstudio.com/')"
# copy the app to the image
COPY ui.R /srv/shiny-server/
COPY server.R /srv/shiny-server/
# run app
CMD ["/usr/bin/shiny-server"]
The line with FROM
tells Docker to start with the rocker/shiny base image. The FROM
command must always be the the first thing in the Dockerfile.
In addition, we are using the rocker project’s base shiny image because we do not need to make our own image. This image contains the Shiny Server.
I decided not to use the “latest” version of the image because we can’t guarantee the many version of R and its dependencies if we used “latest”.
The RUN apt-get update && apt-get install -y
will update and install all our system dependncies.
The next RUN
commands will install the necessary R packages needed for the R shiny application.
The COPY
copies the contents of the R scripts into the correct location inside of our container image.
The CMD
determines what command should be run by default when a container is started from the image.
For more information about why I did the Dockerfile setup this way, take a look at the second and third link under references.
On a final note, docker image is the setup of the virtual computer. If you run the docker image, you will get the instance of an image, which is called a docker container.
What I will be working on tomorrow
- Reading more about Docker images and containers until I have a better understanding
- Get the pull request option for github actions to work and write documentation for that
- Need to figure out when I do git push in R, it is using my other github account
- Find a data that I want to use
- Read more about Shiny Server