我尝试通过容器注册表将一个快速API待办事项应用程序部署到Heroku上。当我构建docker映像并在本地运行它时。我可以在http://localhost:8001/docs中访问我的swagger。但当我部署到Heroku时,我无法访问,并收到以下错误:错误:Exec格式错误
这里是我的main.py
from fastapi import FastAPI,Depends
from fastapi.middleware.cors import CORSMiddleware
from db.db import get_db
from bson.objectid import ObjectId
from models.todo import Todo
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
def root():
return {"message": "Hello World"}
@app.get("/api/todo",response_model=Todo)
async def get_todo(db=Depends(get_db)):
data = await db.todo.find()
result: Todo = Todo(**data)
return result
@app.get("/api/todo/{todo_id}",response_model=Todo)
async def get_todo(todo_id: str,db=Depends(get_db)):
data = await db.todo.find_one({"_id": ObjectId(todo_id)})
result: Todo = Todo(**data)
return result
@app.post("/api/todo")
async def create_todo(db=Depends(get_db),payload:Todo=None):
result = await db.todo.insert_one(payload.dict())
return {"message": "Todo created successfully", "todo_id": str(result.inserted_id)}
@app.put("/api/todo/{todo_id}")
async def update_todo(todo_id: str,db=Depends(get_db),payload:Todo=None):
result = db.todo.update_one({"_id": ObjectId(todo_id)}, {"$set": payload.dict()})
return {"message": "Todo updated successfully", "todo_id": str(result.inserted_id)}
@app.delete("/api/todo/{todo_id}")
async def delete_todo(todo_id: str,db=Depends(get_db)):
result = db.todo.delete_one({"_id": ObjectId(todo_id)})
return {"message": "Todo deleted successfully", "todo_id": str(result.inserted_id)}
和我的Dockerfile
FROM python:3.8
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
# EXPOSE 8000
RUN chmod +x run.sh
ENTRYPOINT ["/bin/bash", "-c" ,"./run.sh"]
我尝试使用CMD而不是ENTERYPOINT,但没有成功。我还尝试使用
CMD ["uvicorn", "main:app","--proxy-headers", "--host", "${HOST}", "--port", "${PORT}"]
run.sh
#!/bin/sh
export APP_MODULE=${APP_MODULE-main:app}
export HOST=${HOST:-0.0.0.0}
export PORT=${PORT:-8001}
# run gunicorn
gunicorn --bind $HOST:$PORT "$APP_MODULE" -k uvicorn.workers.UvicornWorker
requirements.txt
fastapi
uvicorn
motor
gunicorn
2条答案
按热度按时间z9zf31ra1#
您正在与Heroku(linux/amd 64)不兼容的平台上本地(Mac?)构建映像在构建时设置平台/将映像推送到Heroku注册表
您也可以将
DOCKER_DEFAULT_PLATFORM
设置为env变量(以避免每次都设置它-注意,所有映像都是linux/amd 64)lvjbypge2#
当服务器部署到heroku时,尝试使用heroku.yml文件而不是
CMD
来启动服务器。我无法连接到我的服务器,直到我切换到
heroku.yml
。