Skip to content

Commit 50edfe5

Browse files
committed
refactor docker-compose into separate chapter
1 parent f1efa54 commit 50edfe5

File tree

3 files changed

+77
-104
lines changed

3 files changed

+77
-104
lines changed

book/_quarto.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ book:
3232
- lectures/090-conda-lock.qmd
3333
- lectures/100-containerization-1.qmd
3434
- lectures/110-containerization-2.qmd
35+
- lectures/115-docker-compose.qmd
3536
- lectures/120-containerization-3.qmd
3637
- part: parts/data-validation.qmd
3738
chapters:

book/lectures/110-containerization-2.qmd

Lines changed: 1 addition & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ title: Using and running containers
99

1010
## Launching containers using Docker at the command line
1111

12-
Docker has many ways we can run containers (GUI, command line, Docker compose configuration files). Here we will learn how to use the command line interface for running container instances from container images. We will then build on this knowledge to move to using Docker compose configuration files for increased efficiency. We are opting to use command line and configuaration file tools so that we can automate and scale the running of containers. It is also more reproducible.
12+
Docker has many ways we can run containers (GUI, command line, Docker compose configuration files). Here we will learn how to use the command line interface for running container instances from container images. We will then build on this knowledge to move to using Docker compose configuration files for increased efficiency. We are opting to use command line and configuration file tools so that we can automate and scale the running of containers. It is also more reproducible.
1313

1414
Below we demonstrate how to launch and run containers using the [`continuumio/miniconda3` image](https://hub.docker.com/r/continuumio/miniconda3) as an example. We will slowly walk through the 4 basic steps need to run a container:
1515

@@ -166,35 +166,6 @@ Another important note is that the container port is specific to the container,
166166
167167
*Source: <https://hub.docker.com/r/rocker/rstudio>*
168168
169-
### Specifying the image architecture/platform
170-
171-
Newer M1 and M2 Macs use a new processor chip, called ARM,
172-
that is a different architecture compared to the previous
173-
Macs, and current Windows and Linux machines (which use Intel Processors).
174-
Given that containerization software virtualizes
175-
at the level of the operating system user space,
176-
these different architectures lead to building containers with different architectures.
177-
178-
Also given that Newer M1 and M2 Macs are still the minority of computers
179-
in use, it is a better practice to work with container architectures that
180-
work for the majority of in use computers, which are those that have Intel Processors.
181-
To tell Docker to do this,
182-
we add the `--platform=linux/amd64` argument to our Docker `run` and `build`
183-
commands.
184-
185-
To make this process even smoother and less error prone,
186-
we should also set our Docker Desktop
187-
to use Rosetta 2 x86/AMD64 emulation on M1/M2 Macs .
188-
To use this, you must:
189-
- make sure Rosetta 2 is installed on your Mac (instructions to install it [here](https://support.apple.com/en-ca/HT211861))
190-
- Select "Use Virtualization framework" and "Use Rosetta for x86/amd64 emulation on Apple Silicon" in the General settings tab of Docker Desktop.
191-
192-
:::{.callout-note}
193-
- In computer science, emulation works to let you run run software and execute programs originally designed one computer system on another computer system. Emulation is similar to virtualization in concept, but differs from it in that it focuses on enabling software designed for entirely different architectures to be executed.
194-
- You must also be using macOS Ventura or later to use this feature.
195-
- You will still need to use the `--platform linux/amd64` command when building or running images even when using Rosetta 2 emulation, because your computer can run and build both `linux/arm64` and `linux/amd64` images. So you have to be clear which architecture you want to work with.
196-
:::
197-
198169
### Changing the containers default command
199170
200171
When we launch containers, they execute the command at runtime
@@ -299,8 +270,6 @@ docker run \
299270
make all
300271
```
301272
302-
Note: If you are on a M1/M2 Mac, don't forget to include `--platform=linux/amd64` in your run command.
303-
304273
## Docker command line commands
305274
306275
The table below summarizes the Docker commands we have learned so far and can serve as a useful reference when we are using Docker:
@@ -327,75 +296,3 @@ The table below summarizes the Docker commands we have learned so far and can se
327296
| `-v` | Mounts a volume of your computer to the Docker container |
328297
| `-p` | Specifies the ports to map a web app to |
329298
| `-e` | Sets environment variables in the container (*e.g.*, PASSWORD="apassword") |
330-
| `--platform` | Specifies the image architecture, commonly used on M1/M2 Macs to set it to `linux/amd64` |
331-
332-
## Docker compose to launch containers
333-
334-
It can be fiddly and error prone to type long commands into the terminal,
335-
or a GUI every time you want to launch a container.
336-
A better approach is to use Docker compose
337-
to specify how you want to launch the container.
338-
339-
Docker compose uses a YAML file,
340-
specifically named `docker-compose.yml`,
341-
to record how the container should be launched.
342-
This file can include details including:
343-
- the docker image and version to use
344-
- how to mount volumes
345-
- what ports to map
346-
- what environment variables to set.
347-
348-
Here is an example of a `docker-compose.yml` file
349-
for use with the `rocker/rstudio` container image:
350-
351-
```yaml
352-
services:
353-
analysis-env:
354-
image: rocker/rstudio:4.4.2
355-
ports:
356-
- "8787:8787"
357-
volumes:
358-
- .:/home/rstudio/project
359-
environment:
360-
PASSWORD: password
361-
```
362-
363-
To launch the container interactively using this file,
364-
you would type the `docker-compose` command shown below.
365-
366-
```bash
367-
docker-compose up
368-
```
369-
370-
If you are using a web app, as in the case of the
371-
`rocker/rstudio` or `jupyter/minimal-notebook` container images,
372-
you still need to manually navigate to the web app in your browser
373-
and enter the correct URL to access it.
374-
375-
To stop and clean up the container, you would type `Cntrl + C` in the terminal where you launched the container, and then type
376-
377-
```bash
378-
docker-compose rm
379-
```
380-
381-
Let's take a look at an example `docker-compose.yml` being used in a project:
382-
- <https://github.com/ttimbers/breast_cancer_predictor_py>
383-
384-
## Running a Docker container non-interactively using the Docker Compose
385-
386-
We can also use Docker Compose to run containers non-interactively!
387-
We can do this by specifying that we want to `run` the container
388-
(instead of `up` to launch in interactively).
389-
We use the `--rm` flag with the `run` command to make the container ephemeral
390-
(delete it upon exit).
391-
Then we specify the name of the service from the `docker-compose.yml` file
392-
that we want to run
393-
(in our `docker-compose.yml` files so far we only have one service,
394-
the environment for running the analysis).
395-
And finally we add the command we want to run non-interactively
396-
using the container
397-
(in the example below we use `make` to run the data analysis pipeline script).
398-
399-
```bash
400-
docker-compose run --rm analysis-env make all
401-
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: docker-compose
3+
---
4+
5+
6+
## Docker compose to launch containers
7+
8+
It can be fiddly and error prone to type long commands into the terminal,
9+
or a GUI every time you want to launch a container.
10+
A better approach is to use Docker compose
11+
to specify how you want to launch the container.
12+
13+
Docker compose uses a YAML file,
14+
specifically named `docker-compose.yml`,
15+
to record how the container should be launched.
16+
This file can include details including:
17+
- the docker image and version to use
18+
- how to mount volumes
19+
- what ports to map
20+
- what environment variables to set.
21+
22+
Here is an example of a `docker-compose.yml` file
23+
for use with the `rocker/rstudio` container image:
24+
25+
```yaml
26+
services:
27+
analysis-env:
28+
image: rocker/rstudio:4.4.2
29+
ports:
30+
- "8787:8787"
31+
volumes:
32+
- .:/home/rstudio/project
33+
environment:
34+
PASSWORD: password
35+
```
36+
37+
To launch the container interactively using this file,
38+
you would type the `docker-compose` command shown below.
39+
40+
```bash
41+
docker-compose up
42+
```
43+
44+
If you are using a web app, as in the case of the
45+
`rocker/rstudio` or `jupyter/minimal-notebook` container images,
46+
you still need to manually navigate to the web app in your browser
47+
and enter the correct URL to access it.
48+
49+
To stop and clean up the container, you would type `Cntrl + C` in the terminal where you launched the container, and then type
50+
51+
```bash
52+
docker-compose rm
53+
```
54+
55+
Let's take a look at an example `docker-compose.yml` being used in a project:
56+
- <https://github.com/ttimbers/breast_cancer_predictor_py>
57+
58+
## Running a Docker container non-interactively using the Docker Compose
59+
60+
We can also use Docker Compose to run containers non-interactively!
61+
We can do this by specifying that we want to `run` the container
62+
(instead of `up` to launch in interactively).
63+
We use the `--rm` flag with the `run` command to make the container ephemeral
64+
(delete it upon exit).
65+
Then we specify the name of the service from the `docker-compose.yml` file
66+
that we want to run
67+
(in our `docker-compose.yml` files so far we only have one service,
68+
the environment for running the analysis).
69+
And finally we add the command we want to run non-interactively
70+
using the container
71+
(in the example below we use `make` to run the data analysis pipeline script).
72+
73+
```bash
74+
docker-compose run --rm analysis-env make all
75+
```

0 commit comments

Comments
 (0)