forked from fhdsl/Containers_for_Scientists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-writing-dockerfiles.qmd
83 lines (55 loc) · 4.21 KB
/
06-writing-dockerfiles.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Writing Dockerfiles
```{r, out.width = "100%", echo = FALSE}
ottrpal::include_slide("https://docs.google.com/presentation/d/1T5Lfei2UVou9b0qaUCrWXmkcIwAao-UcN4pHMPEE4CY/edit#slide=id.g30a80783034_1_34")
```
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](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/).
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.
## 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!
## 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.
- If you know you want use `R` on your container then take a look at [the `rocker` images](https://hub.docker.com/u/rocker).
- If you know you want to use Jupyter notebooks on your container, go to the [Jupyter Project images](https://hub.docker.com/u/jupyter).
- If you are doing anything with bioinformatics software, [take a look at Biocontainers](https://biocontainers.pro/).
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.
### More learning
For more about Dockerfiles go to [Docker's documentation tutorials](https://docs.docker.com/get-started/docker-concepts/building-images/writing-a-dockerfile/)