You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/lectures/110-containerization-2.qmd
+1-104Lines changed: 1 addition & 104 deletions
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ title: Using and running containers
9
9
10
10
## Launching containers using Docker at the command line
11
11
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.
13
13
14
14
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:
15
15
@@ -166,35 +166,6 @@ Another important note is that the container port is specific to the container,
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
-
198
169
### Changing the containers default command
199
170
200
171
When we launch containers, they execute the command at runtime
@@ -299,8 +270,6 @@ docker run \
299
270
make all
300
271
```
301
272
302
-
Note: If you are on a M1/M2 Mac, don't forget to include `--platform=linux/amd64`in your run command.
303
-
304
273
## Docker command line commands
305
274
306
275
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
327
296
| `-v` | Mounts a volume of your computer to the Docker container |
328
297
| `-p` | Specifies the ports to map a web app to |
329
298
| `-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:
0 commit comments