Summarize the problem
I am working on a project to incorporate local python program with docker. The local python program takes PDFs from local directory as input to kick off the program. I think volume would be the solution to share storage between the host and container but I wasn't able to successfully run local python program after mounting the filesystem.
Question: How to run python file in docker after mounting the local filesystem?
Current solution:
This is what I've tried so far:
local file directory:
docker_test_app
┣ .vscode
┃ ┣ .ropeproject
┃ ┃ ┣ config.py
┃ ┃ ┗ objectdb
┃ ┗ settings.json
┣ src
┃ ┣ .DS_Store
┃ ┣ BankofAmerica_Statement_9.pdf
┃ ┣ Chase_Statement_5.pdf
┃ ┣ Wells_Statement_7.pdf
┃ ┣ requirements.txt
┃ ┗ test.py
┣ .DS_Store
┗ Dockerfile
Dockerfile:
FROM python:3
WORKDIR /app
COPY src/requirements.txt ./
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY src /app
EXPOSE 8080
CMD ["python","test.py"]
A simple python test.py to take PDFs as input:
import fitz
import glob
bank_names_list = ['Chase Bank','bankofamerica','Your Business and Wells Fargo'] # match docs
chase_match = bank_names_list[0]
boa_match = bank_names_list[1]
wells_match = bank_names_list[2]
fileFolder = '/Users/xyz/Desktop/docker_test_app/src/' # host filesystem
subFiles = glob.glob(fileFolder + r'*.pdf')
for i in subFiles:
doc = fitz.open(i)
page = doc.loadPage(0)
pageTextblocks = page.getText('blocks')
pageTextblocks.sort(key=lambda block: block[3])
for block in pageTextblocks:
targetBlock = block[4]
if chase_match in targetBlock:
print('hello Chase')
Docker commands that I ran in vs code terminal:
docker volume create TestVolume
docker build -t pythontest .
docker run -it -v /Users/xyz/Desktop/docker_test_app/src:/projects bash
After running the command, it took me directly to bash terminal and I ran ls projects
where I could see all three PDFs. After I exit out of bash terminal, I ran docker start reverent_curran
and docker exec -it reverent_curran bash
but nothing returned from the python program. I expect to have 'hello Chase' printed.
I feel like I am missing some components here to get it working. What did I missing here? Docs as references: docker docs and another similar question on SO Thanks for any help
EDIT:
Have tried to modify python script as suggested but still nothing returned from the program. What are other options here to achieve the goal?
EDIT 2:
Here is the error reproduced from running docker exec -it nifty_keller bash projects/test.py
projects/test.py: line 3: import: command not found
projects/test.py: line 4: import: command not found
projects/test.py: line 5: import: command not found
projects/test.py: line 6: import: command not found
projects/test.py: line 8: bank_names_list: command not found
projects/test.py: line 9: chase_match: command not found
projects/test.py: line 10: boa_match: command not found
projects/test.py: line 11: wells_match: command not found
projects/test.py: line 13: fileFolder: command not found
projects/test.py: line 15: syntax error near unexpected token `('
projects/test.py: line 15: `subFiles = glob.glob(fileFolder + r'*.pdf') '