Skip to content

Commit d078b85

Browse files
committed
20220323 mosquitto - master branch - PR 1 of 2
Changed Dockerfile and default service definition to support `MOSQUITTO_BASE` argument which defaults to `eclipse-mosquitto:latest`. This means users will not have to edit `.templates/mosquitto/Dockerfile` in order to pin to a specific version. Also added image metadata fields (`build-args` and `based-on`) and unset environment variables not needed outside Dockerfile scope. Documentation updated to explain new structure and how to pin. Signed-off-by: Phill Kelley <[email protected]>
1 parent 9cc8533 commit d078b85

File tree

3 files changed

+114
-39
lines changed

3 files changed

+114
-39
lines changed

.templates/mosquitto/Dockerfile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
# supported build argument
2+
ARG MOSQUITTO_BASE=eclipse-mosquitto:latest
3+
14
# Download base image
2-
FROM eclipse-mosquitto:latest
5+
FROM $MOSQUITTO_BASE
6+
7+
# re-reference supported argument and copy to environment var
8+
ARG MOSQUITTO_BASE
9+
ENV MOSQUITTO_BASE=${MOSQUITTO_BASE}
310

411
# see https://github.com/alpinelinux/docker-alpine/issues/98
512
RUN sed -i 's/https/http/' /etc/apk/repositories
@@ -34,4 +41,13 @@ ENV IOTSTACK_ENTRY_POINT=
3441
# IOTstack also declares these paths
3542
VOLUME ["/mosquitto/config", "/mosquitto/pwfile"]
3643

44+
# set container metadata
45+
LABEL com.github.SensorsIot.IOTstack.Dockerfile.build-args="${MOSQUITTO_BASE}"
46+
LABEL com.github.SensorsIot.IOTstack.Dockerfile.based-on="https://github.com/eclipse/mosquitto"
47+
48+
# don't need these variables in the running container
49+
ENV MOSQUITTO_BASE=
50+
ENV HEALTHCHECK_SCRIPT=
51+
ENV IOTSTACK_ENTRY_POINT=
52+
3753
# EOF

.templates/mosquitto/service.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
mosquitto:
22
container_name: mosquitto
3-
build: ./.templates/mosquitto/.
3+
build:
4+
context: ./.templates/mosquitto/.
5+
args:
6+
- MOSQUITTO_BASE=eclipse-mosquitto:latest
47
restart: unless-stopped
58
environment:
69
- TZ=Etc/UTC

docs/Containers/Mosquitto.md

Lines changed: 93 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,30 @@ $ docker-compose up -d
8686
8787
`docker-compose` reads the *Compose* file. When it arrives at the `mosquitto` fragment, it finds:
8888

89-
```
89+
```yaml
9090
mosquitto:
9191
container_name: mosquitto
92-
build: ./.templates/mosquitto/.
92+
build:
93+
context: ./.templates/mosquitto/.
94+
args:
95+
- MOSQUITTO_BASE=eclipse-mosquitto:latest
9396
9497
```
9598

96-
The `build` statement tells `docker-compose` to look for:
99+
Note:
100+
101+
* Earlier versions of the Mosquitto service definition looked like this:
102+
103+
```yaml
104+
mosquitto:
105+
container_name: mosquitto
106+
build: ./.templates/mosquitto/.
107+
108+
```
109+
110+
The single-line `build` produces *exactly* the same result as the four-line `build`, save that the single-line form does not support [pinning Mosquitto to a specific version](#mosquitto-version-pinning).
111+
112+
The `./.templates/mosquitto/.` path associated with the `build` tells `docker-compose` to look for:
97113

98114
```
99115
~/IOTstack/.templates/mosquitto/Dockerfile
@@ -103,11 +119,10 @@ The `build` statement tells `docker-compose` to look for:
103119
104120
The *Dockerfile* begins with:
105121

122+
```dockerfile
123+
ARG MOSQUITTO_BASE=eclipse-mosquitto:latest
124+
FROM $MOSQUITTO_BASE
106125
```
107-
FROM eclipse-mosquitto:latest
108-
```
109-
110-
> If you need to pin to a particular version of Mosquitto, the *Dockerfile* is the place to do it. See [Mosquitto version pinning](#mosquitto-version-pinning).
111126

112127
The `FROM` statement tells the build process to pull down the ***base image*** from [*DockerHub*](https://hub.docker.com).
113128

@@ -144,7 +159,7 @@ You *may* see the same pattern in Portainer, which reports the *base image* as "
144159

145160
> Whether you see one or two rows depends on the version of `docker-compose` you are using and how your version of `docker-compose` builds local images.
146161
147-
### Migration considerations
162+
### <a name="migration-considerations"> Migration considerations </a>
148163

149164
Under the original IOTstack implementation of Mosquitto (just "as it comes" from *DockerHub*), the service definition expected the configuration files to be at:
150165

@@ -217,7 +232,7 @@ log_timestamp_format %Y-%m-%dT%H:%M:%S
217232

218233
When `log_dest` is set to `stdout`, you inspect Mosquitto's logs like this:
219234

220-
```
235+
```bash
221236
$ docker logs mosquitto
222237
```
223238

@@ -233,14 +248,14 @@ log_timestamp_format %Y-%m-%dT%H:%M:%S
233248

234249
and then restart Mosquitto:
235250

236-
```
251+
```bash
237252
$ cd ~/IOTstack
238253
$ docker-compose restart mosquitto
239254
```
240255

241256
The path `/mosquitto/log/mosquitto.log` is an **internal** path. When this style of logging is active, you inspect Mosquitto's logs using the **external** path like this:
242257

243-
```
258+
```bash
244259
$ sudo tail ~/IOTstack/volumes/mosquitto/log/mosquitto.log
245260
```
246261

@@ -291,13 +306,13 @@ The Mosquitto container performs self-repair each time the container is brought
291306

292307
To create a username and password, use the following as a template.
293308

294-
```
309+
```bash
295310
$ docker exec mosquitto mosquitto_passwd -b /mosquitto/pwfile/pwfile «username» «password»
296311
```
297312

298313
Replace «username» and «password» with appropriate values, then execute the command. For example, to create the username "hello" with password "world":
299314

300-
```
315+
```bash
301316
$ docker exec mosquitto mosquitto_passwd -b /mosquitto/pwfile/pwfile hello world
302317
```
303318

@@ -333,7 +348,7 @@ hello:$7$101$ZFOHHVJLp2bcgX+h$MdHsc4rfOAhmGG+65NpIEJkxY0beNeFUyfjNAGx1ILDmI498o4
333348

334349
To remove an entry from the password file:
335350

336-
```
351+
```bash
337352
$ docker exec mosquitto mosquitto_passwd -D /mosquitto/pwfile/pwfile «username»
338353
```
339354

@@ -423,15 +438,15 @@ There are several ways to reset the password file. Your options are:
423438

424439
If you do not have the Mosquitto clients installed on your Raspberry Pi (ie `$ which mosquitto_pub` does not return a path), install them using:
425440

426-
```
441+
```bash
427442
$ sudo apt install -y mosquitto-clients
428443
```
429444

430445
#### test: *anonymous access is prohibited*
431446

432447
Test **without** providing credentials:
433448

434-
```
449+
```bash
435450
$ mosquitto_pub -h 127.0.0.1 -p 1883 -t "/password/test" -m "up up and away"
436451
Connection Refused: not authorised.
437452
Error: The connection was refused.
@@ -445,7 +460,7 @@ Note:
445460

446461
Test with credentials
447462

448-
```
463+
```bash
449464
$ mosquitto_pub -h 127.0.0.1 -p 1883 -t "/password/test" -m "up up and away" -u hello -P world
450465
$
451466
```
@@ -458,14 +473,14 @@ Note:
458473

459474
Prove round-trip connectivity will succeed when credentials are provided. First, set up a subscriber as a background process. This mimics the role of a process like Node-Red:
460475

461-
```
476+
```bash
462477
$ mosquitto_sub -v -h 127.0.0.1 -p 1883 -t "/password/test" -F "%I %t %p" -u hello -P world &
463478
[1] 25996
464479
```
465480

466481
Repeat the earlier test:
467482

468-
```
483+
```bash
469484
$ mosquitto_pub -h 127.0.0.1 -p 1883 -t "/password/test" -m "up up and away" -u hello -P world
470485
2021-02-16T14:40:51+1100 /password/test up up and away
471486
```
@@ -476,7 +491,7 @@ Note:
476491

477492
When you have finished testing you can kill the background process (press return twice after you enter the `kill` command):
478493

479-
```
494+
```bash
480495
$ kill %1
481496
$
482497
[1]+ Terminated mosquitto_sub -v -h 127.0.0.1 -p 1883 -t "/password/test" -F "%I %t %p" -u hello -P world
@@ -499,7 +514,7 @@ The agent is invoked 30 seconds after the container starts, and every 30 seconds
499514
* Subscribes to the same broker for the same topic for a single message event.
500515
* Compares the payload sent with the payload received. If the payloads (ie time-stamps) match, the agent concludes that the Mosquitto broker (the process running inside the same container) is functioning properly for round-trip messaging.
501516

502-
### monitoring health-check
517+
### <a name="monitoring health-check"> monitoring health-check </a>
503518

504519
Portainer's *Containers* display contains a *Status* column which shows health-check results for all containers that support the feature.
505520

@@ -545,7 +560,7 @@ Notes:
545560
* If you enable authentication for your Mosquitto broker, you will need to add `-u «user»` and `-P «password»` parameters to this command.
546561
* You should expect to see a new message appear approximately every 30 seconds. That indicates the health-check agent is functioning normally. Use <kbd>control</kbd>+<kbd>c</kbd> to terminate the command.
547562

548-
### customising health-check
563+
### <a name="customising-health-check"> customising health-check </a>
549564

550565
You can customise the operation of the health-check agent by editing the `mosquitto` service definition in your *Compose* file:
551566

@@ -636,47 +651,88 @@ Your existing Mosquitto container continues to run while the rebuild proceeds. O
636651

637652
The `prune` is the simplest way of cleaning up. The first call removes the old *local image*. The second call cleans up the old *base image*. Whether an old *base image* exists depends on the version of `docker-compose` you are using and how your version of `docker-compose` builds local images.
638653

639-
### Mosquitto version pinning
654+
### <a name="mosquitto-version-pinning"> Mosquitto version pinning </a>
640655

641-
If you need to pin Mosquitto to a particular version:
656+
If an update to Mosquitto introduces a breaking change, you can revert to an earlier know-good version by pinning to that version. Here's how:
642657

643-
1. Use your favourite text editor to open the following file:
658+
1. Use your favourite text editor to open:
644659

645660
```
646-
~/IOTstack/.templates/mosquitto/Dockerfile
661+
~/IOTstack/docker-compose.yml
647662
```
648663

649-
2. Find the line:
664+
2. Find the Mosquitto service definition. If your service definition contains this line:
650665

651-
```
652-
FROM eclipse-mosquitto:latest
666+
```yaml
667+
build: ./.templates/mosquitto/.
653668
```
654669

655-
3. Replace `latest` with the version you wish to pin to. For example, to pin to version 2.0.10:
670+
then replace that line with the following four lines:
656671

672+
```yaml
673+
build:
674+
context: ./.templates/mosquitto/.
675+
args:
676+
- MOSQUITTO_BASE=eclipse-mosquitto:latest
657677
```
658-
FROM eclipse-mosquitto:2.0.10
678+
679+
Notes:
680+
681+
* The four-line form of the `build` directive is now the default for Mosquitto so those lines may already be present in your compose file.
682+
* Remember to use spaces, not tabs, when editing compose files.
683+
684+
3. Replace `latest` with the version you wish to pin to. For example, to pin to version 2.0.13:
685+
686+
```yaml
687+
- MOSQUITTO_BASE=eclipse-mosquitto:2.0.13
659688
```
660689

661690
4. Save the file and tell `docker-compose` to rebuild the local image:
662691

663692
```bash
664693
$ cd ~/IOTstack
665-
$ docker-compose up -d --build mosquitto
694+
$ docker-compose build --no-cache --pull mosquitto
695+
$ docker-compose up -d mosquitto
666696
$ docker system prune
667697
```
668698

669699
The new *local image* is built, then the new container is instantiated based on that image. The `prune` deletes the old *local image*.
670700

671-
Note:
701+
5. Images built in this way will always be tagged with "latest", as in:
672702

673-
* As well as preventing Docker from updating the *base image*, pinning will also block incoming updates to the *Dockerfile* from a `git pull`. Nothing will change until you decide to remove the pin.
703+
```bash
704+
$ docker images iotstack_mosquitto
705+
REPOSITORY TAG IMAGE ID CREATED SIZE
706+
iotstack_mosquitto latest 8c0543149b9b About a minute ago 16.2MB
707+
```
708+
709+
You may find it useful to assign an explicit tag to help you remember the version number used for the build. For example:
710+
711+
```bash
712+
$ docker tag iotstack_mosquitto:latest iotstack_mosquitto:2.0.13
713+
$ docker images iotstack_mosquitto
714+
REPOSITORY TAG IMAGE ID CREATED SIZE
715+
iotstack_mosquitto 2.0.13 8c0543149b9b About a minute ago 16.2MB
716+
iotstack_mosquitto latest 8c0543149b9b About a minute ago 16.2MB
717+
```
718+
719+
You can also query the image metadata to discover version information:
720+
721+
```bash
722+
$ docker image inspect iotstack_mosquitto:latest | jq .[0].Config.Labels
723+
{
724+
"com.github.SensorsIot.IOTstack.Dockerfile.based-on": "https://github.com/eclipse/mosquitto",
725+
"com.github.SensorsIot.IOTstack.Dockerfile.build-args": "eclipse-mosquitto:2.0.13",
726+
"description": "Eclipse Mosquitto MQTT Broker",
727+
"maintainer": "Roger Light <[email protected]>"
728+
}
729+
```
674730

675731
## About Port 9001
676732

677733
Earlier versions of the IOTstack service definition for Mosquitto included two port mappings:
678734

679-
```
735+
```yaml
680736
ports:
681737
- "1883:1883"
682738
- "9001:9001"
@@ -693,7 +749,7 @@ If you have a use-case that needs port 9001, you can re-enable support by:
693749

694750
1. Inserting the port mapping under the `mosquitto` definition in `docker-compose.yml`:
695751

696-
```
752+
```yaml
697753
- "9001:9001"
698754
```
699755

@@ -708,7 +764,7 @@ If you have a use-case that needs port 9001, you can re-enable support by:
708764

709765
3. Restarting the container:
710766

711-
```
767+
```bash
712768
$ cd ~/IOTstack
713769
$ docker-compose restart mosquitto
714770
```

0 commit comments

Comments
 (0)