Python 进程(不包括主进程)在 docker 内无法访问互联网。 我有一个类
BotManager
为每个 AsyncTelebot 启动一个新进程。它有方法:
async def add_bot(self, api_key: str):
if api_key not in self.bots:
if await self._check_bot(api_key):
bot = AsyncTeleBot(api_key)
self.bots[api_key] = bot
terminate_flag = multiprocessing.Event()
self.terminate_flags[api_key] = terminate_flag
process = multiprocessing.Process(target=self.bot_polling_process,
args=(api_key, terminate_flag),
daemon=True)
self.processes[api_key] = process
process.start()
else:
raise InvalidBotKeyException('Неверный API ключ для бота')
else:
raise BotAlreadyWorksException('Такой бот уже работает')
@staticmethod
def bot_polling_process(api_key, terminate_flag):
bot = AsyncTeleBot(api_key)
setup_handlers(bot)
async def polling():
while not terminate_flag.is_set():
try:
await bot.infinity_polling(timeout=10, request_timeout=90, interval=0)
except Exception as e:
print(f"Exception occurred: {e}")
continue
asyncio.run(polling())
如果我在我的电脑上启动应用程序,它可以正常工作,但是当我尝试在我的服务器上启动它时,我会得到以下跟踪:
2024-07-29 17:17:32,856 (asyncio_helper.py:103 MainThread) ERROR - TeleBot: "Unknown error: RuntimeError"
backend | 2024-07-29 17:17:32,857 (async_telebot.py:370 MainThread) ERROR - TeleBot: "Infinity polling exception: Request timeout. Request: method=get url=getMe params=<aiohttp.formdata.FormData object at 0x7fa295e432b0> files=None request_timeout=300"
backend | 2024-07-29 17:17:32,859 (async_telebot.py:372 MainThread) ERROR - TeleBot: "Exception traceback:
backend | Traceback (most recent call last):
backend | File "/usr/local/lib/python3.10/site-packages/telebot/async_telebot.py", line 366, in infinity_polling
backend | await self._process_polling(non_stop=True, timeout=timeout, request_timeout=request_timeout,
backend | File "/usr/local/lib/python3.10/site-packages/telebot/async_telebot.py", line 423, in _process_polling
backend | self._user = await self.get_me()
backend | File "/usr/local/lib/python3.10/site-packages/telebot/async_telebot.py", line 2579, in get_me
backend | result = await asyncio_helper.get_me(self.token)
backend | File "/usr/local/lib/python3.10/site-packages/telebot/asyncio_helper.py", line 154, in get_me
backend | return await _process_request(token, method_url)
backend | File "/usr/local/lib/python3.10/site-packages/telebot/asyncio_helper.py", line 105, in _process_request
backend | raise RequestTimeout("Request timeout. Request: method={0} url={1} params={2} files={3} request_timeout={4}".format(method, url, params, files, request_timeout, current_try))
backend | telebot.asyncio_helper.RequestTimeout: Request timeout. Request: method=get url=getMe params=<aiohttp.formdata.FormData object at 0x7fa295e432b0> files=None request_timeout=300
backend | "
我检查并意识到所有 python 进程(不包括主进程)都没有互联网连接
我的 Dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
docker-compose 文件:
version: '3.8'
networks:
dev:
services:
mongo:
image: mongo:latest
container_name: mongo
environment:
- MONGO_INITDB_DATABASE=telegram_posts
ports:
- "27017:27017"
volumes:
- mongo-data:/data/db
networks:
- dev
redis:
image: redis:latest
container_name: redis
ports:
- "6379:6379"
networks:
- dev
backend:
build:
context: ./telePosting
dockerfile: Dockerfile
container_name: backend
environment:
- MONGO_URL=mongodb://mongo:27017
- MONGO_DB_NAME=telegram_posts
- REDIS_URL=redis
- REDIS_PORT=6379
- PYTHONUNBUFFERED=1
ports:
- "8000:8000"
depends_on:
- mongo
- redis
networks:
- dev
frontend:
build:
context: ./telePostFrontend/vite-project
dockerfile: Dockerfile
container_name: frontend
ports:
- "3000:80"
depends_on:
- backend
networks:
- dev
volumes:
mongo-data:
我该如何解决这个问题?
我尝试 ping 一些站点,发现其他进程没有互联网连接
您已在 docker-compose 文件中创建了一个新网络
dev
并将其分配给所有容器。 它没有通往互联网的路由,因此这些容器只能相互通信。
如果您不熟悉 docker 网络,我建议不要创建新网络,而仅使用默认网络。 通过从 docker-compose 中删除所有
networks:
部分来完成此操作。
您可以在这里找到文档:https://docs.docker.com/compose/networking/
或者,如果您需要默认设置未提供的网络配置,则可以指定网络驱动程序:https://docs.docker.com/network/drivers/