Chapter 12 Docker Images
This document covers the using docker image
to create custom images that you can store on Docker Hub and install on your EC2 Server. By the end of this, you will be able to build your own custom docker images using a DockerFile.
12.1 Prerequisites
Use docker container ls
and visit the EC2 Server’s port 8787 to confirm that RStudio server is running with linked volumes to the /home/ubuntu/rstudio_docker
.
If a container is NOT running RStudio, use this command to setup the RStudio Server with linked volumes using Docker:
12.2 Definitions
Dockerfile - A text document that contains all the commands a user could call on the command line to assemble an image.
Building an Image - Using
docker build
, users can create an automated build that executes several command-line instructions in succession that are recorded in a Dockerfile.
12.3 Example - Adding shinyWidgets to an Image
12.3.1 Making a Dockerfile
Make the following text file in your RStudio IDE.
# File: Dockerfile
FROM rocker/shiny-verse:latest
RUN apt-get update -qq \
&& apt-get -y --no-install-recommends install \
lbzip2 \
&& install2.r --error --deps TRUE \
shinyWidgets
Here’s what is happening in the Dockerfile:
FROM rocker/shiny-verse:latest
- Usesshiny-verse
as the starting point for the image.RUN apt-get update -qq
- Updates the installation software&& apt-get -y --no-install-recommends install
- Installs Linux libraries that R Packages depend on (e.g.lbzip2
is a linux library)&& install2.r --error --deps TRUE
- Installs R libaries from CRAN. This is where we list the R packages we want.
12.3.2 Saving a Dockerfile
Save the file as rstudio_docker/Dockerfile
.
12.3.3 Docker Build
The docker build
command is how to build docker images that extend current images using software that you need for your applications to run.
Navigate into the rstudio_docker
folder. Run the following command:
Here’s what is happening in the Dockerfile:
docker build
- Used to build an image from a Dockerfile (can also useDocker image build
).
- The directory that contains the Dockerfile named “Dockerfile”. If your dockerfile is located somewhere else or named differently than Dockerfile, use tab completion to locate it (i.e.docker build path/to/your/file/dockerfile_name
)-t
- Adds an image name and tag.- name - The name of the image. The name we chose is
shiny-widgets
. - tag - The version of the image.
latest
is used by default.
- name - The name of the image. The name we chose is
12.3.4 Docker Image List (Local)
The image is now stored locally on our EC2 Server. We can verify this using sudo docker image ls
to list the images. We can now use this image to build containers that
12.3.5 Docker Hub
If we’d like to be able to share the image, we can push the image to Docker Hub.
12.3.5.1 List the Images
sudo docker image ls
12.3.5.2 Prep for upload with tag command
sudo docker tag [IMAGE ID] your_dockerhub_user_name/shiny-widgets:latest
- Replace the Image ID with the correct Image ID and your docker hub User Name.sudo docker image ls
- Make sure the repository now matches your user name.
12.3.5.3 Login to Docker Hub
sudo docker login
- Provide your Docker Hub User Name and Password.
12.3.5.4 Push to Docker Hub
sudo docker push your_docker_hub_user_name/shiny-widgets:latest
- Pushes the Dockerfile to DockerHub.
12.3.5.5 Check Docker Hub
Congrats. Your image is now available on Docker Hub. You can share it with others or use it on other EC2 Servers that you create.
12.4 shinyauth - Complex Dockerfile & Image for Stock Analyzer
The shiny-widgets
image that we created is much too simplistic for our Stock Analyzer application. In addition to shinywidgets
, we need:
- CRAN Libraries:
shinythemes
,shinyjs
,mongolite
,jsonlite
,config
,remotes
,tidyquant
, andplotly
. - GitHub Libraries:
business-science/shinyauthr
To make this image, I created mdancho/shinyauth
- A Docker Image that contains the necessary R Packages and Linux Dependencies that we need.
12.4.1 Dockerfile
The Dockerfile looks like this:
FROM rocker/shiny-verse:latest
RUN apt-get update -qq \
&& apt-get -y --no-install-recommends install \
lbzip2 \
libfftw3-dev \
libgdal-dev \
libgeos-dev \
libgsl0-dev \
libgl1-mesa-dev \
libglu1-mesa-dev \
libhdf4-alt-dev \
libhdf5-dev \
libjq-dev \
liblwgeom-dev \
libpq-dev \
libproj-dev \
libprotobuf-dev \
libnetcdf-dev \
libsqlite3-dev \
libssl-dev \
libudunits2-dev \
netcdf-bin \
postgis \
protobuf-compiler \
sqlite3 \
tk-dev \
unixodbc-dev \
libsasl2-dev \
libv8-dev \
libsodium-dev \
&& install2.r --error --deps TRUE \
shinyWidgets \
shinythemes \
shinyjs \
mongolite \
jsonlite \
config \
remotes \
tidyquant \
plotly \
&& installGithub.r business-science/shinyauthr
12.4.2 Docker Hub Image
You can find the shinyauth
image several ways.
Docker Hub
Docker Search
sudo docker search mdancho
- Searches for any public images that “mdancho” has pushed to Docker Hub.
12.4.3 Docker pull to install shinyauth
sudo docker pull mdancho/shinyauth
- Will pull
the shinyauth:latest
image onto your EC2 Server.
12.4.4 Docker image list
sudo docker image ls
- List the images. Verify that you’ve installed the shinyauth
image.
12.5 Wrapup
- You now are able to
build
your own docker images. Simply follow the Shiny Widgets Example. - You know the basic structure of a
Dockerfile
. Simply modify as necessary to build more complex docker images.
Have a question? Leave a comment.