【发布时间】:2019-09-05 13:19:18
【问题描述】:
我已经使用 python 和烧瓶运行了 docker 文件。 但我无法在本地主机中连接烧瓶服务器。 我已经确认该代码与 Mac 中的 PyCham 配合良好。
查看日志后,我认为 Flask 服务器在容器中运行良好。 但我无法在 localhost 中连接
Python 代码
# app.py
from flask import Flask
app = Flask('app')
@app.route('/')
def index():
return "I'm from docker"
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
else:
print('imported')
Dockerfile
FROM ubuntu:latest
MAINTAINER ian <ian@mysterico.com>
RUN apt-get update
RUN apt-get install python3 -y
RUN echo "python3 install SUCCESS #####"
RUN apt-get install python3-pip -y
RUN echo "pip3 install SUCCESS ######"
EXPOSE 5000
COPY requirements.txt /
COPY app.py /
WORKDIR /
RUN pip3 install -r requirements.txt
CMD python3 app.py
一些 Docker 日志
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0691161cd0b5 flask:latest "/bin/sh -c 'python3…" 2 minutes ago Up 2 minutes 0.0.0.0:5000->5000/tcp flask
$ docker logs 0691161cd0b5
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 280-257-458
$ docker exec -it 71305c45d05b bash
root@71305c45d05b:/# curl 127.0.0.1:5000 -v
Rebuilt URL to: 127.0.0.1:5000/
Trying 127.0.0.1...
TCP_NODELAY set
Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
GET / HTTP/1.1
Host: 127.0.0.1:5000
User-Agent: curl/7.58.0
Accept: */*
HTTP 1.0, assume close after body
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 15
Server: Werkzeug/0.15.2 Python/3.6.7
Date: Mon, 15 Apr 2019 15:31:36 GMT
Closing connection 0
I'm from docker
我希望当我输入 localhost:5000 时它会返回“我来自 docker”,但 Chrome 浏览器返回“无法连接”
我设置错了什么?
【问题讨论】:
-
如何启动这个容器?
-
我确实认为您可以添加您在浏览器中使用的网址。还有 http 状态码,这会更有帮助。