Videos
Everything looks good from here. Let's do some debugging.
Let's begin by starting our container with a name:
docker run -d -p 5000:5000 --name flask-app-test flask-app
You can also avoid the -d flag and get the logs right onto your screen.
First check if the Flask app is really running or maybe it has crashed. Start with a simple docker ps (see if you notice anything strange there) followed by docker logs flask-app-test (these will be the logs of the Flask app, has it crashed? is there aything strange?)
If everything looks good until here, then it's time to enter the container. Let's try with docker exec -ti flask-app-test bash. Once you are in, install wget by apt-get install wget and then test if the webserver works from the inside by doing wget http://localhost:5000 and by cat-ting the hopefully downloaded file (ls followed by cat file.html, or whatever it's called).
- Get the container id:
[sudo] docker ps -qf "name=<containername>"
- Get that container ip:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container_id>
- Then connect to
http://<the_resulting_ip>:5000/
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.
Note that this will bind to all interfaces on the host, which may in some circumstances be a security risk - see https://stackoverflow.com/a/58138250/4332 for more information on binding to a specific interface.
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
I have been trying to look for online solutions and practices followed but generally they do it for the flask template app which does not cover the cases for real-world large scale applications. I personally have worked on numerous flask applications but while deploying am never successful in actually implementing the Dockerfile. Kindly guide me on how to tackle this.