Deploying a minimal flask app in docker - server connection issues

后端 未结 6 1284
清酒与你
清酒与你 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:17

    When using the flask command instead of app.run, you can pass the --host option to change the host. The line in Docker would be:

    CMD ["flask", "run", "--host", "0.0.0.0"]
    

    or

    CMD flask run --host 0.0.0.0
    
    0 讨论(0)
  • 2020-11-22 01:18

    First of all in your python script you need to change code from

    app.run()
    

    to

    app.run(host="0.0.0.0")
    

    Second, In your docker file, last line should be like

    CMD ["flask", "run", "-h", "0.0.0.0", "-p", "5000"]
    

    And on host machine if 0.0.0.0:5000 doesn't work then you should try with localhost:5000

    Note - The CMD command has to be proper. Because CMD command provide defaults for executing container.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 01:30

    The problem is you are only binding to the localhost interface, you should be binding to 0.0.0.0 if you want the container to be accessible from outside. If you change:

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

    to

    if __name__ == '__main__':
        app.run(host='0.0.0.0')
    

    It should work.

    0 讨论(0)
  • 2020-11-22 01:40

    To build on other answers:

    Imagine you have two computers. Each computer has a network interface (WiFi, say), which is its public IP. Each computer has a loopback/localhost interface, at 127.0.0.1. This means "just this computer."

    If you listed on 127.0.0.1 on computer A, you would not expect to be able to connect to that via 127.0.0.1 when running on computer B. After all, you asked to listen on computer A's local, private address.

    Docker is similar setup; technically it's the same computer, but the Linux kernel is allowing each container to run with its own isolated network stack. So 127.0.0.1 in a container is the same as 127.0.0.1 on a different computer than your host—you can't connect to it.

    Longer version, with diagrams: https://pythonspeed.com/articles/docker-connection-refused/

    0 讨论(0)
  • 2020-11-22 01:41

    Your Docker container has more than one network interface. For example, my container has the following:

    $ ip addr
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    32: eth0@if33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
        link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
        inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
           valid_lft forever preferred_lft forever
    

    if you run docker network inspect bridge, you can see that your container is connected to that bridge with the second interface in the above output. This default bridge is also connected to the Docker process on your host.

    Therefore you would have to run the command:

    CMD flask run --host 172.17.0.2
    

    To access your Flask app running in a Docker container from your host machine. Replace 172.17.0.2 with whatever the particular IP address is of your container.

    0 讨论(0)
提交回复
热议问题