docker“开始(Python)”不工作

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 

以下“获取声明”不起作用: 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空。

另外如果:

 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) 

我认为你做错了什么,我复制了所有三个文件,并运行它就好了。

https://gist.github.com/kingbuzzman/b48be91757fc60e97f6a9a189d006bd8

这里是完整的步骤:(我真的build议你在/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 

(您可以突出显示并将所有内容直接复制到您的terminal中,然后查看)

在这里输入图像说明

我们正在寻找的是最后一行<h3>Hello World!</h3><b>Hostname:</b> 979c5e755f64<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>这是我们如何知道它的运行。