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: docs/Containers/Python.md
+35-35Lines changed: 35 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,9 +20,9 @@ When you select Python in the menu:
20
20
├── docker-entrypoint.sh
21
21
└── Dockerfile
22
22
```
23
-
23
+
24
24
Note:
25
-
25
+
26
26
* Under "old menu" (old-menu branch), the `service.yml` is also copied into the `python` directory but is then not used.
27
27
28
28
2. This service definition is added to your `docker-compose.yml`:
@@ -43,9 +43,9 @@ When you select Python in the menu:
43
43
networks:
44
44
- iotstack_nw
45
45
```
46
-
46
+
47
47
Note:
48
-
48
+
49
49
* This service definition is for "new menu" (master branch). The only difference with "old menu" (old-menu branch) is the omission of the last two lines.
50
50
51
51
### <aname="customisingPython"> customising your Python service definition </a>
@@ -115,9 +115,9 @@ This is what happens:
115
115
```
116
116
117
117
The `docker-entrypoint.sh` script runs each time the container launches and performs initialisation and "self repair" functions.
118
-
118
+
119
119
The output of the Dockerfile run is a new **local** image tagged with the name `iotstack_python`.
120
-
120
+
121
121
4. The `iotstack_python` image is instantiated to become the running container.
122
122
5. When the container starts, the `docker-entrypoint.sh` script runs and initialises the container's persistent storage area:
123
123
@@ -128,11 +128,11 @@ This is what happens:
128
128
└── [drwxr-xr-x pi ] app
129
129
└── [-rwxr-xr-x pi ] app.py
130
130
```
131
-
131
+
132
132
Note:
133
-
133
+
134
134
* the top-level `python` folder is owned by "root" but the `app` directory and its contents are owned by "pi".
135
-
135
+
136
136
5. The initial `app.py` Python script is a "hello world" placeholder. It runs as an infinite loop emitting messages every 10 seconds until terminated. You can see what it is doing by running:
137
137
138
138
```bash
@@ -142,9 +142,9 @@ This is what happens:
142
142
The world is re-born. Hello World.
143
143
…
144
144
```
145
-
145
+
146
146
Pressing <kbd>control</kbd>+<kbd>c</kbd> terminates the log display but does not terminate the running container.
147
-
147
+
148
148
## <aname="stopPython"> stopping the Python service </a>
149
149
150
150
To stop the container from running, either:
@@ -155,14 +155,14 @@ To stop the container from running, either:
155
155
$ cd ~/IOTstack
156
156
$ docker-compose down
157
157
```
158
-
158
+
159
159
* terminate the python container
160
160
161
161
```bash
162
162
$ cd ~/IOTstack
163
163
$ docker-compose rm --force --stop -v python
164
164
```
165
-
165
+
166
166
## <aname="startPython"> starting the Python service </a>
167
167
168
168
To bring up the container again after you have stopped it, either:
@@ -173,7 +173,7 @@ To bring up the container again after you have stopped it, either:
173
173
$ cd ~/IOTstack
174
174
$ docker-compose up -d
175
175
```
176
-
176
+
177
177
* bring up the python container
178
178
179
179
```bash
@@ -293,7 +293,7 @@ To make *Flask* and *beautifulsoup4* a permanent part of your container:
293
293
```
294
294
$ cd ~/IOTstack/services/python/app
295
295
```
296
-
296
+
297
297
2. Use your favourite text editor to create the file `requirements.txt` in that directory. Each package you want to add should be on a line by itself:
298
298
299
299
```
@@ -311,7 +311,7 @@ To make *Flask* and *beautifulsoup4* a permanent part of your container:
311
311
```
312
312
313
313
Note:
314
-
314
+
315
315
* You will see a warning about running pip as root - ignore it.
316
316
317
317
4. Confirm that the packages have been added:
@@ -331,19 +331,19 @@ Note:
331
331
```
332
332
~/IOTstack/volumes/python/app/requirements.txt
333
333
```
334
-
334
+
335
335
This copy is the result of the "self-repair" code that runs each time the container starts noticing that `requirements.txt` is missing and making a copy from the defaults stored inside the image.
336
-
336
+
337
337
If you make more changes to the master version of `requirements.txt` in the *services* directory and rebuild the local image, the copy in the *volumes* directory will **not** be kept in-sync. That's because the "self-repair" code **never** overwrites existing files.
338
-
338
+
339
339
If you want to bring the copy of `requirements.txt` in the *volumes* directory up-to-date:
340
-
340
+
341
341
```
342
342
$ cd ~/IOTstack
343
343
$ rm ./volumes/python/app/requirements.txt
344
344
$ docker-compose restart python
345
345
```
346
-
346
+
347
347
The `requirements.txt` file will be recreated and it will be a copy of the version in the *services* directory as of the last image rebuild.
348
348
349
349
### <aname="scriptBaking"> making your own Python script the default </a>
@@ -355,13 +355,13 @@ Suppose the Python script you have been developing reaches a major milestone and
That generates a `requirements.txt` representing the state of play inside the running container. Because it is running *inside* the container, the `requirements.txt` created by that command appears *outside* the container at:
360
-
360
+
361
361
```
362
362
~/IOTstack/volumes/python/app/requirements.txt
363
363
```
364
-
364
+
365
365
2. Make your work the default:
366
366
367
367
```bash
@@ -370,13 +370,13 @@ Suppose the Python script you have been developing reaches a major milestone and
370
370
```
371
371
372
372
The `cp` command copies:
373
-
373
+
374
374
* your Python script;
375
375
* the optional `requirements.txt` (from step 1); and
376
376
* any other files you may have put into the Python working directory.
377
377
378
378
Key point:
379
-
379
+
380
380
* **everything** copied into `./services/python/app` will become part of the new local image.
381
381
382
382
3. Terminate the Python container and erase its persistent storage area:
@@ -386,26 +386,26 @@ Suppose the Python script you have been developing reaches a major milestone and
386
386
$ docker-compose rm --force --stop -v python
387
387
$ sudo rm -rf ./volumes/python
388
388
```
389
-
389
+
390
390
Note:
391
-
391
+
392
392
* If erasing the persistent storage area feels too risky, just move it out of the way:
393
393
394
394
```
395
395
$ cd ~/IOTstack/volumes
396
396
$ sudo mv python python.off
397
397
```
398
-
398
+
399
399
4. Rebuild the local image:
400
400
401
401
```bash
402
402
$ cd ~/IOTstack
403
403
$ docker-compose build --force-rm python
404
404
$ docker-compose up -d --force-recreate python
405
405
```
406
-
406
+
407
407
On its first launch, the new container will re-populate the persistent storage area but, this time, it will be your Python script and any other supporting files, rather than the original "hello world" script.
408
-
408
+
409
409
5. Clean up by removing the old local image:
410
410
411
411
```bash
@@ -433,7 +433,7 @@ Proceed like this:
433
433
```
434
434
$ docker rmi iotstack_python
435
435
```
436
-
436
+
437
437
3. Rename the `python` services directory to the name of your project:
438
438
439
439
```
@@ -459,11 +459,11 @@ Proceed like this:
459
459
networks: networks:
460
460
- iotstack_nw - iotstack_nw
461
461
```
462
-
462
+
463
463
Note:
464
-
464
+
465
465
* if you make a copy of the `python` service definition and then perform the required "wishbone" edits on the copy, the `python` definition will still be active so `docker-compose` may try to bring up both services. You will eliminate the risk of confusing yourself if you follow these instructions "as written" by **not** leaving the `python` service definition in place.
0 commit comments