Skip to content

Commit a7ed523

Browse files
Merge pull request #129 from UoMResearchIT/missed_fixes
Missed fixes
2 parents 7d2e9b2 + a7c615b commit a7ed523

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

episodes/docker-compose.Rmd

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ The first thing we need to do is create a `compose.yml` file.
4343
All `compose.yml` files start with `services:`.
4444
This is the root element under which we define the services we want to run.
4545
```yml
46+
# compose.yml
4647
services:
4748
```
4849
4950
Next, let's add the service for the SPUC container.
5051
We'll call it `spuc` and we will tell it what `image` to use.
5152
```yml
53+
# compose.yml
5254
services:
5355
spuc: # The name of the service
5456
image: spuacv/spuc:latest # The image to use
@@ -253,6 +255,7 @@ If you just want to stop it, you can use `[Ctrl+C]` like we did before.
253255
The next item in our list is the name of the container.
254256
We can name the container using the `container_name` key.
255257
```yml
258+
# compose.yml
256259
services:
257260
spuc:
258261
image: spuacv/spuc:latest
@@ -298,6 +301,7 @@ It's worth noting the `ports` key is a list, so we can map multiple ports if we
298301
and that the host and container ports don't have to be the same!
299302

300303
```yml
304+
# compose.yml
301305
services:
302306
spuc:
303307
image: spuacv/spuc:latest
@@ -329,6 +333,7 @@ We will use a bind mount for this - mapping a file from the host to the containe
329333

330334
As with the CLI, this is (confusingly) done using the `volumes` key.
331335
```yml
336+
# compose.yml
332337
services:
333338
spuc:
334339
image: spuacv/spuc:latest
@@ -357,6 +362,7 @@ Otherwise, it generates a volume.
357362

358363
Let's add a volume to persist the unicorn sightings between runs of the container.
359364
```yml
365+
# compose.yml
360366
services:
361367
spuc:
362368
image: spuacv/spuc:latest
@@ -382,6 +388,7 @@ The volumes are separate from *services*, so they are declared at the same level
382388
To declare a named volume, we specify its name and end with a `:`.
383389
We will do this at the end of the file.
384390
```yml
391+
# compose.yml
385392
services:
386393
spuc:
387394
image: spuacv/spuc:latest
@@ -436,6 +443,7 @@ Next, we need to set the `EXPORT` environment variable to `true`.
436443
This is done using the `environment` key.
437444

438445
```yml
446+
# compose.yml
439447
services:
440448
spuc:
441449
image: spuacv/spuc:latest
@@ -472,6 +480,7 @@ Finally, lets set the units by overriding the command, as we did before.
472480
For this, we use the `command` key.
473481

474482
```yml
483+
# compose.yml
475484
services:
476485
spuc:
477486
image: spuacv/spuc:latest
@@ -507,6 +516,7 @@ The only thing we are missing is enabling the plugin.
507516

508517
We used a bind mount before to put the plugin file in the container, so lets try again:
509518
```yml
519+
# compose.yml
510520
services:
511521
spuc:
512522
image: spuacv/spuc:latest
@@ -554,6 +564,7 @@ Let's go back to that.
554564

555565
We could use the tag we used when we built the container to use that image:
556566
```yml
567+
# compose.yml
557568
services:
558569
spuc:
559570
image: spuc-stats # Use the image we built earlier
@@ -578,6 +589,7 @@ Instead, we can use the `build` key to tell Docker Compose to build the containe
578589

579590
To do that, we use the `build` key instead of the `image` key:
580591
```yml
592+
# compose.yml
581593
services:
582594
spuc:
583595
# image: spuc-stats
@@ -707,6 +719,7 @@ For our service named `spuc`, the hostname would be `spuc` with the protocol `ht
707719

708720
Knowing this, we are able to add SPUCSVi to our Docker Compose file!
709721
```yml
722+
# compose.yml
710723
services:
711724
spuc:
712725
build:
@@ -821,6 +834,7 @@ This is a good security practice and helps keep things tidy.
821834

822835
To do this we need to stop exposing the ports for SPUC, by removing the `ports` key from the SPUC service:
823836
```yml
837+
# compose.yml
824838
services:
825839
spuc:
826840
build:
@@ -873,6 +887,7 @@ You also need to specify the network name for each service that you want to conn
873887
For example, to specify the network name as `spuc_network`, you would add the following to the file:
874888

875889
```yml
890+
# compose.yml
876891
services:
877892
spuc:
878893
build:
@@ -919,6 +934,7 @@ Docker Compose has a solution to this - the `depends_on` key.
919934

920935
We can use this key to tell Docker Compose that the SPUCSVi service depends on the SPUC service.
921936
```yml
937+
# compose.yml
922938
services:
923939
spuc:
924940
build:
@@ -968,6 +984,7 @@ We need to add the `--fail` flag to `curl` to ensure that it returns a non-zero
968984
The other change we need to make is to add a `condition` to the `depends_on` key in the SPUCSVi service.
969985
This tells Docker Compose to only start the service if the service it depends on is *healthy*, rather than just *started*.
970986
```yml
987+
# compose.yml
971988
services:
972989
spuc:
973990
build:
@@ -1012,6 +1029,7 @@ This is a little hard to see in action as the SPUC service starts so quickly.
10121029
To be able to see it, let's add a `sleep` command to the `entrypoint` of the SPUC service to simulate a slow start.
10131030

10141031
```yml
1032+
# compose.yml
10151033
services:
10161034
spuc:
10171035
build:
@@ -1068,6 +1086,7 @@ To simulate a service that does not pass the healthcheck,
10681086
we can set the `EXPORT` environment variable to `false` in the SPUC service.
10691087
This will mean that the export endpoint is not available, so the healthcheck will fail.
10701088
```yml
1089+
# compose.yml
10711090
services:
10721091
spuc:
10731092
build:

episodes/dockerfiles.Rmd

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Let's add a new plugin that will return a statistical analysis of the brightness
2727

2828
First lets make a file `stats.py` with the following content:
2929
```python
30+
# stats.py
3031
from __main__ import app
3132
from __main__ import file_path
3233

@@ -93,6 +94,7 @@ This way we can be sure that our new image will have all the dependencies we nee
9394

9495
Let's create a new file called `Dockerfile` and add the following content:
9596
```Dockerfile
97+
# Dockerfile
9698
FROM spuacv/spuc:latest
9799
```
98100

@@ -222,6 +224,9 @@ We do this by adding a `RUN` instruction to the `Dockerfile`.
222224
This instruction runs a command in the container and then saves the result as a new layer in the image.
223225
In this case we want to install the `pandas` package so we add the following lines to the `Dockerfile`:
224226
```Dockerfile
227+
# Dockerfile
228+
FROM spuacv/spuc:latest
229+
225230
RUN pip install pandas
226231
```
227232

@@ -288,6 +293,11 @@ It takes two arguments: the source file on the host machine and the destination
288293

289294
Let's add it to the `Dockerfile`:
290295
```Dockerfile
296+
# Dockerfile
297+
FROM spuacv/spuc:latest
298+
299+
RUN pip install pandas
300+
291301
COPY stats.py /spuc/plugins/stats.py
292302
```
293303

@@ -344,6 +354,12 @@ The plugin is still loaded!
344354
And again... why stop there?
345355
We've already configured the print how we like it, so lets add it to the image as well!
346356
```Dockerfile
357+
# Dockerfile
358+
FROM spuacv/spuc:latest
359+
360+
RUN pip install pandas
361+
362+
COPY stats.py /spuc/plugins/stats.py
347363
COPY print.config /spuc/config/print.config
348364
```
349365

@@ -390,6 +406,14 @@ We can also set environment variables in the `Dockerfile` using the `ENV` instru
390406
These can always be overridden when running the container, as we have done ourselves, but it is useful to set defaults.
391407
We like the `EXPORT` variable set to `True`, so let's add that to the `Dockerfile`:
392408
```Dockerfile
409+
# Dockerfile
410+
FROM spuacv/spuc:latest
411+
412+
RUN pip install pandas
413+
414+
COPY stats.py /spuc/plugins/stats.py
415+
COPY print.config /spuc/config/print.config
416+
393417
ENV EXPORT=True
394418
```
395419

@@ -438,6 +462,7 @@ Because of this, moving the `ENV` instruction will *change* the layers, and the
438462

439463
We can see this by moving the `ENV` instruction in our `Dockerfile` before the RUN command:
440464
```Dockerfile
465+
# Dockerfile
441466
FROM spuacv/spuc:latest
442467

443468
ENV EXPORT=True
@@ -492,6 +517,16 @@ As you may remember, the default command is composed of an *entrypoint* and a *c
492517
We can modify either of them in the Dockerfile.
493518
Just to make clear wheat the full command is directly from our dockerfile, lets write down both:
494519
```Dockerfile
520+
# Dockerfile
521+
FROM spuacv/spuc:latest
522+
523+
ENV EXPORT=True
524+
525+
RUN pip install pandas
526+
527+
COPY stats.py /spuc/plugins/stats.py
528+
COPY print.config /spuc/config/print.config
529+
495530
ENTRYPOINT ["python", "/spuc/spuc.py"]
496531
CMD ["--units", "iulu"]
497532
```

0 commit comments

Comments
 (0)