【发布时间】:2021-12-26 19:46:05
【问题描述】:
我正在尝试将一个用 python 编写并通过 Docker 容器化的简单 Discord 机器人部署到 Google Cloud Run。
Dockerfile 非常简单:
FROM python:3.9
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
ENV PORT 8080
ENV HOST 0.0.0.0
CMD [ "python3", "discord_bot.py"]
我可以在 Docker 容器中本地运行机器人而不会出现问题,还可以通过下面的 cloudmigrate.yaml 文件将其干净地推送到 Google 容器注册表...
steps:
- id: "build image"
name: "gcr.io/cloud-builders/docker"
args: ["build", "-t", "gcr.io/${PROJECT_ID}/${_SERVICE_NAME}", "."]
- id: "push image"
name: "gcr.io/cloud-builders/docker"
args: ["push", "gcr.io/${PROJECT_ID}/${_SERVICE_NAME}"]
substitutions:
_SERVICE_NAME: discord-bot
images:
- "gcr.io/${PROJECT_ID}/${_SERVICE_NAME}"
...但是当我尝试从容器构建 Cloud Run 实例时,出现以下问题:
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
我在日志中遇到的唯一问题如下:
We have successfully logged in as <username redacted>
Cloud Rundiscord-bot {@type: type.googleapis.com/google.cloud.audit.AuditLog, resourceName: namespaces/<project name redacted>/services/discord-bot, response: {…}, serviceName: run.googleapis.com, status: {…}}
{@type: type.googleapis.com/google.cloud.audit.AuditLog, resourceName: namespaces/<project name redacted>/services/discord-bot, response: {…}, serviceName: run.googleapis.com, status: {…}}
奇怪的是,如上图所示,我实际上在日志中收到一条消息,表明初始登录成功完成,但之后它立即爆炸……python 脚本现在是一个非常简单的占位符……
import os
import discord
TOKEN = os.environ['DISCORD_BOT_LEVEL_TOKEN']
client = discord.Client()
@client.event
async def on_ready():
print('We have successfully logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
#print(message)
#print(dir(message))
if message.author == client.user:
return
if message.content.lower() == 'hello':
await message.channel.send(f'Hello, {message.author.display_name}!')
return
if message.content.lower() == 'bye':
await message.channel.send(f'See you later, {message.author.display_name}!')
return
client.run(TOKEN)
注意:我直接在 Cloud Run 设置中添加了环境变量,因此在这种情况下这不是问题。
我确信这很简单,但我已经为此苦苦思考了几个小时了......
【问题讨论】:
-
client.run 创建一个监听 8080 端口的 web 服务?
-
容器镜像应该按照the container runtime contract的要求为64位Linux编译。你能验证它是否是为 64 位 Linux 编译的吗?
标签: python docker google-cloud-platform discord.py google-cloud-run