From d8482db23fd344752bf276e30b413760067f8289 Mon Sep 17 00:00:00 2001 From: Ahmad Magdy Date: Fri, 20 Apr 2018 16:30:17 +0200 Subject: [PATCH] Update docker file --- Dockerfile | 22 ++++++++++++++-------- app.py | 8 +++++++- install-reqs.sh | 5 +++++ 3 files changed, 26 insertions(+), 9 deletions(-) create mode 100755 install-reqs.sh diff --git a/Dockerfile b/Dockerfile index 6338b11..0dd1070 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,27 @@ # Use an official Python runtime as a parent image FROM python:2.7-slim +ARG PORT +ENV PORT ${PORT:-8080} + +# Define environment variable +ARG NAME +ENV NAME ${NAME:-World} + # Set the working directory to /app WORKDIR /app -# Copy the current directory contents into the container at /app -ADD . /app +ADD install-reqs.sh requirements.txt /app/ # Install any needed packages specified in requirements.txt -RUN pip install --trusted-host pypi.python.org -r requirements.txt +# RUN pip install --trusted-host pypi.python.org -r requirements.txt +RUN bash -c "/app/install-reqs.sh" -# Make port 80 available to the world outside this container -EXPOSE 80 - -# Define environment variable -ENV NAME World +# Make port available to the world outside this container +EXPOSE ${PORT} # Run app.py when the container launches CMD ["python", "app.py"] +# Copy the current directory contents into the container at /app +ADD . /app diff --git a/app.py b/app.py index f52343f..5a13190 100644 --- a/app.py +++ b/app.py @@ -8,6 +8,12 @@ app = Flask(__name__) +def parse_int(s, val=None): + if s.isdigit(): + return int(s, 10) + else: + return val + @app.route("/") def hello(): try: @@ -21,5 +27,5 @@ def hello(): return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits) if __name__ == "__main__": - app.run(host='0.0.0.0', port=8080) + app.run(host='0.0.0.0', port=parse_int(os.getenv("PORT", 8080))) diff --git a/install-reqs.sh b/install-reqs.sh new file mode 100755 index 0000000..e261b7d --- /dev/null +++ b/install-reqs.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +set -eo pipefail; + +pip install --trusted-host pypi.python.org -r requirements.txt \ No newline at end of file