Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I open a flask app on my browser that's running remotely on docker?

I am running a flask app using Pycharm and I get the following message: INFO:werkzeug: * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

I have been trying to access the website from my local browser and it’s not working. First, I tried the following in Pycharm and it didn’t work: “RUN > Edit configuration > Docker deployment > Container > Add port binding > container port=5000 > host port=5000”. Is this right? should I add something in Host IP?

I also tried to access the API URL in the build, execution > docker, but it’s not working.

What is the easier way to solve this problem?

like image 919
russel Avatar asked Feb 01 '26 21:02

russel


1 Answers

app.py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Hello World!"

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

you can provide host ip directly

requirements.txt

Flask==0.10.1

or it can be any version

Dockerfile

FROM ubuntu:16.04
MAINTANER Your Name "[email protected]"
RUN apt-get update -y && \
    apt-get install -y python-pip python-dev
#your Dockerfile is missing this line
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt
COPY . /app
ENTRYPOINT [ "python" ]
CMD [ "app.py" ]

docker commands to run

docker build -t flaskapp:latest .
#flask runs in default port 5000
docker run -d -p 5000:5000 flaskapp

In browser: http://localhost:5000

like image 94
Jobin Mathew Avatar answered Feb 03 '26 10:02

Jobin Mathew