Docker“入门(Python)”无效

时间:2017-12-04 10:01:46

标签: python docker

Python开发人员:请参阅上一部分“For Python developer”!

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 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)

1 个答案:

答案 0 :(得分:1)

我认为你做了别的错事,我复制了所有三个文件并运行得很好。

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

(您可以突出显示并将所有这些直接复制到您的终端并查看此内容)

enter image description here

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