我正在尝试将后端上传到Google Cloud Run。我正在使用具有2个组件的Docker-Compose:一个Golang服务器和一个Postgres DB。
当我在本地运行Docker-Compose时,一切正常!当我使用
上传到Gcloud时 gcloud builds submit . --tag gcr.io/BACKEND_NAME
gcloud run deploy --image gcr.io/BACKEND_NAME --platform managed
Gcloud的运行状况检查失败,卡在Deploying... Revision deployment finished. Waiting for health check to begin.
上并抛出Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.
我了解到Google Cloud Run提供了一个PORT
环境变量,我试图在我的docker-compose.yml
中进行解释。但是命令仍然失败。我没主意了,这里可能出什么问题了?
这是我的docker-compose.yml
version: '3'
services:
db:
image: postgres:latest # use latest official postgres version
container_name: db
restart: "always"
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=db
volumes:
- database-data:/var/lib/postgresql/data/ # persist data even if container shuts down
api:
container_name: api
depends_on:
- db
restart: on-failure
build: .
ports:
# Bind GCR provided incoming PORT to port 8000 of our api
- "${PORT}:8000"
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=db
volumes:
database-data: # named volumes can be managed easier using docker-compose
和api
容器是Golang二进制文件,它在调用http.ListenAndServe(":8000", handler)
之前等待与Postgres DB建立连接。