I read the official document a few days ago. in the second part, I finished it.app.py
withdockerfile
, andrequirements.txt
.
2. Run-time input:docker bulid -t dockerdemo .
In the end, I made a mistake. Don’t know why, remember to report the error message is roughlydocker Collecting Flask (from -r requirements.txt (line 1))
3. Then enter it nowdocker bulid -t friendlyhello .
It will run through.
Ask big brothers to explain why.
Operating environment: Docker for Mac
Version: dockerversion 17.06.1-ce, build874a737
Focus:app.py
Anddockerfile
in additionrequirements.txt
There has been no change.
Attachapp.py
Code
from flask import Flask
from redis import Redis, RedisError
import os
import socket
# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)
@app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis, counter disabled</i>"
html = "<h3>Hello {name}! </h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
Attachdockerfile
Code
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Attachrequirements.txt
Flask
Redis
Ask the bosses to explain the problems
The build will execute the Run command. If the network times out, or the build will hang up and the log is incomplete, it is probably these problems.