简体   繁体   中英

Flask App not working when running on docker container

Im new to docker and Flask and I'm getting an issue when I try to run the app. The browser says the site (172.17.0.2:5000) can't be reached. For anyone wondering, the Dockerfiles:

FROM ubuntu

RUN apt-get update && apt-get install -y python3 python3-pip

RUN pip3 install flask

RUN mkdir -p /opt/MyApp-test

COPY . /opt/MyApp-test

WORKDIR /opt/MyApp-test

EXPOSE 5000

ENTRYPOINT python3 main.py
CMD flask run --host 0.0.0.0

The main.py:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def index():
    return 'IT WORKED! I AM RUNNING FROM A DOCKER CONTAINER!!!'


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

And when I run the container, I get:

(base) daniellombardi@Daniels-MacBook-Pro MyApp-test % docker run 2625
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

I try to go to http://127.0.0.1:5000/ , unable to connect, then I inspect the container to get its IP address, and it says 172.0.1 instead of 127.0.0.1:5000, but also unable to connect. And the app works when I run it on my computer.

在调用 docker run 时,您应该将容器内暴露的端口绑定到 docker 主机中的端口,因此 docker run 应该这样调用:

docker run 2625 -p 5000:5000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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