Skip to content

Commit ea6a4bd

Browse files
author
mikespub
committed
make flask configurable
1 parent ebe6666 commit ea6a4bd

4 files changed

Lines changed: 26 additions & 13 deletions

File tree

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FLASK_PORT=5080

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ RUN pip install -r requirements.txt
1111
# Copy our code from the current folder to /app inside the container
1212
ADD . /app
1313

14-
# Make port 5000 available for links and/or publish
15-
EXPOSE 80
14+
# Make port 5080 available for links and/or publish
15+
ENV FLASK_PORT 5080
16+
EXPOSE $FLASK_PORT
1617

1718
# Environment Variables
19+
ENV FLASK_HOST 0.0.0.0
1820
ENV NAME World
1921

2022
# Define our command to be run when launching the container

app.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@
1111

1212
@app.route("/")
1313
def hello():
14+
from flask import request
15+
import json
16+
info = json.dumps(request.environ, indent=2, default=lambda o: repr(o))
1417
try:
1518
visits = redis.incr('counter')
1619
except RedisError:
1720
visits = "<i>cannot connect to Redis, counter disabled</i>"
1821

1922
html = "<h3>Hello {name}!</h3>" \
2023
"<b>Hostname:</b> {hostname}<br/>" \
21-
"<b>Visits:</b> {visits}"
22-
return html.format(name=os.getenv('NAME', "world"), hostname=socket.gethostname(), visits=visits)
24+
"<b>Visits:</b> {visits}<br/>" \
25+
"<b>File:</b> {file}<br/>" \
26+
"<b>Environ:</b> <pre>{environ}</pre>"
27+
return html.format(name=os.getenv('NAME', "world"), hostname=socket.gethostname(), visits=visits, file=__file__, environ=info.replace('<', '&lt;'))
2328

2429

2530
if __name__ == "__main__":
26-
app.run(host='0.0.0.0', port=80)
31+
app.run(host=os.getenv('FLASK_HOST', '0.0.0.0'), port=int(os.getenv('FLASK_PORT', 5080)))

docker-compose.yml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
web:
2-
build: .
3-
links:
4-
- redis
5-
ports:
6-
- "5000:80"
7-
redis:
8-
image: redis
1+
version: '3'
2+
services:
3+
web:
4+
build: .
5+
links:
6+
- redis
7+
ports:
8+
- "${FLASK_PORT}:5080"
9+
environment:
10+
- FLASK_PORT=5080
11+
- FLASK_HOST=0.0.0.0
12+
redis:
13+
image: redis

0 commit comments

Comments
 (0)