Last updated: 2019-03-19

Checks: 2 0

Knit directory: for-future-reference/

This reproducible R Markdown analysis was created with workflowr (version 1.2.0.9000). The Report tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
html 8ab13ac John Blischak 2019-02-06 Build site.
Rmd 440a5eb John Blischak 2019-02-06 Add sagemath Docker container and how to mount local directory.
html 84cd2a1 John Blischak 2019-01-28 Build site.
Rmd 86f5c3d John Blischak 2019-01-28 Add notes on using Docker.

Docker is virtualization software. The official documentation is at docs.docker.com.

Terminology:

Getting help:

docker --help
docker <subcommand> --help

Using Docker containers

The name of an image is referred to as a repository. If the repository is from a remote source like DockerHub, it will also have an associated username. Lastly, each version of the image has a specific tag (the default is latest). Thus in the commands below, any instance of <image> can be replaced with the corresponding (username/)repository(:tag) that specifies the desired image.

docker pull <image>
docker pull continuumio/miniconda3
docker pull continuumio/miniconda3:4.5.11
docker image ls
# Alternative:
docker images
docker run --rm <image> <command>
docker container ls
# Alternative:
docker ps
docker stop <container>

You can reference the container by its ID or Name (you have to run docker container ls to obtain this information).

docker run --rm -it <image>
# Depending on the Dockerfile, you may have to specify the executable to run
docker run --rm -it <image> bash

Building a Docker image

docker build -t <name-of-tag> .
# Remove exited containers:
docker ps -aq --no-trunc -f status=exited | xargs docker rm
# Remove untagged images:
docker images -q --filter dangling=true | xargs docker rmi
# Start with the xenial tag of the ubuntu image
FROM ubuntu:xenial

# Add contact info
MAINTAINER <name>, <email>

# Install software
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    emacs \
    git \
    nano

# Execute code
RUN mkdir /root/project

# Set the working directory
WORKDIR /root/project

# Copy file from local machine to Docker image
COPY file.txt /root/project/

# Copy directory from local machine to Docker image
COPY dir/ /root/project/dir/

# Run this command by default when running an interactive container
CMD [ "/bin/bash" ]

Mounting a local directory in a Docker container

A Docker container can be very useful for running software that requires a complex installation. However, in order for this useful, it needs to be able to read and write from the local host machine. To mount a local directory, use the flag -v.

docker run -it -v ~/Desktop/project:/project ubuntu bash

Note that it is critical that both the local and container paths are absolute paths. Docker will throw an error if the container path is not absolute. However, if the local path is relative, instead of mounting that directory, it will create a new volume and save the files there (use docker volume list to view and docker volume rm <name> to delete).

For Docker containers that are run as root, the newly created files have root permissions. This is annoying, but the only workaround I have found is building your own Docker image that adds a layer creating a local user.

For Docker containers that are run as non-root, you should be able to read and write both in the container and from the local machine with no issues.

Resources:

Useful images

Launch Jupyter from a Docker container (see my comment on GitHub for details):

docker run -i -t -p 8888:8888 continuumio/miniconda3 /bin/bash -c "/opt/conda/bin/conda install -c conda-forge jupyter -y --quiet && mkdir /opt/notebooks && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='0.0.0.0' --port=8888 --no-browser --allow-root"
docker run -p 8888:8888 sagemath/sagemath:latest sage-jupyter

The URL that sage-jupyter returns is rough, e.g. http://0efce28e9a34:8888/?token=791b95e58b652231dc507bb4fd9f2af7ea65b62d2eee4d7b&token=791b95e58b652231dc507bb4fd9f2af7ea65b62d2eee4d7b. First, you have to update the address to be http://localhost:8888. Second, it repeats the same token twice. This still works though.

Note that sage-jupyter is a variable that is controlled by entrypoint.sh. It executes sage -n jupyter --no-browser --ip='0.0.0.0' --port=8888.

Tutorials