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.
Related
I am trying to run a flask app using a docker.
Operating system Windows 11, WSL image Ubuntu-20.04.
A simple reproducible example:
https://github.com/Konrad-H/stackoverflow-question
If I run (inside a venv)
$ python app/main.py
the following message appears in the console:
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
And I can successfully connect to the app.
On the other hand, if I try to run the repo using:
$ docker build -t s-o-question:latest .
$ docker run -p 5000:5000 s-o-question
I get the exact same message on the console, but the webpage takes a long time loading and after a while a connection timeout appears.
The error appears both inside WSL2 and inside Windows.
Source Code:
main.py
import logging
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route("/")
def index():
print("Hello World I am Sea")
return "Hello big world"
if __name__ == "__main__":
app.logger = logging.getLogger("audio-gui")
app.run( host='0.0.0.0',port=5000, debug=True)
Dockerfile
FROM python:3.8
# Working Directory
WORKDIR /app
# Copy source code to working directory
COPY . ./app /app/
# Install packages from requirements.txt
# hadolint ignore=DL3013
RUN pip install --no-cache-dir --upgrade pip &&\
pip install --no-cache-dir --trusted-host pypi.python.org -r requirements.txt
EXPOSE 5000
ENTRYPOINT [ "python" ]
CMD [ "app/main.py" ]
I had the same issue. The URL http/localhost:5000/ worked for me (5000 is my exposed port number). you can try and see if it works. Alternatively, you can also try http://<host-ip>:5000/. Note that the host-ip should be IP address of your local machine not the IP address of the docker container.
you can also check this answer
The issue is that you cannot use same port number for ex if your app runs on port 5000 it must be bind to another port like 8080 in docker .
try this docker run -p 8080:5000 s-o-question
I had the same issue on my win 11 device , i worked around this using the above solution.
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.
can't open file '/web/manage.py': [Errno 2] No such file or directory
exited with code 2
NOTE: Tried all similar problems solution posted, did not work.
No matter what I do, not able to get http://localhost/5000 to work. Even if the above error goes away by removing volume and command from docker-container.
Below is docker-compose.yml
services:
web:
build: ./web
command: python /web/manage.py runserver 0.0.0.0:8000
volumes:
- './users:/usr/src/app'
ports:
- 5000:5000
env_file:
- ./.env.dev
Below is Dockerfile:
# pull official base image
FROM python:3.9.5-slim-buster
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt
# copy project
COPY . /usr/src/app/
BELOW IS manage.py:
from flask.cli import FlaskGroup
from project import app
cli = FlaskGroup(app)
if __name__ == "__main__":
cli()
BELOW IS init.py:
from flask import Flask, jsonify
app = Flask(__name__)
#app.route("/")
def hello_world():
return jsonify(hello="world")
Below is the structure:
The ones marked in red appeared when I ran this command: docker-compose build
enter image description here
A couple of changes to do.
The cli command in your docker-compose.yml file needs to be:
command: python /usr/src/app/manage.py run -h 0.0.0.0 -p 8000
There the command name is run and not runserver. Also the host ip to bind and port to listen are configured as different command options.
Also the configured port mapping for the service needs to map to the container port from the command:
ports:
- 5000:8000
In your manage.py module, FlaskGroup should be provided create_app option which is factory not the app instance.
You can implement this as a lambda function.
cli = FlaskGroup(create_app=(lambda:app))
Edit
The source files are not mounted in the container volume that why you're getting "no such file manage.py".
You need to mount your source files in the container volume under /usr/src/app.
volumes:
- './web:/usr/src/app'
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 trying to deploy my app on GAE using their trial version. I was so far successful in creating an app.yaml with a custom settings for flexible environment with python 3.6.
However, while deploying the app, the app builds successfully, however, I keep getting the following error
Updating service [default] (this may take several minutes)...failed.
ERROR: (gcloud.app.deploy) Error Response: [9]
Application startup error:
/bin/sh: 1: exec: gunicorn: not found
Following is the folder hierarchy of files in my project:
Following the code for app.yaml
env: flex
runtime: custom
api_version: 1
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
python_version: 3.6
#handlers:
#- url: /SmsResponse
# script: Twilio_Routing.RecivedSms
#
#- url: /CallResponse
# script: Twilio_Routing.ReceivedCall
I am surely missing out on something and I would really appreciate some help here.
Link to git repo
requirements.txt
Flask==0.10.1
gunicorn==19.3.0
twilio==6.8.4
DockerFile
FROM gcr.io/google-appengine/python
LABEL python_version=python3.6
RUN virtualenv --no-download /env -p python3.6
# Set virtualenv environment variables. This is equivalent to running
# source /env/bin/activate
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH
# Copy the application's requirements.txt and run pip to install all
# dependencies into the virtualenv.
ADD requirements.txt requirements.txt
RUN pip install -r requirements.txt
ADD . /app/
#CMD gunicorn -b :$PORT main:app
ENTRYPOINT [ "python", "Twilio_Routing.py" ]
P.S. After the changes for the requirements.txt, I am getting error 502 Bad Gateway.
Logs showing that the service was executed successfully.
017-12-25 01:29:03 default[20171224t212610] * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
2017-12-25 01:29:03 default[20171224t212610] * Restarting with stat
2017-12-25 01:29:03 default[20171224t212610] * Debugger is active!
2017-12-25 01:29:03 default[20171224t212610] * Debugger PIN: 134-103-452
2017-12-25 01:29:17 default[20171224t212610] * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
2017-12-25 01:29:17 default[20171224t212610] * Restarting with stat
2017-12-25 01:29:17 default[20171224t212610] * Debugger is active!
2017-12-25 01:29:17 default[20171224t212610] * Debugger PIN: 134-103-452
Can someone look at my code in git and tell me what is it that I am missing here?
for me the error was as simple as making sure gunicorn was in requirements.txt
Flask==1.0.2
gunicorn==19.9.0
Note:
I see the OP had added this flag; this is to help others that may be running into exec: gunicorn: not found
A few changes and I was able to run your app in docker.
In Twilio_Routing.py , change host to listen on 0.0.0.0 instead of 127.0.0.1.This is needed to to have the server available externally as well.
Since your app.yaml is already configured, you don't need to customize your Dockerfile as Google App Engine requires. Keep it as your own custom one.Here's what I used:
#Python's Alpine Base Image
FROM python:3.6-alpine3.6
#Installing all python modules specified
ADD requirements.txt requirements.txt
RUN pip install -r requirements.txt
#Copy App Contents
ADD . /app
WORKDIR /app
#Start Flask Server
CMD [ "python","Twilio_Routing.py"]
#Expose server port
EXPOSE 8080
Considering the example shown in the GoogleCloudPlatform/python-runtime page, consider changing your CMD line from:
CMD exec gunicorn -b :$PORT main:app
To:
CMD gunicorn -b :$PORT main:app
I see exec used here only when the base image is the python one, not gcr.io/google-appengine/python.
I also came across this error recently when I tried to run my docker image.
/bin/sh: 1: gunicorn: not found
Simple trick that worked for me
delete the container and the image
restart the docker engine.
and try to create the image again using the below commands
docker build -t <image_name>:<tag_name> .
and try to run using this command.
docker run -p 5000:5000 -e PORT =5000 image id
it worked for me! hope it helps.