6  Writing Dockerfiles

Now that you’re familiar with the basics of Dockerfiles and how to use them to build images, let’s dive into some more of the things you can do with them.

FROM is one of the main commands that a Dockerfile can take as described by their documentation.

Now you are also familiar with CMD which runs something when the container is built

FROM creates a layer from the another Docker image. CMD specifies what command to run within the container. RUN builds your application with make. COPY adds files from your Docker client’s current directory.

Next let’s use RUN to add a package to our image.

6.1 Templates for adding packages!

Starting off with your example Dockerfile, we will practice adding another package and re-build the docker image with a new package.

Note that spacing is important as well as having a \ at the end of each line if the command is continuing.

To add R packages from CRAN, you can use this kind of format:

RUN Rscript -e  "install.packages( \
    c('BiocManager', \
      'R.utils', \
      'newpackagename'))"

To add an R package from Bioconductor, you can follow this kind of format:

RUN Rscript -e "options(warn = 2); BiocManager::install( \
  c('limma', \
    'newpackagename')

To add a Python package using pip, you will need to add pip3 to install Python packages using this format. But first you’ll need to make sure you have pip installed using:

Install pip:

RUN apt-get update && apt-get install -y --no-install-recommends \
    python3-pip

Then you can use pip install to install packages

RUN pip3 install \
    "somepackage==0.1.0"

There are so many things you can add to your Docker image. (Picture whatever software and packages you are using on your computer). We can only get you started for the feel of how to build a Dockerfile, and what you put on your Docker image will be up to you.

To figure out how to add something, a good strategy is to look for other Dockerfiles that might have the package you want installed and borrow their RUN command. Then try to re-build your Docker image with that added RUN command and see if it builds successfully.

Make sure that whatever changes you make to your Dockerfile, that you add version control it and add it to your GitHub repository!

6.2 Troubleshooting tips for building images

  1. Look for a good base image to start with on your FROM Something that has a lot of what you need but not more software packages than you need.
  2. When adding packages, look for other Dockerfiles folks have written that have the same operating system aka usually Ubuntu, and copy their installation steps.
  3. Use version numbers so if you rebuild the same versions will be installed and that won’t be a moving target for you.
  4. Should the installation steps fail, try to pinpoint what is the first part it is failing on. Look for if there’s a message like “missing dependency” or something similar. It may mean you need to add another package in there before installing this package.
  5. Google your error messages. Look on StackOverflow. Post on StackOverflow.
  6. If all else fails, can you just install a different software or a different version number of that software that can do the same functionality?
  7. If you change something in a base image or in a file that is copied over you may need to use --no-cache so that everything really gets rebuilt from scratch.

6.2.1 More learning

For more about Dockerfiles go to Docker’s documentation tutorials