I am learning docker using flask application.Here is the below code I am using
app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == "__main__":
app.run(debug=True)
Dockerfile
FROM python:3
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
ENTRYPOINT ["python3", "app.py"]
docker-compose.yml
version: '3.9'
services:
flask:
build: .
image: myapp:latest
When I tried to run the above flask app through python3 app.py it works on the port 5000 correctly.But when I tried to dockerize it docker compose up –build shows that the app is started no error or warning logs but localhost is not getting connected
[![localhost not connected][1]][1]In Dockerfile I tried specifying the host also.Nothing worked. Can someone tell me how to resolve this issue?
I have tried all the solutions mentioned in this link Deploying a minimal flask app in docker – server connection issues
But still having the same issue. I have created a virtual env for this flask app and trying to run the docker container. After changing
app.run()
to
app.run(host='0.0.0.0')
and tried docker run <container_id> got the following console response
Kindly look all the attached image for clear reference
I could see that the app is running in my localhost as well as in some other ip.Attaching the below screenshot for the reference. Can some one address this issue?
[1]: https://i.stack.imgur.com/1yboh.png
2
Answers
Change docker-compose to this
You need to map a port in docker. To do that, add a ports field to your
docker-compose.yml
file. For example:The Flask server will listen on port 80. You can now access the application using port 81 on your machine. More detail