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

2024-04-26 12:59:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经用python和flask运行了docker文件。 但我无法在localhost中连接flask服务器。 我已经确认了该代码在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 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

我希望它会返回“我从码头”当我进入本地主机:5000,但Chrome浏览器返回“无法连接”

我做错什么了?你知道吗


Tags: install文件todockerrunfrompy服务器
1条回答
网友
1楼 · 发布于 2024-04-26 12:59:14

我假设您在本地主机(而不是虚拟机)上运行docker? 你试过在烧瓶上显式使用端口5000吗?你知道吗

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

相关问题 更多 >