Skip to content

Commit 30acee5

Browse files
authored
Merge pull request #404 from Paraphraser/20210908-python-old-menu
20210908 Python - old-menu branch - PR 2 of 2
2 parents 625e3d0 + 197b5c2 commit 30acee5

File tree

8 files changed

+80
-66
lines changed

8 files changed

+80
-66
lines changed

.templates/python/Dockerfile

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,48 @@
11
FROM python:3
22

3-
WORKDIR /usr/src/app
3+
# user+group ID defaults for IOTstack
4+
ENV IOTSTACK_UID=1000
5+
ENV IOTSTACK_GID=1000
46

5-
COPY requirements.txt ./
7+
# the template folder is named
8+
ENV TEMPLATE="app"
69

7-
RUN pip install --no-cache-dir -r requirements.txt
10+
# where IOTstack template files are stored
11+
ENV PYTHON_DEFAULTS="/iotstack_defaults/${TEMPLATE}"
812

9-
CMD [ "python", "./app.py" ]
13+
# ensure the defaults directory exists
14+
RUN mkdir -p ${PYTHON_DEFAULTS}
15+
16+
# copy template files to defaults directory in image
17+
COPY ${TEMPLATE} ${PYTHON_DEFAULTS}
18+
19+
# the requirements file (if it exists) is
20+
ENV REQUIREMENTS="${PYTHON_DEFAULTS}/requirements.txt"
21+
22+
# set up requirements (if the file exists)
23+
RUN if [ -e ${REQUIREMENTS} ] ; then pip3 install --no-cache-dir -r ${REQUIREMENTS} ; fi
24+
25+
# the python working directory is
26+
ENV PYTHON_WORKDIR="/usr/src/${TEMPLATE}"
27+
28+
# ensure the working directory exists
29+
RUN mkdir -p ${PYTHON_WORKDIR}
30+
31+
# add an entry-point script
32+
ENV ENTRY_POINT="docker-entrypoint.sh"
33+
COPY ${ENTRY_POINT} /${ENTRY_POINT}
34+
RUN chmod 755 /${ENTRY_POINT}
35+
36+
# unset variables that are not needed
37+
ENV TEMPLATE=
38+
ENV REQUIREMENTS=
39+
ENV ENTRY_POINT=
40+
41+
# set the working directory
42+
WORKDIR ${PYTHON_WORKDIR}
43+
44+
# away we go
45+
ENTRYPOINT ["/docker-entrypoint.sh"]
46+
CMD ["/usr/local/bin/python", "./app.py"]
47+
48+
# EOF

.templates/python/app/app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import time
2+
3+
print("The world is born. Hello World.", flush=True)
4+
5+
while True:
6+
7+
time.sleep(10)
8+
print("The world is re-born. Hello World.", flush=True)

.templates/python/app/requirements.txt

Whitespace-only changes.

.templates/python/directoryfix.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# does the working directory exist (something is badly wrong if it does not)
5+
if [ -d "$PYTHON_WORKDIR" ] ; then
6+
7+
# are self-healing defaults available?
8+
if [ -d "$PYTHON_DEFAULTS" ] ; then
9+
10+
# yes! replace anything that has gone missing
11+
cp -an "$PYTHON_DEFAULTS"/* "$PYTHON_WORKDIR"
12+
13+
fi
14+
15+
# set appropriate ownership throughout
16+
chown -R "$IOTSTACK_UID:$IOTSTACK_GID" "$PYTHON_WORKDIR"
17+
18+
fi
19+
20+
# start python
21+
exec "$@"

.templates/python/requirements.txt

Whitespace-only changes.

.templates/python/service.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
container_name: python
33
build: ./services/python/.
44
restart: unless-stopped
5-
network_mode: host
5+
environment:
6+
- TZ=Etc/UTC
7+
- IOTSTACK_UID=1000
8+
- IOTSTACK_GID=1000
9+
# ports:
10+
# - "external:internal"
611
volumes:
712
- ./volumes/python/app:/usr/src/app

docs/Containers/Python.md

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,5 @@
11
# Python
2-
* [Docker hub](https://hub.docker.com/_/python)
32

4-
## Running python code in docker
3+
This is "old menu" documentation.
54

6-
In order to run code in docker the container needs to be build from a Dockerfile. There are 2 key files in the service directory
7-
8-
### services/python/requirements.txt
9-
10-
Normally on your system you would install modules with pip and they would be available system wide. The container that comes off Docker hub is blank and we will have to install them and bake them into the container. Before your first run add the modules that you require to the requirements.txt, each on a new line
11-
12-
```
13-
flask
14-
bs4
15-
```
16-
**IMPORTANT**: Every time you alter the requirements file you will need to rebuild the container and bake in the new modules
17-
18-
To build the container run `docker-compose build python`.
19-
20-
### services/python/service.yml
21-
22-
This is the template that gets concatenated into docker-compose.yml and there are a few things to note here
23-
24-
```yml
25-
python:
26-
container_name: python
27-
build: ./services/python/.
28-
restart: unless-stopped
29-
network_mode: host
30-
volumes:
31-
- ./volumes/python/app:/usr/src/app
32-
```
33-
34-
The container runs in host network mode. This is because i have no idea which ports you want to use. The implication of this is you will not be able to connect by name to the other container and therefore if you want to connect to the mqtt service or influx you will need to use `localhost` or `127.0.0.1` because the python container "thinks" from network perspective that it is the Pi
35-
36-
The container is set to restart unless stopped. Therefore if you write an application it will effectively execute in an endless loop. If you only want a run once method then you will need to comment out the "restart" section in the docker-compose.yml file and the service.yml
37-
38-
## Where to put your code
39-
40-
You will need to copy your code to `IOTstack/volumes/python/app`. The container is set to execute `app.py` as the main file.
41-
42-
### writing to the console
43-
44-
If you execute a print statement the text will appear in the console of the container. The output can be accessed by running `docker logs python`
45-
46-
### writing to disk
47-
Inside the container the working directory is `/usr/src/app` as mapped in the volume command. It would be advised to read or write any data from this directory.
48-
49-
## Image clutter
50-
51-
Doing multiple builds of the python image will create many unused images. These can be cleaned up inside portainer or by running `./scripts/prune-images.sh`
5+
Please see [New Menu documentation](https://sensorsiot.github.io/IOTstack/Containers/Python/).

0 commit comments

Comments
 (0)