admin 管理员组文章数量: 1086019
I'm developing a small telegram bot+mini app in python that will access postgres database. I've already written some basic code for interacting with db for bot and it seems that mini app will be very similar. The code itself is a simply database.py file which is currently around 180-200 lines of code. I have no previous experience with docker but as far as I know it would be better to separate such things in different containers.
So that brings two questions: Should I separate bot and webapp into two containers? If yes is there a way to use the same database.py file in different containers?
I'm developing a small telegram bot+mini app in python that will access postgres database. I've already written some basic code for interacting with db for bot and it seems that mini app will be very similar. The code itself is a simply database.py file which is currently around 180-200 lines of code. I have no previous experience with docker but as far as I know it would be better to separate such things in different containers.
So that brings two questions: Should I separate bot and webapp into two containers? If yes is there a way to use the same database.py file in different containers?
Share Improve this question asked Mar 28 at 5:20 Pixol20Pixol20 31 bronze badge 1- Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Bot Commented Mar 28 at 7:56
1 Answer
Reset to default 1Yes, you can share database.py
between both applications. To do this, create a common package containing database.py
and import it from both the bot and the app. You can also store other shared files in the same directory to keep everything anized.
Suggested Directory Structure:
-|
|- bot
|- app
|- common
|- database
|- __init__.py
|- database.py
The common
directory will contain shared resources for both the bot and the app. Placing database.py
inside a subdirectory (database/
) helps with future extensions.
The __init__.py
file should contain imports from database.py
, for example
from .database import MyDatabaseWrapper as MyDatabaseWrapper
Docker Configuration:
You need to ensure that the shared common
directory is included in both the bot and app containers.
Bot Dockerfile
# Base setup (FROM, COPY, WORKDIR, etc.)
# Copy shared code
COPY ./common /common
# Copy bot source code
COPY ./bot /bot
# Ensure Python can find both directories
ENV PYTHONPATH=/bot:/common
# Other setup steps...
App Dockerfile
# Base setup (FROM, COPY, WORKDIR, etc.)
# Copy shared code
COPY ./common /common
# Copy app source code
COPY ./app /app
# Ensure Python can find both directories
ENV PYTHONPATH=/app:/common
# Other setup steps...
Importing the Database Module:
Now, both the bot and the app can access the shared database code like this:
from database import MyDatabaseWrapper
This approach keeps your codebase modular and prevents duplication.
本文标签: pythonUsing same code to access database in different containersStack Overflow
版权声明:本文标题:python - Using same code to access database in different containers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744055357a2525750.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论