结构如下:
âââ data_storage_service
â  âââ db
â  âââ Dockerfile
â  âââ initial-db.sqlite
âââ docker-compose.yml
âââ script_execution_service
âââ app
âââ Dockerfile
âââ main.py
âââ requirements.txt
1) data_storage_service中的Dockerfile是:“
FROM alpine:latest
RUN apk --no-cache add sqlite
WORKDIR /db
COPY initial-db.sqlite /db/
CMD ["tail", "-f", "/dev/null"]
-
initial-db.sqlite仍然为空
-
docker-compose.yml如下所示:
version: '3.8'
services:
data_storage_service:
build:
context: ./data_storage_service
container_name: data_storage_service
volumes:
- ./data_storage_service/db:/db
script_execution_service:
build:
context: ./script_execution_service
container_name: script_execution_service
depends_on:
- data_storage_service
volumes:
- ./script_execution_service/app:/app
environment:
- DB_PATH=/db/initial-db.sqlite
-
script_execution_service中的Dockerfile:
FROM python:3.10
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]
-
main.py如下所示:
import os
import sqlite3
def fetch_data_from_db(db_path, table_name):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM {table_name}")
rows = cursor.fetchall()
conn.close()
return rows
def main():
db_path = os.environ.get('DB_PATH')
ventes = fetch_data_from_db(db_path, 'ventes')
print(ventes)
if __name__ == '__main__':
main()
当我执行docker compose up时,我会出现以下错误:
script_execution_service | python: can't open file '/app/main.py': [Errno 2] No such file or directory
有人能帮我吗,我是新手。谢谢