gpt4 book ai didi

python - Docker "Get Started(Python)"不工作

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:09 24 4
gpt4 key购买 nike

Python 开发人员:请参阅最后一部分“Python 开发人员”!

MacO: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:80Friendlyhello

仅产生以下内容:

5e4d9c813323        friendlyhello       "python app.py"     6 seconds ago       Exited (0) 4 seconds ago                       focused_payne

并且docker log js为空。

另外如果:

docker run -it -p 4000:80 friendlyhello /bin/bash

运行:python app.py

什么都没有。所以它可能与python app.py有关。

注意:我不是 Python 开发人员。

对于 Python 开发人员:

应用程序.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

以下是完整步骤:(我真的建议您在运行此命令时处于 /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>这就是我们知道它正在运行的方式。

关于python - Docker "Get Started(Python)"不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47630767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com