代码之家  ›  专栏  ›  技术社区  ›  peachykeen

Docker让Flask服务器运行,但我无法用浏览器连接到它

  •  -1
  • peachykeen  · 技术社区  · 6 年前

    我正试图通过Docker运行我的flask应用程序。我上周就开始工作了,但现在它给了我一些问题。我可以启动服务器,但似乎无法通过浏览器访问任何内容。

    这是我的 app.py 文件(减少):

    ... imports ...
    app = Flask(__name__)
    DATABASE = "users.db"
    app.secret_key = os.environ["SECRET_KEY"]
    app.config['UPLOAD_FOLDER'] = 'static/Content'
    
    BASE_PATH = os.path.dirname(os.path.abspath(__file__))
    
    # For Dropbox
    __TOKEN = os.environ["dbx_access_token"]
    __dbx = None 
    
    ... functions ...
    
    
    if __name__ == '__main__':
         app.run(port=5001, threaded=True, host=('0.0.0.0'))
    

    这是我的 Dockerfile :

    # Use an official Python runtime as a parent image
    FROM python:3
    
    # Set the working directory to /VOSW-2016-original
    WORKDIR /VOSW-2016-original
    
    # Copy the current directory contents into the container at /VOSW-2016-original
    ADD . /VOSW-2016-original
    
    # Putting a variables in the environment
    ENV SECRET_KEY="XXXXXXXX"
    ENV dbx_access_token="XXXXXXXX"
    
    # Install any needed packages specified in requirements.txt
    RUN pip install --trusted-host pypi.python.org -r requirements.txt
    
    # Make port 8000 available to the world outside this container
    EXPOSE 8000
    
    # Run app.py when the container launches
    CMD ["python", "app.py", "--host=0.0.0.0"]
    

    以下是我用来启动服务器的命令:

    docker build -t vosw_original ./VOSW-2016-original/
    

    然后:

    docker run -i -t -p 5001:8000 vosw_original
    

    然后我得到消息:

    * Running on http://0.0.0.0:5001/ (Press CTRL+C to quit)
    

    因此,服务器似乎正在运行,但当我执行以下任一操作时,似乎无法访问它:

    1. http://0.0.0.0:5001/
    2. http://0.0.0.0:8000/
    3. http://127.0.0.1:5001/
    4. http://127.0.0.1:8000/

    我哪里出错了?

    1 回复  |  直到 6 年前
        1
  •  1
  •   r3mus n0x    6 年前

    您在错误的端口上运行应用程序:

    app.run(port=5001, threaded=True, host=('0.0.0.0'))
    

    同时暴露8000。所以通过 -p 5001:8000 您正在将容器的8000端口(没有任何侦听)映射到主机的5001端口。当您的应用程序在端口5001上运行时 在容器内 这就是信息的意义所在。