我已将Flask应用迁移到FastApi应用,并尝试将新的fastapi应用部署为使用Dockerfile运行的coud,但由于端口问题,我遇到了错误。
我已经尝试了所有的解决方案,但没有工作之前,这样的错误,我也尝试了许多不同的方式来编写dockerfile,但我有同样的问题。
最后一次尝试使用FastApi Documentation创建我的docker文件,它也不起作用。
我的Dockerfile:
# Start from the official slim Python base image.
FROM python:3.9-slim
# Set the current working directory to /code.
#This is where we'll put the requirements.txt file and the app directory.
WORKDIR /code
# Copy the file with the requirements to the /code directory.
# Copy only the file with the requirements first, not the rest of the code.
# As this file doesn't change often, Docker will detect it and use the cache for this step, enabling the cache for the next step too.
COPY ./requirements.txt /code/requirements.txt
# Install the package dependencies in the requirements file.
# The --no-cache-dir option tells pip to not save the downloaded packages locally,
# as that is only if pip was going to be run again to install the same packages,
# but that's not the case when working with containers.
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
# As this has all the code which is what changes most frequently the Docker
# cache won't be used for this or any following steps easily
COPY ./app /code/app
# Because the program will be started at /code and inside of it is the directory ./app with your code,
# Uvicorn will be able to see and import app from app.main.
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
我也试着在main中配置运行端口:
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=int(os.environ.get('PORT', 8080)), log_level="info")
我正在使用cloudbuild.yaml
文件部署应用程序:
steps:
# Build the container image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/cog-dev/new-serving', '.']
# Push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/cog-dev/new-serving']
# Deploy container image to Cloud Run
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args: ['run', 'deploy', 'new-serving', '--image', 'gcr.io/cog-dev/new-serving', '--region', 'europe-west1', '--allow-unauthenticated', '--platform', 'managed']
# Store images in Google Artifact Registry
images:
- gcr.io/cog-dev/new-serving
在stackoverflow上的大多数解决方案都已经尝试过,甚至改变了端口号
**更新:**执行Use Google Cloud user credentials when testing containers locally后,我在本地测试docker镜像,得到如下错误:
File "/code/./app/endpoints/campaign.py", line 11, in <module>
from app.services.recommend_service import RecommendService
File "/code/./app/services/recommend_service.py", line 19, in <module>
datastore_client = datastore.Client()
File "/usr/local/lib/python3.9/site-packages/google/cloud/datastore/client.py", line 301, in __init__
super(Client, self).__init__(
File "/usr/local/lib/python3.9/site-packages/google/cloud/client/__init__.py", line 320, in __init__
_ClientProjectMixin.__init__(self, project=project, credentials=credentials)
File "/usr/local/lib/python3.9/site-packages/google/cloud/client/__init__.py", line 271, in __init__
raise EnvironmentError(
OSError: Project was not passed and could not be determined from the environment.
1条答案
按热度按时间ih99xse11#
这里也有同样的问题,通过将
timeout-keep-alive
参数添加到0来解决:CMD exec uvicorn app.main:app --workers 1 --timeout-keep-alive 0 --port 8080 --host 0.0.0.0