无法从Docker外部访问在Docker中运行的Flask Service [英] Not able to access Flask Service running in docker from outside the docker

查看:86
本文介绍了无法从Docker外部访问在Docker中运行的Flask Service的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在docker内部创建了一个flask服务

I have created a flask service inside docker

import json
from flask import Flask, request


app = Flask(__name__)


@app.route("/", methods=["GET", "POST"])
def home():
    return "App is working"

if __name__ == "__main__":
    app.run("0.0.0.0", debug=False)

但是我无法从Docker外部访问以上服务.

But I am not able to access above service from outside the docker.

我的 Dockerfile

FROM python:3.7

MAINTAINER sociopath(xyz@gmail.com)

COPY requirements.txt domain.yml config.yml /tmp/
COPY data /tmp/data
COPY helper /tmp/helper
COPY models /tmp/models
COPY app.py /tmp/app.py

WORKDIR /tmp

RUN pip install -r requirements.txt

EXPOSE 5000

CMD flask run --host 0.0.0.0

我使用命令构建它

docker build.-t cont1

并使用

docker run -d -p 5000:5000 --name c1 cont1

当我从docker内部发送请求时,我得到 App正在运行,但是当我尝试从我的计算机访问时没有响应.

When I send the request from inside the docker I get App is working but no response when I try to access from my machine.

修改

我有Windows操作系统,正在使用安装了docker的Ubuntu VM.我在Ubuntu上收到响应,但是当我尝试从Windows访问同一文件时,我收到了错误消息.

I have windows OS in which I'm using Ubuntu VM where docker is installed. I get response on Ubuntu but When I try to access the same from windows I received an error.

推荐答案

将docker文件暴露给外部源(侦听器)(

Expose your docker file to an external source(Listener)(Ref)

EXPOSE 5000

例如:-

FROM python:3.7

MAINTAINER sociopath(xyz@gmail.com)

COPY requirements.txt domain.yml config.yml /tmp/
COPY data /tmp/data
COPY helper /tmp/helper
COPY models /tmp/models
COPY app.py /tmp/app.py

WORKDIR /tmp

RUN pip install -r requirements.txt
EXPOSE 5000
CMD flask run --host=0.0.0.0

这篇关于无法从Docker外部访问在Docker中运行的Flask Service的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆