Deploying a minimal flask app in docker - server connection issues

后端 未结 6 1369
清酒与你
清酒与你 2020-11-22 00:51

I have an app who\'s only dependency is flask, which runs fine outside docker and binds to the default port 5000. Here is the full source:

from          


        
6条回答
  •  后悔当初
    2020-11-22 01:28

    You need to modify the host to 0.0.0.0 in the docker file. This is a minimal example

    # Example of Dockerfile
    
    FROM python:3.8.5-alpine3.12
    
    WORKDIR /app
    
    EXPOSE 5000
    ENV FLASK_APP=app.py
    
    COPY . /app
    RUN pip install -r requirements.txt
    
    ENTRYPOINT [ "flask"]
    CMD [ "run", "--host", "0.0.0.0" ]
    

    and the file app.py is

    # app.py
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route("/")
    def home():
        return "Hello world"
    
    
    if __name__ == "__main__":
        app.run()
    

    Then compile with

    docker build . -t deploy_flask
    

    and run with

    docker run -p 5000:5000 -t -i deploy_flask:latest
    

    You can check the response with curl http://127.0.0.1:5000/ -v

提交回复
热议问题