Flask app runs on docker but not from the host
flask production docker
docker flask 404
flask docker development
docker flask connection refused
docker-compose flask app
host flask app on docker
docker-compose multiple flask apps
I have a flask app that runs a couple of things based on the user call. In this app, I also have one basic endpoint that just returns a string which would help me to see if the app is running or not.
@app.route("/hello") def hello(): return "Hello World!"
Now I tried to dockerize this app using this tutorial from the official docker site. The flask app listens on the port 5000 and so I added an entry in my Dockerfile to expose the port 5000.
And the command that I am using in the Dockerfile to run the app is
model.py file has the flask code that calls other functions based on the user input.
Now when I run my app after containerizing it, I see the required output on the terminal that the flask app is indeed running. This is the command that I used to run the app.
docker run -p 5000:5000 firstContainer
When I try to call the basic helloWorld method above by using the request http://localhost:5000/hello, I get an error message saying that the site is unavailable. Is there anything that I am doing wrong wrt the port mappings here? How do I fix this issue ?
EDIT: Adding more details
So I tried to go into the container to see what's happening and I was able to view the files that were available on the container and they look good. When I tried to start the app again in the container using the base command
it returned an error saying that the port is already in use. So this should mean that the app is indeed listening on the port. I also installed curl inside the container to browse the URL and it returned the expected string when I ran it inside the container. I just don't understand how I can expose it to outside world
* Serving Flask app "model" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: on
The problem is the server defaults to answer internal requests only. You have two options.
You can set the host programatically to:
Use the CMD:
flask run --host=0.0.0.0`
Tip: There is also a container running flask behind
I followed this tutorial on how to build a flask app that runs in docker that My question is, how do I access the flask app in the Docker container on local host? If the container is not running or if you are still getting the error I COPY . /app is the command that copies the entire flask app into the docker run -d -p (port I used something fairly simple like *.dev. Keep in mind the host name will appear in the URL of
try this :
docker run --rm -it --network=host -p 5000:5000 firstContainer
the problem is probably related to networking. so --network=host connects the container to underlying host.
Let's put together a basic web app with Docker Compose and Flask using Redis (we end up with a Python/Flask container and a Redis container all on one host). We do not need custom networking or to design our own networks to run on; Step 3: Run the app using Gunicorn. Now test the Flask app using the Gunicorn. It will run the app on the localhost: port and gives the output “Welcome to the Data Science Learner”. gunicorn "run:app" Step 4: Create a Docker File. After creating the Flask App now you have to create a Docker File to tell the docker to create an image for the
I have the same issue on my Flask App in the docker container. I have used --network=host and resolved the issue.
# docker run -it -p 8080:8080 --network=host mbilgen/metacriticv3:1.0 WARNING: Published ports are discarded when using host network mode * Serving Flask app "main" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: on * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 258-420-336 127.0.0.1 - - [12/May/2019 03:55:08] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [12/May/2019 03:55:13] "GET /games HTTP/1.1" 200 - INFO:werkzeug:127.0.0.1 - - [12/May/2019 03:55:13] "GET /games HTTP/1.1" 200 -
Detailed steps to get your Flask application running in a Docker container. Flask is a microframework for Python, with a basis in Werkzeug and Jinja 2. in a Docker container!' if __name == '__main__': app.run(debug=True, host='0.0.0.0') Flask by default runs on port 5000 (not 8000 like Django or 80 like Apache). app ├── docker-compose.yml ├── flask │ ├── Dockerfile │ ├── .dockerignore │ ├── app │ │ ├── __init__.py │ │ └── views.py │ ├── app.ini │ ├── requirements.txt │ └── run.py ├── nginx │ ├── Dockerfile │ └── nginx.conf ├── .gitignore └── readme.md
Also it's not as resource intensive as VMs. Now we are going to get started with dockerizing a flask application. host as 0.0.0.0, the reason is that by default when you run the flask application the host defaults to localhost and Your flask app has no clue about which port is put in dockerfile. You can leave your application at the default port and tell docker to expose it on the desired port with the -p option: docker run -i -t -p 6080:5000
This article shows how to containerize Flask and Postgres for local Docker the application to a cloud hosting provider via Docker Compose and Docker Machine. We're now ready to get the containers up and running. (dev) Image cache directory does not exist, creating it at /Users/realpython/.docker/machine/cache. Docker Compose simplifies multi-container Docker environments on a single host. Let’s put together a basic web app with Docker Compose and Flask using Redis (we end up with a Python/Flask container and a Redis container all on one host). We do not need custom networking or to design our own networks to run on; Compose does everything for us!
When running Docker Engine in swarm mode, you can use docker stack deploy If you're not familiar with swarm mode, you might want to read Swarm mode key It consists of a Python app which maintains a counter in a Redis instance and app = Flask(__name__) redis = Redis(host='redis', port=6379) @app.route('/') Dockerize your Flask Application. In this article, we’ll take a look at how to dockerize a Flask application. Flask is a microframework for Python, with a basis in Werkzeug and Jinja 2. The Docker Hub image. Since Docker Hub doesn’t have an official Flask repository (at the time of this writing), we’ll explain how to build our own.
- Try being more explicit:
docker run -p localhost:5000:5000 firstContainer. You can also substitute a specific ip address for
- Are you able to reach your app at 127.0.0.1:5000/hello and how is docker installed/running? What is the value of $DOCKER_HOST?
- @BMitch I was not able to reach 127.0.0.1:5000/hello when it running from docker but when I start the app on my host machine, I was able to hit that endpoint. And regarding the $DOCKER_HOST value, I will have to check on that.
- If you did not set the host to
0.0.0.0programatically, CMD may need to be
flask run --host=0.0.0.0to expose the server. There is also a container running flask behind ngnix out-of-the-box (google: "tiangolo flask docker")
- @ReutSharabani I had to set the host to 0.0.0.0 to get it working. I somehow missed your comment earlier. Feel free to mention that as an answer so that I can accept it.
- When I try this command to start the app on docker, I see that the request will keep waiting for a response from the server i.e. earlier I used to get an immediate error saying that it is unable to connect to the URL. No it keeps on waiting for a response. Any pointers on what is happening under the hood ? Also, after starting the app on docker, I did docker ps in another terminal window. I was able to see this image running but under the ports column, it was showing empty. Is that expected ?
- make sure you have not set port and host in app.run(....) : if name == "main": app.run(host='0.0.0.0', port=80)
- I did set a port number in my app.run() command. I've set it to 5000. Why would it be a problem if I ask my app to listen on a specific port ?
- if you set it to 5000 that is exactly the port number you expose, it will be ok. I thought you might set different port numbers in app.run and docker expose.
- the actual issue is networking. change --network=host to --net=host and if the problem is still there check your iptables (firewall) and remove all rules. if the problem persists give me your code and dockerfile to check on my laptop.