简体   繁体   English

在docker中部署Flask应用程序。 无法连接到该网站

[英]Deploying Flask app in docker. unable to connect to the site

I've run docker file using python and flask. 我使用python和flask运行docker文件。 but i can't connect flask server in localhost. 但是我无法在localhost中连接flask服务器。 I've already confirmed that the code is working well with PyCham in Mac . 我已经确认代码在Mac中与PyCham一起运行良好。

after checking the log, I think the flask server is working well in the container. 检查日志后,我认为烧瓶服务器在容器中运行良好。 but i can't connect in localhost 但我无法连接localhost


Python Code 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 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

Some Docker logs 一些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

I expect that it will return "I'm from docker" when i entered localhost:5000, but Chrome browser returned "Unable to connect" 当我输入localhost:5000时,我希望它会返回“我来自docker”,但Chrome浏览器返回“无法连接”

What did I set wrong? 我犯了什么错?

I assume you are running docker on your localhost (and not on a VM)? 我假设您在本地主机上运行docker(而不是在VM上)? Have you tried explicitly using port 5000 on flask? 您是否尝试在烧瓶上明确使用端口5000?

app.run(host="0.0.0.0", port=int("5000"), debug=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM