我想使用fastapi和redis从Python代码中构建并运行一个docker镜像。为此,我已经使用fastapi和redis在Python中编写了我的API,并安装了docker和docker-compose。但是,我没有设法将redis集成到我的docker镜像中(这是我第一次尝试创建docker镜像)。
我设法理解了如何在Docker容器中使用Python,并将fastapi添加到所包含的Docker中,但对于uvicorn和redis,这似乎更难,我不明白。
更准确地说,终端指示:“Uvicorn运行在http://0.0.0.0:80(按CTRL+C退出)”,但当我去链接https://0.0.0.0/,我得到了一个错误,在我的网络浏览器(“无法连接”)。
而且,为了整合红魔,我迷路了。
下面是我的Python代码(名为“main.py“):
from fastapi import FastAPI
from redis import StrictRedis
app = FastAPI()
#Products are identified by name, so there cannot be two products with the same
#name !
redis_data = StrictRedis(host="localhost", port=6379, db=0)
products = {}
for key in redis_data.scan_iter():
products[key.decode("utf-8")] = eval(redis_data.get(key))
@app.get("/")
async def get_product_information(product_name: str=""):
"""
product is the name of the product about which we want information (quantity
and price).
Returns a dictionary indicating the price and the quantity disponible of the
product.
If product is an empty string, then returns the dictionary containing
information for every referenced product.
If the product in not referenced, then returns a message error.
"""
if product_name == "":
return products
if product_name in products.keys():
return products[product_name]
return "Error : None of the referenced products has the specified name"
@app.post("/")
async def add_product(product: dict):
"""
Adds a product to the database. The product is representend by a dictionary
with tree items :
one indicating the name (key : "name", value : the product's name) ;
one indicating the price (key : "price", value the price) ;
one indicating the quantity (key : "quantity", value : the quantity).
"""
product = product.copy()
for feature in ("name", "quantity", "price"):
if not feature in product.keys():
return f"Error : The product should have a {feature}."
product_name = product.pop("name")
products[product_name] = product
redis_data.set(product_name.encode("utf-8"), str(product).encode("utf-8"))
return "The product has been added."
@app.delete("/")
async def remove_product(product_name: str):
"""
Removes the product product from the database.
"""
if product_name in products.keys():
del products[product_name]
redis_data.delete(product_name)
return "The product has been removed."
return "Error : None of the referenced products has the specified name."
字符串
以下是我的dockerfile的内容:
FROM python:3.9
# Any working directory can be chosen as per choice like '/' or '/home' etc
# I have chosen /usr/app/src
WORKDIR /usr/app/src
#To COPY the remote file at working directory in container
COPY main.py ./
COPY requirements.txt ./
RUN pip install -r requirements.txt
#CMD instruction should be used to run the software
#contained by the image, along with any arguments.
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
型
这是文件“requirements.txt”的内容:
fastapi
uvicorn
型
你能帮帮我吗?
先谢了,
晚上好!
1条答案
按热度按时间vaj7vani1#
在启动Docker容器时,你需要考虑的是端口Map。你告诉fastapi监听端口80:
字符串
但是,Docker本身会通过端口Map暴露它,这意味着它会将容器内部的端口80暴露在本地主机上的其他端口上,如8080。
参见Docker's own documentation:
第一个月
如果你使用Docker compose,你可以这样做:
型
就像their documentation