最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Using same code to access database in different containers - Stack Overflow

programmeradmin3浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

Yes, 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.

发布评论

评论列表(0)

  1. 暂无评论