Skip to content

Commit 96ab813

Browse files
committed
Add Dockerfile
1 parent 501da9d commit 96ab813

5 files changed

Lines changed: 87 additions & 0 deletions

File tree

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Dockerfile
2+
.dockerignore
3+
venv/
4+
.git/
5+
.mypy_cache/
6+
config.json

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.pyc
33
venv/
44
config.json
5+
.mypy_cache/

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM python:3.10-alpine3.14
2+
3+
# Add user (UID 4333, chosen randomly)
4+
RUN addgroup -g 4333 -S spaceapi \
5+
&& adduser -u 4333 -S -G spaceapi spaceapi
6+
7+
# Install system dependencies
8+
RUN apk update && apk add bash dumb-init
9+
10+
# Install project dependencies
11+
COPY requirements.txt /code/requirements.txt
12+
RUN pip install -r /code/requirements.txt
13+
14+
# Add code
15+
COPY . /code
16+
WORKDIR /code
17+
RUN chown -R spaceapi:spaceapi /code/
18+
19+
# Default config
20+
ENV RELAY_INTERVAL_SECONDS=60
21+
22+
# Switch to non-privileged user
23+
USER spaceapi
24+
25+
# Specify entrypoint
26+
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
27+
CMD ["bash", "entrypoint.sh"]

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ After installing the requirements from `requirements.txt`, just run `python3
2626
relay.py <endpoint-url>` regularly :)
2727

2828

29+
## Docker
30+
31+
The repository provides a `Dockerfile` that calls `relay.py` in a loop.
32+
Configure it using the following env vars:
33+
34+
- `INFLUXDB_HOST`
35+
- `INFLUXDB_PORT`
36+
- `INFLUXDB_USER`
37+
- `INFLUXDB_PASS`
38+
- `INFLUXDB_DB`
39+
- `SPACEAPI_ENDPOINT`
40+
- `RELAY_INTERVAL_SECONDS`
41+
42+
2943
## License
3044

3145
Licensed under either of

entrypoint.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Ensure that the specified variable is defined and non-empty.
5+
# Otherwise, abort.
6+
function ensure_var() {
7+
var=$1
8+
if [[ ${!var:-} == "" ]]; then
9+
1>&2 echo "Error: Variable $var not defined!"
10+
exit 1
11+
fi
12+
}
13+
14+
# Required vars
15+
ensure_var "INFLUXDB_HOST"
16+
ensure_var "INFLUXDB_PORT"
17+
ensure_var "INFLUXDB_USER"
18+
ensure_var "INFLUXDB_PASS"
19+
ensure_var "INFLUXDB_DB"
20+
ensure_var "SPACEAPI_ENDPOINT"
21+
ensure_var "RELAY_INTERVAL_SECONDS"
22+
23+
# Write config file
24+
{
25+
echo "{"
26+
echo " \"influxdb_host\": \"$INFLUXDB_HOST\","
27+
echo " \"influxdb_port\": $INFLUXDB_PORT,"
28+
echo " \"influxdb_user\": \"$INFLUXDB_USER\","
29+
echo " \"influxdb_pass\": \"$INFLUXDB_PASS\","
30+
echo " \"influxdb_db\": \"$INFLUXDB_DB\""
31+
echo "}"
32+
} > config.json
33+
34+
# Run every $RELAY_INTERVAL_SECONDS seconds
35+
while true; do
36+
echo -n "$(date) "
37+
python3 relay.py "$SPACEAPI_ENDPOINT"
38+
sleep "$RELAY_INTERVAL_SECONDS"
39+
done

0 commit comments

Comments
 (0)