I have a simple flask app, which runs in a docker container, which I do not want to access via the localhost. Therefore I set the host = "0.0.0.0".
Flask App:
if __name__ == "__main__":
app.run(
debug=True
, use_reloader = False
, host = "0.0.0.0"
)
When I run this locally everything works as expected.
Then I build my Docker with the following parameters:
# tell the port number the container should expose
EXPOSE 5000
# run the application
CMD ["python", "/usr/src/app/main.py" ]
Afterwards I build my docker container with the following command:
docker build -t my_app:latest .
and run it:
docker run -p 5000:5000 my_app:latest
It starts the container:
* 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: on
* Running on all addresses (0.0.0.0)
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://127.0.0.1:5000
* Running on http://172.17.0.2:5000 (Press CTRL+C to quit)
Somehow thecontainer is now running on two adresses and only http://127.0.0.1:5000 is accessible.
I already tried various proposed solution but cannot find a solution for my issue.
Thank you for your help!
Related
I am trying to run a Flask API inside a Docker container. After running the container I get the following on terminal-
* Serving Flask app 'return-nlp' (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 all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://172.17.0.2:3000/ (Press CTRL+C to quit)
On sending a POST request on the given URL, I don't get any response. I have also tried sending the same request to http://127.0.0.1:3000/, my own IP Address but I don't get any response from the container and get the two responses on Postman-
Error: connect ECONNREFUSED 127.0.0.1:3000
Error: connect ETIMEDOUT
thanks to #KalusD. for suggesting using the -p option to publish the port on the host machine, this seems to have solved the issue.
we use the -p option to bind the port from the process running in the container to the port on the host machine
--publish , -p Publish a container's port(s) to the host
Check here for more info from the official docs.
I'm brand new to Docker so I think there's something wrong in my setup.
Here's my app.py (reduced version):
import flask
from flask import request
from flask_cors import CORS, cross_origin
app = flask.Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
#app.route('/', methods=['GET'])
#cross_origin()
def index():
return('Home')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
My Dockerfile:
FROM python:3
WORKDIR /app
ENV FLASK_APP=app.py
COPY ./requirements.txt .
RUN pip3 install -r requirements.txt
COPY . .
CMD ["python3", "app.py"]
I'm building the image with docker build -t flaskapi . and running with
docker run --rm -it -p 80:5000 flaskapi which gives the following output:
* Serving Flask app "app" (lazy loading)
* Environment: development
* Debug mode: on
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://172.17.0.2:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
Visiting localhost in the browser, everything works fine but when trying to vist http://172.17.0.2:5000/ or testing with Postman, I eventually get a timeout error. I feel like it's just a small mistake I've made somewhere but I can't quite see it. What can I do to fix this?
By default, Docker runs in a separate subnet that is not accessible by the outside world. To be able to access the service running inside the container, you will have to map one of your host's ports to the internal docker subnet.
In your run command, you map TCP 80 to TCP 5000 inside the docker network (the -p 80:5000 part). That's why your service is accessible when visiting http://localhost (80 is implied). In essence, any request to your port 80 will end up being served by the service running at http://172.17.0.2:5000/.
The 172.* ip is in Docker's subnet range. To communicate via that, with your current settings, you would have to be in another container that shares the same network as this one.
I have made a small API to connect to a database using Flask.
When I run it I get this output on local (which works fine in postman)
* 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 want to run this file (main.py) on a server that I have at 172.22.98.254. But when I run it there it still gives me this output:
* 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)
So, when I use my postman doing this
where my post URL is http://172.22.98.254:5000/test, How can I use this from the server that I have. I have an ubuntu server.
By default, app.run() hosts server on localhost(127.0.0.1). To make it accessible,
app.run('0.0.0.0', port=5000)
Although, the server bundled with Flask is not for production, it is recommended to use WSGI server(mod_wsgi, nginx, gunicorn, etc.)
https://flask.palletsprojects.com/en/1.0.x/deploying/wsgi-standalone/
I have changed the host default ip address to server or Local network computer ip address. it works fine. My local ip address is 192.168.1.34, using same port as 5000.
app.run(host='192.168.1.34', port=5000)
I have created a simple flask app that is running on a this is the skeleton o the flask app, which by default runs at port 5000:
# Create the application instance
app = connexion.App(__name__, specification_dir="./")
# read the swagger.yml file to configure the endpoints
app.add_api("swagger.yml")
# Create a URL route in our application for "/"
#app.route("/")
def home():
"""
This function just responds to the browser URL
localhost:5000/
:return: the rendered template "home.html"
"""
return render_template("home.html")
if __name__ == "__main__":
app.run(debug=True)
In the Dockerfile I'm exposing the same port:
RUN python3 -m pip install -r requirements.txt
COPY . /app
EXPOSE 5000
Then I run the container as:
sudo docker run -d -p 5000:5000 my_app:latest
and once the container is up, I'm able to acces to app at:
http://localhost:5000
Now, I'm trying to change to port 5100, for that I'm changing:
a) In the Dockerfile:
COPY . /app
EXPOSE 5100
...
b) When I run the container:
sudo docker run -d -p 5100:5100 my_app:latest
But when I try to visit: http://localhost:5100/
The app is not running there
When I do Docker ps this is shown:
EDIT:
I tried changing the flask app:
app.run(host='0.0.0.0', port=5100)
Still not working, this is the screenshot from docker ps:
Not sure if the error is because still says 5000: at the begining:
5000/tcp, 0.0.0.0:5100->5100/tcp romantic_fermi
This is what I get from docker logs...
* Serving Flask app "server" (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://0.0.0.0:5000/ (Press CTRL+C to quit)
You could technically change the default port assigned to the Flask object, but it's simpler to just change the docker mapping.
When you run a command like this:
$ docker run -d -p 5100:5100 my_app:latest
You are saying that you want to forward a port from inside the container (on the right) to your host machine (on the left).
# Left side is your host machine
# Right side is inside of the container
5100:5100
So you could update your run to map to 5000 inside of the container:
$ docker run -d -p 5100:5000 my_app:latest
Then you'll be able to access via http://localhost:5100
PS: If you haven't used docker-compose before, I would highly recommend setting it up after you've worked through this issue. It'll make your life easier in general.
On your .py script ou need to set 5100 port with:
app.run(debug=True,host='0.0.0.0', port=5100)
Everything else you did is correct!
If still your python are listening on port 5000, probably it's the old version.
I'm trying to learn Docker containers with APIs. I have created a simple Hello World python REST API with flask:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(host="127.0.0.1", debug=True, port=8080)
This works when I run the script and go to http://localhost:8080/
This is my Dockerfile:
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /hello_world
# Copy the current directory contents into the container at /app
ADD . /hello_world
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
EXPOSE 8080
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "hello_world.py"]
requirements.txt:
Flask
My current directory contains Dockerfile, hello_world.py and requirements.txt.
I can successfully build the image with docker build -t hello_world ."
Running it with docker run -p 8080:8080 -t hello_world gives me the following output:
Serving Flask app "hello_world" (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: 985-433-141
When I try going to http://127.0.0.1:8080/ I get the "Can't reach this page" error. Do you know what I'm doing wrong? Thank you.