0

I'm trying to set up imports inside docker container, so they behave the same way as in the manually executed script.

Project tree:

 .
 ┣ StatisticManager
 ┃ ┣ resources
 ┃ ┃ ┣ data.db
 ┃ ┃ ┣ data.csv
 ┃ ┃ ┗ index.html
 ┃ ┣ scraper.py
 ┃ ┣ Dockerfile
 ┃ ┗ requirements.txt
 ┣ WebService
 ┃ ┣ templates
 ┃ ┃ ┗ plot.html
 ┃ ┣ app.py
 ┃ ┣ Dockerfile
 ┃ ┗ requirements.txt
 ┗ docker-compose.yaml

I'm using from StatisticManager.scraper import Database in app.py

That's where ModuleNotFoundError occurs after the container is composed up with docker-compose.yaml

StatisticManager/Dockerfile:

FROM python:3.9.1
RUN mkdir -p /usr/src/app/
WORKDIR /usr/src/app/
COPY . /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python", "scraper.py"]

WebService/Dockerfile:

FROM python:3.9.1
RUN mkdir -p /usr/src/app/
WORKDIR /usr/src/app/
COPY . /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]

docker-compose.yaml:

version: "3"
services:
  scraper:
    build: StatisticManager/
    restart: always
    volumes:
      - db_volume:/usr/src/app
  web_service:
    build:
      context: WebService/
    restart: always
    ports:
      - 5000:5000
    environment:
      - TZ=Europe
    depends_on:
      - "scraper"
volumes:
  db_volume:
dmain
  • 1
  • is `StatisticManager` a pip installed module, or wrote by yourself? – Lei Yang Mar 15 '22 at 12:28
  • wrote it by myself – dmain Mar 15 '22 at 12:30
  • normally, you can only import file beside your calling script. if the called one (`StatisticManager `) is in topper folder, then you need add that parent folder to `sys.path` – Lei Yang Mar 15 '22 at 12:33
  • Does this answer your question? [Python: 'ModuleNotFoundError' when trying to import module from imported package](https://stackoverflow.com/questions/54598292/python-modulenotfounderror-when-trying-to-import-module-from-imported-package) – Lei Yang Mar 15 '22 at 12:33

1 Answers1

2

app.py is running in the web_service container... you specified context: WebService/ for building that container.

This means that when Docker is building that container image it will only look in the WebService directory when copying files - hence why it didn't include the StatisticsManager files.

Have a look into "Docker build contexts" for more information on how this works.