Python开发人员:请参阅最后一部分“面向Python开发人员”!
MacOs:10.13.1 (17B1003)
Docker版本:
Client:
Version: 17.09.0-ce
API version: 1.32
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:40:09 2017
OS/Arch: darwin/amd64
Server:
Version: 17.09.0-ce
API version: 1.32 (minimum version 1.12)
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:45:38 2017
OS/Arch: linux/amd64
Experimental: true以下"Get stated“不起作用:https://docs.docker.com/get-started/part2/#run-the-app
运行:docker run -p 4000:80 friendlyhello
只会生成以下内容:
5e4d9c813323 friendlyhello "python app.py" 6 seconds ago Exited (0) 4 seconds ago focused_payne并且docker logs _ js为空。
另外,如果:
docker run -it -p 4000:80 friendlyhello /bin/bash和run: python app.py
没什么。因此,这可能与python app.py有关。
注意:我不是Python开发人员。
对于Python开发人员:
app.py:
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)发布于 2017-12-05 21:43:13
我认为你做错了什么,我复制了所有三个文件,并且运行得很好。
https://gist.github.com/kingbuzzman/b48be91757fc60e97f6a9a189d006bd8
下面是完整的步骤:(我真的建议你在运行这篇文章时使用/tmp )
# 1. Create a temp folder
# 2. Download all the files into the temp folder
# 3. Build the docker image
# 4. Run the image in a detached mode -- so we can curl the url
# 5. Sleep a little and wait until the app is fully up
# 6. Test that it all works
# 7. Stop the container (and we don't care about its stdout)
mkdir -p dockerapp && \
curl -sL https://gist.github.com/kingbuzzman/b48be91757fc60e97f6a9a189d006bd8/download | tar -xvz -C dockerapp --strip-components=1 && \
docker build -t friendlyhello dockerapp && \
docker run --rm -p 4000:80 --name friendlyhello -d friendlyhello && \
sleep 2 && \
curl -s http://localhost:4000/ -w "\n" && \
docker kill friendlyhello >/dev/null(您可以突出显示所有这些内容并将其直接复制到您的终端中,然后查看以下内容)

我们在这里寻找的是最后一行<h3>Hello World!</h3><b>Hostname:</b> 979c5e755f64<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>,它是我们知道它运行的方式。
https://stackoverflow.com/questions/47630767
复制相似问题