如何在Docker容器中设置flask-socketio? [英] How to setup flask-socketio in a docker container?

查看:52
本文介绍了如何在Docker容器中设置flask-socketio?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试在Docker容器中设置flask-socketio.它似乎正在运行,但是当我尝试访问端口5000上的localhost时,出现了错误(来自浏览器),就像我以前使用flask应用程序一样.上面写着:无法连接!

Hello I'm trying to setup flask-socketio in a docker container. It seems to run but I get an error( from the browser) when I try to access localhost on port 5000 like I'm used to do with flask apps. It say's: unable to connect!

我将向您展示5个重要文件:Dockerfile,Requirements.txt,docker-compose.yml,web_app.py和index.html

I will show you the 5 important files: Dockerfile, requirements.txt, docker-compose.yml, web_app.py and index.html

Dockerfile:

Dockerfile:

FROM python:3.6.5

WORKDIR /code
COPY * /code/
RUN pip install -r requirements.txt

requirements.txt:

requirements.txt:

Flask==1.0.2
Flask-SocketIO==3.0.1
eventlet==0.24.1

docker-compose.yml:

docker-compose.yml:

version: "3"
services:
  web:
    build: ./web
    ports:
      - '5000:5000'
    volumes:
      - './web:/code'

我使用命令 docker-compose up --build docker-compose run web/bin/bash 命令以交互模式进入此容器.

I use the commands docker-compose up --build and docker-compose run web /bin/bash to enter this container in interactive mode.

web_app.py:

web_app.py:

from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@app.route('/')
def index():
    return render_template('index.html')

@socketio.on('my event')
def log_message(message):
    emit('my response', {'data': 'got it!'})

if __name__ == '__main__':
    socketio.run(app)

index.html:

index.html:

<!doctype html>
<html>
<head>
  <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
  <title>SocketIO</title>
</head>
<body>

  <script type="text/javascript" charset="utf-8">
    //Establish connection and emit a message to confirm.

    var socket = io.connect('http://' + document.domain + ':' + location.port);
    socket.on('connect', function() {
        socket.emit('my event', {data: 'I\'m connected!'});
    });

  </script>

</body>
</html>

一旦在容器中,我就简单地运行: python web_app.py ,但是什么也没有发生.没有错误,也没有工作页面.

Once inside the container I simple run: python web_app.py but nothing happens. No error and no working page.

我觉得自己不见了,因此采取了正确初始化所有步骤的步骤,但我无法找出它的含义.网络上充满了非常不同的示例,我感到困惑.更难的是,我在这里使用eventlet,但并非每个示例都遵循这种方法.有些人使用gevent或其他东西.

I feel like I'm missing so steps to initialize everything correctly but I cant find out what it is. The web is full of very different examples and I'm confused. What makes it even harder is that I'm using eventlet here but not every example goes this route. Some use gevent or other things.

如果有人给我一点提示,我将非常高兴.欢呼声

I would be really glad if someone gave me a little hint. Cheers

推荐答案

开始撰写时,请确保已为flask设置了所有环境变量.还要在入口点或命令中提供-host = 0.0.0.0 来侦听所有网络接口.

When starting compose, make sure all environment variable are set for flask. Also provide the --host=0.0.0.0 to listen on all network interfaces, in your entrypoint or command.

更新的docker-compose文件:

The updated docker-compose file:

version: "3"
services:
    web:
        build: ./web
        volumes:
            - './application:/application'
        environment:
            FLASK_DEBUG: 1
            FLASK_ENV: development
            FLASK_APP: web_app.py
        ports:
            - '5000:5000'
        entrypoint:
            - flask
            - run
            - --host=0.0.0.0

如果出于开发目的以交互方式运行容器,则可以使用 docker-compose run 以下命令.需要-service-ports 来公开组成文件中指定的容器端口.如果未提供此标志,则不会有外部流量到达该应用.那是我最初的问题.

When you want to run the container in interactive mode for development purposes, you could run it with docker-compose run the below command. --service-ports is required to expose the containers ports as specified in the compose file. If this flag isn't provided, no external traffic will reach the app. That was my original problem.

docker-compose run --service-ports web bash

或者,您可以手动发布端口

Alternatively you could publish the port manually

docker-compose run --publish 5000:5000 web bash

这篇关于如何在Docker容器中设置flask-socketio?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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