I am unable to rum Docker Container Registry on the Azure App service. I have a flask app and the following is the Dockerfile of it:-
FROM python:3.8-slim-buster
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# copy project
WORKDIR /usr/src/app
COPY . /usr/src/app/
# expose port 80
EXPOSE 80
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:80", "app:app"]
I have deployed the docker image on the Container Registry. I have also set WEBSITES_PORT to 80 under App Service -> Application Settings.
Even after doing that, I get the following error:-
ERROR - Container XYZ didn't respond to HTTP pings on port: 80, failing site start.
I have tried running it locally and it works fine. But, it just does not seem to work on the Azure App service. Any help is highly appreciated.
I don't see an issue in the code you posted but to verify, here is a configuration for a Flask app with a Gunicorn server that works on a containerized Azure App Service:
app.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello_world():
return "<p>Hello World!</p>"
Dockerfile
FROM python:3.8-slim-buster
ADD app.py app.py
ADD requirements.txt requirements.txt
RUN pip install --upgrade pip
RUN python3 -m pip install -r requirements.txt
EXPOSE 80
CMD ["gunicorn", "--bind=0.0.0.0:80", "app:app"]
requirements.txt
flask
gunicorn
I assume you selected "Docker Container" when you created the Azure App Service?
And then simply chose your image?
HelloWorld-1.py
app = Flask(__name__)
#app.route('/')
def printHelloWorld():
print("+++++++++++++++++++++")
print("+ HELLO WORLD-1 +")
print("+++++++++++++++++++++")
return '<h1>Bishwajit</h1>'
# return '<h1>Hello %s!<h1>' %name
if name == '__main__':
app.run(debug='true')
Dockerfile
FROM python:3
ADD HelloWorld-1.py /HelloWorld-1.py
RUN pip install flask
EXPOSE 80
CMD [ "python", "/HelloWorld-1.py"]
Building docker using the below command
docker build -t helloworld .
Running docker image using below command
docker run -d --name helloworld -p 80:80 helloworld
when i run the below command
docker ps -a
i get the below output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cebfe8a22493 helloworld "python /home/HelloW…" 2 minutes ago Up 2 minutes (unhealthy) 0.0.0.0:80->80/tcp helloworld
If I hit in the browser(127.0.0.1:5000), it does not give response,
But when i run the python file individually, it runs properly in the browser.
I reproduced your problem and there were four main problems:
Not importing flask.
Using name instead of __name__
Not assigning the correct port.
Not assigning the host.
This is how your HelloWorld-1.py should look like:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def printHelloWorld():
print("+++++++++++++++++++++")
print("+ HELLO WORLD-1 +")
print("+++++++++++++++++++++")
return '<h1>Bishwajit</h1>'
# return '<h1>Hello %s!<h1>' %name
if __name__ == '__main__':
app.run(host='0.0.0.0')
This is how you Dockerfile should look like:
FROM python:3
ADD HelloWorld-1.py .
RUN pip install flask
CMD [ "python", "/HelloWorld-1.py"]
Then simply build and run:
docker build . -t helloflask
docker run -dit -p 5000:5000 helloflask
Now go to localhost:5000 and it should work.
Additionally: You could actually assign any other port, for example 4444, and then go to localhost:4444:
docker run -dit -p 4444:5000 helloflask
I made a test flask application that looks like the following:
from flask import Flask
from flask_cors import CORS
import os
app = Flask(__name__)
CORS(app)
#app.route('/')
def hello_word():
return 'hello', 200
if __name__ == '__main__':
app.run(threaded=True, host='0.0.0.0', port=int(os.environ.get("PORT", 8080)))
However, if i host this application on Azure Container Instance, the application never "stops". The memory usage is always at around 50mb and I'm constantly getting charged. If I host the same application on Google Cloud run, I'm only charged for the request time (20ms or so). The following is my dockerfile
FROM python:3.9-slim
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
RUN pip install Flask gunicorn
ENV PORT=80
CMD exec gunicorn --bind :$PORT --workers 3 --threads 3 --timeout 100 main:app --access-logfile -
Any thoughts on how to stop the container instance once the request is served on Azure?
Actually, the ACI just run the image for you and nothing else. It means if your image has an application that keeps running, then the ACI keeps running. And it seems you need to schedule to stop the ACI, maybe you can try the Azure logic App. You can use it to create the ACI and then stop it after a period that you need.
The Problem
I want to use Pyro4 for remote procedure calls across multiple containers using docker-compose. Currently, I am just trying to implement a simplified version of the Pyro4 warehouse example that I have setup to run on different machines, instead of the default localhost, since I am using multiple containers.
I can successfully start the Pyro name server in its own container, but in another container I can not publish the Warehouse class and start Pyro's request loop. I am get the error OSError: [Errno 99] Cannot assign requested address.
My attempt and additional information
I am using balena to deploy this to a Raspberry Pi 3 B+, and I have an environment variable (device variable in balena cloud) "PYRO_HOST=pyro-ns" to set the address of the pyro name server.
I see the pyro name server get created
05.02.20 15:27:33 (-0500) pyro-ns Broadcast server running on 0.0.0.0:9091
05.02.20 15:27:33 (-0500) pyro-ns NS running on pyro-ns:9090 (172.17.0.3)
05.02.20 15:27:33 (-0500) pyro-ns Warning: HMAC key not set. Anyone can connect to this server!
05.02.20 15:27:33 (-0500) pyro-ns URI = PYRO:Pyro.NameServer#pyro-ns:9090
However, I am get the error OSError: [Errno 99] Cannot assign requested address when I try to publish the Warehouse class and start Pyro's request loop using
Pyro4.Daemon.serveSimple(
{
Warehouse: "example.warehouse"
},
ns=True, verbose=True)
I get the following
05.02.20 16:52:00 (-0500) container_B Traceback (most recent call last):
05.02.20 16:52:00 (-0500) container_B File "src/container_B_main.py", line 33, in <module>
05.02.20 16:52:00 (-0500) container_B main()
05.02.20 16:52:00 (-0500) container_B File "src/container_B_main.py", line 30, in main
05.02.20 16:52:00 (-0500) container_B ns=True, verbose=True)
05.02.20 16:52:00 (-0500) container_B File "/usr/local/lib/python3.5/site-packages/Pyro4/core.py", line 1204, in serveSimple
05.02.20 16:52:00 (-0500) container_B daemon = Daemon(host, port)
05.02.20 16:52:00 (-0500) container_B File "/usr/local/lib/python3.5/site-packages/Pyro4/core.py", line 1141, in __init__
05.02.20 16:52:00 (-0500) container_B self.transportServer.init(self, host, port, unixsocket)
05.02.20 16:52:00 (-0500) container_B File "/usr/local/lib/python3.5/site-packages/Pyro4/socketserver/threadpoolserver.py", line 134, in init
05.02.20 16:52:00 (-0500) container_B sslContext=sslContext)
05.02.20 16:52:00 (-0500) container_B File "/usr/local/lib/python3.5/site-packages/Pyro4/socketutil.py", line 298, in createSocket
05.02.20 16:52:00 (-0500) container_B bindOnUnusedPort(sock, bind[0])
05.02.20 16:52:00 (-0500) container_B File "/usr/local/lib/python3.5/site-packages/Pyro4/socketutil.py", line 542, in bindOnUnusedPort
05.02.20 16:52:00 (-0500) container_B sock.bind((host, 0))
05.02.20 16:52:00 (-0500) container_B OSError: [Errno 99] Cannot assign requested address
What am I missing that will allow Pyro to work across the multiple containers using docker-compose?
Following is my code:
docker-compose.yml
version: '2'
services:
pyro-ns:
privileged: true
restart: always
build: ./pyro-ns
ports:
- "9090:9090"
container_A:
privileged: true
restart: always
build: ./container_A
depends_on:
- pyro-ns
- container_B
container_B:
privileged: true
restart: always
build: ./container_B
depends_on:
- pyro-ns
pyro-ns
Dockerfile.template
FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run
# enable container init system.
ENV INITSYSTEM on
# use `install_packages` if you need to install dependencies,
# for instance if you need git, just uncomment the line below.
# RUN install_packages git
RUN pip install --upgrade pip
RUN pip install Pyro4 dill
ENV PYRO_SERIALIZERS_ACCEPTED=serpent,json,marshal,pickle,dill
ENV PYTHONUNBUFFERED=0
CMD ["python", "-m", "Pyro4.naming"]
EXPOSE 9090
container_A
Dockerfile.template
FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run
# enable container init system.
ENV INITSYSTEM on
# use `install_packages` if you need to install dependencies,
# for instance if you need git, just uncomment the line below.
# RUN install_packages git
RUN pip install --upgrade pip
RUN pip install Pyro4 dill
# Set our working directory
WORKDIR /usr/src/container_A
# Copy requirements.txt first for better cache on later pushes
COPY requirements.txt requirements.txt
# pip install python deps from requirements.txt on the resin.io build server
RUN pip install -r requirements.txt
# This will copy all files in our root to the working directory in the container
COPY . ./
# main.py will run when container starts up on the device
CMD ["python","-u","src/container_A_main.py"]
container_A_main.py
import Pyro4
import Pyro4.util
import sys
sys.excepthook = Pyro4.util.excepthook
try:
print('Top of container A')
warehouse = Pyro4.Proxy("PYRONAME:example.warehouse")
print('The warehouse contains: ', warehouse.list_contents())
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print(message)
container_B
Dockerfile.template
FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run
# enable container init system.
ENV INITSYSTEM on
# use `install_packages` if you need to install dependencies,
# for instance if you need git, just uncomment the line below.
# RUN install_packages git
RUN pip install --upgrade pip
# Set our working directory
WORKDIR /usr/src/container_B
# Copy requirements.txt first for better cache on later pushes
COPY requirements.txt requirements.txt
# pip install python deps from requirements.txt on the resin.io build server
RUN pip install -r requirements.txt
# This will copy all files in our root to the working directory in the container
COPY . ./
# container_B_main.py will run when container starts up on the device
CMD ["python","-u","src/container_B_main.py"]
container_B_main.py
from __future__ import print_function
import Pyro4
#Pyro4.expose
#Pyro4.behavior(instance_mode="single")
class Warehouse(object):
def __init__(self):
self.contents = ["chair", "bike", "flashlight", "laptop", "couch"]
def list_contents(self):
return self.contents
def take(self, name, item):
self.contents.remove(item)
print("{0} took the {1}.".format(name, item))
def store(self, name, item):
self.contents.append(item)
print("{0} stored the {1}.".format(name, item))
def main():
#Pyro4.config.HOST = "pyro-ns"
Pyro4.Daemon.serveSimple(
{
Warehouse: "example.warehouse"
},
ns=True, verbose=True)
if __name__ == "__main__":
main()
For both container_A and container_B the requirements.txt file is the same.
requirements.txt
Pyro4
After some help from the balena forums, I was able to successfully get my Pyro example to run.
Following are my updated and working files for reference.
---
docker-compose.yml
version: '2'
services:
pyro-ns:
privileged: true
restart: always
build: ./pyro-ns
command: ["--host=pyro-ns"]
container_A:
privileged: true
restart: always
build: ./container_A
depends_on:
- pyro-ns
- container_B
container_B:
privileged: true
restart: always
build: ./container_B
depends_on:
- pyro-ns
pyro-ns
Dockerfile.template
FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run
# enable container init system.
ENV INITSYSTEM on
# use `install_packages` if you need to install dependencies,
# for instance if you need git, just uncomment the line below.
# RUN install_packages git
RUN pip install --upgrade pip
RUN pip install Pyro4 dill
ENV PYRO_SERIALIZERS_ACCEPTED=serpent,json,marshal,pickle,dill
ENV PYTHONUNBUFFERED=0
ENTRYPOINT ["pyro4-ns"]
EXPOSE 9090
container_A
Dockerfile.template
FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run
# enable container init system.
ENV INITSYSTEM on
# use `install_packages` if you need to install dependencies,
# for instance if you need git, just uncomment the line below.
# RUN install_packages git
RUN pip install --upgrade pip
RUN pip install Pyro4 dill
# Set our working directory
WORKDIR /usr/src/container_A
# Copy requirements.txt first for better cache on later pushes
COPY requirements.txt requirements.txt
# pip install python deps from requirements.txt on the resin.io build server
RUN pip install -r requirements.txt
# This will copy all files in our root to the working directory in the container
COPY . ./
# main.py will run when container starts up on the device
CMD ["python","-u","src/container_A_main.py"]
container_A_main.py
import Pyro4
import Pyro4.util
import sys
sys.excepthook = Pyro4.util.excepthook
try:
print('Top of container A')
warehouse = Pyro4.Proxy("PYRONAME:example.warehouse")
print('The warehouse contains: ', warehouse.list_contents())
print('Inifite loop after running the Warehouse class via Pyro4')
while True:
# Infinite loop.
pass
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print(message)
container_B
Dockerfile.template
FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run
# enable container init system.
ENV INITSYSTEM on
# use `install_packages` if you need to install dependencies,
# for instance if you need git, just uncomment the line below.
# RUN install_packages git
RUN pip install --upgrade pip
# Set our working directory
WORKDIR /usr/src/container_B
# Copy requirements.txt first for better cache on later pushes
COPY requirements.txt requirements.txt
# pip install python deps from requirements.txt on the resin.io build server
RUN pip install -r requirements.txt
# This will copy all files in our root to the working directory in the container
COPY . ./
# main.py will run when container starts up on the device
CMD ["python","-u","src/container_B_main.py"]
EXPOSE 9100
container_B_main.py
from __future__ import print_function
import Pyro4
#Pyro4.expose
#Pyro4.behavior(instance_mode="single")
class Warehouse(object):
def __init__(self):
self.contents = ["chair", "bike", "flashlight", "laptop", "couch"]
def list_contents(self):
return self.contents
def take(self, name, item):
self.contents.remove(item)
print("{0} took the {1}.".format(name, item))
def store(self, name, item):
self.contents.append(item)
print("{0} stored the {1}.".format(name, item))
def main():
Pyro4.config.SERIALIZER = 'pickle'
daemon = Pyro4.Daemon(host="container_B", port=9100)
uri = daemon.register(Warehouse)
print('The uri of the registered Warehouse class')
print(uri)
try:
ns = Pyro4.locateNS(host="pyro-ns", port=9090)
print('The located name server')
print(ns)
ns.register("example.warehouse", uri)
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print(message)
daemon.requestLoop()
if __name__ == "__main__":
main()
For both container_A and container_B the requirements.txt file is the same.
requirements.txt
`Pyro4`
I am trying to make a Flask framework with python and trying to host it on Docker.
#importing dependencies
from flask import Flask
#initializing the name of the application
app = Flask(__name__)
#app.route('/')
def hello(parameter_list):
return 'Hello, this is my first try on Docker'
if __name__ == "__main__":
app.run(host="0.0.0.0", debug= True)
I am getting at Line 5 that name 'app' is not defined
what should i do to remove this error?
this is my first time asking a question over here, Please let me know if any other clarification is needed or suggestions for future posts.
Thanks in advance
The error you have shown in the image and the code does not seem matched. to reproduce your error is to pass app to flask object instead of __name__.
Here you go with HelloWorld
FROM python:alpine3.7
RUN pip install flask==0.10.1
COPY . /app
WORKDIR /app
EXPOSE 5000
CMD python app.py
and app.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Welcome to the Data Science Learner!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int("5000"), debug=True)
build
docker build -t flask-test .
run
docker run -it --rm flask-test
You can use the same with Docker compose,
docker-compose rm -f && docker-compose up --build