简体   繁体   English

码头工人 | 烧瓶 + Redis | 烧瓶没有启动

[英]Docker | Flask + Redis | Flask not launching

I'm trying to containerize a simple python app which stores amount of time a site has been visited.我正在尝试容器化一个简单的 python 应用程序,该应用程序存储访问站点的时间。 Redis seems to work but Flask is refusing to lunch. Redis 似乎工作,但 Flask 拒绝吃午饭。 I get this error on running docker-compose up --build :我在运行docker-compose up --build 时收到此错误:

web-fe_1 exited with code 0 web-fe_1 退出,代码为 0

Below are my Dockerfile, docker-compose.yml & app.py下面是我的 Dockerfile、docker-compose.yml 和 app.py

 FROM python:3.6-alpine ENV FLASK_APP app.py ADD . /code WORKDIR /code RUN pip install -r requirements.txt CMD [ "flask","run","--host=0.0.0.0","--port=5000"]

 version: "3.5" services: web-fe: build: . command: python app.py ports: - target: 5000 published: 5000 networks: - counter-net volumes: - type: volume source: counter-vol target: /code redis: image: "redis:alpine" networks: counter-net: networks: counter-net: volumes: counter-vol:

 import os from flask import Flask from flask_redis import FlaskRedis app = Flask(__name__) app.config['REDIS_URL'] = 'redis://redis:6379/0' redis = FlaskRedis(app) @app.route('/') def counter(): return '{0} {1} {2}'.format('Hello! You have visited me:',str(redis.incr('web2_counter')),' times.')

This is almost identical to Poulton's counter-app on https://github.com/nigelpoulton/counter-app .. with only difference that my app.py has no call function ( if__name__== “ main ).. And the problem is that I would like to make this work without modification to app.py and solve this only through Dockerfile / docker-compose这几乎是相同的波尔顿的反应用上https://github.com/nigelpoulton/counter-app 。与唯一的区别在于我的app.py没有通话功能(if__name __ ==“”)。而这个问题是我想在不修改app.py 的情况下完成这项工作,并且只能通过Dockerfile / docker -compose解决这个问题

About your docker-compose.yml defining networks is not necessary as bridge network is the default already.关于您docker-compose.yml定义网络不是必需的,因为bridge网络已经是默认设置。 You can update your docker-compose.yml to:您可以将docker-compose.yml更新为:

version: "3.5"
services:
  web-fe:
    build: .
    command: python app.py
    ports:
      - target: 5000
        published: 5000
    volumes:
      - type: volume
        source: counter-vol
        target: /code
  redis:
    image: "redis:alpine"

volumes:
  counter-vol:

And I geuss changing line app.config['REDIS_URL'] = 'redis://redis:6379/0' to app.config['REDIS_URL'] = 'http://redis:6379'我将行app.config['REDIS_URL'] = 'redis://redis:6379/0'更改为app.config['REDIS_URL'] = 'http://redis:6379'

You're missing the final 2 lines of his app.py which makes the app listen for connections.你错过了他的 app.py 的最后两行,这使得应用程序侦听连接。 When you don't have those 2 lines, your app exits immediately and your container stops.当您没有这两行时,您的应用程序会立即退出并且您的容器会停止。

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

I'm not very good with python.我对python不是很好。 Can you just leave out the 'if' line and keep the 'app.run' line so it always runs?你能去掉'if'行并保留'app.run'行使其始终运行吗?

In your Dockerfile you specify a CMD that tells the container to flask run your application.在您的 Dockerfile 中,您指定一个CMD ,它告诉容器flask run您的应用程序。 That reads in the app.py file (from $FLASK_APP ) and launches a Web server.读取app.py文件(来自$FLASK_APP )并启动 Web 服务器。 In the docker-compose.yml file, though, you override this with command: python app.py which just runs the Python file, without the Flask wrapper.但是,在docker-compose.yml文件中,您可以使用以下command: python app.py覆盖它command: python app.pycommand: python app.py运行 Python 文件,没有 Flask 包装器。

One way to address this is with @HansKillian's approach to make app.py call app.run() itself, but it'd be easier to strip out all of the unnecessary parts from the docker-compose.yml file.解决这个问题的一种方法是使用@HansKillian 的方法app.py调用app.run()本身,但从docker-compose.yml文件中删除所有不必要的部分会更容易。 Especially deleting the command: override should resolve this.尤其是删除command: override 应该可以解决这个问题。

version: "3.5"
services:
  web-fe:
    build: .
    # command: python app.py  <-- CMD in the Dockerfile
    ports:
      - target: 5000
        published: 5000
    # networks:               <-- Compose provides a "default" network
    #   - counter-net
    # volumes:                <-- Code is in the image and does not need
    #   - type: volume            to be hidden with a volume
    #     source: counter-vol
    #     target: /code
  redis:
    image: "redis:alpine"
    # networks:               <-- Compose provides a "default" network
    #   counter-net:

# networks:                   <-- Unused
#   counter-net:

# volumes:                    <-- Unused
#   counter-vol:

(Compose automatically provides a network named default for you, so you don't usually need to specify networks: . The volumes: declaration mounts a Docker named volume over your application code, so you're not actually running the code in the image. This particular setup will apparently work the first time but ignore any changes if you docker-compose build the image again.) (Compose 会自动为您提供一个名为default的网络,因此您通常不需要指定networks: 。volumes volumes:声明在您的应用程序代码上挂载了一个名为 volume 的 Docker,因此您实际上并没有在映像中运行代码。这个特定的设置显然会在第一次工作,但如果您再次使用docker-compose build图像,则会忽略任何更改。)

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

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