0

I have the following docker image

FROM python:3.8-slim WORKDIR /app # copy the dependencies file to the working directory COPY requirements.txt . COPY model-segmentation-512.h5 . COPY run.py . # TODO add python dependencies # install pip deps RUN apt update RUN pip install --no-cache-dir -r requirements.txt RUN mkdir /app/input RUN mkdir /app/output # copy the content of the local src directory to the working directory #COPY src/ . # command to run on container start ENTRYPOINT [ "python", "run.py"] 

and then I would like to run my image using the following command where json_file is a file I can update on my machine whenever I want that will be read by run.py to import all the required parameters for the python script.:

docker run -v /local/input:/app/input -v /local/output:/app/output/ -t docker_image python3 run.py model-segmentation-512.h5 json_file.json 

However when I do this I get a FileNotFoundError: [Errno 2] No such file or directory: 'path/json_file.json' so I think I'm not introducing properly my json file. What should I change to allow my docker image to read an updated json file (just like a variable) every time I run it?

2
  • Can you run this in a Python virtual environment, without involving Docker? Since a Docker container is normally prevented from accessing host files, this class of script that principally reads and writes files is often easier to run outside a container. Commented Feb 7, 2023 at 16:57
  • If that's not an option, does the script somehow know to look for the input file in the /app/input directory? Commented Feb 7, 2023 at 16:57

2 Answers 2

1

Map the json file into the container using something like -v $(pwd)/json_file.json:/mapped_file.json and pass the mapped filename to your program, so you get

docker run -v $(pwd)/json_file.json:/mapped_file.json -v /local/input:/app/input -v /local/output:/app/output/ -t docker_image python3 run.py model-segmentation-512.h5 /mapped_file.json 
Sign up to request clarification or add additional context in comments.

Comments

0

I think you are using ENTRYPOINT in the wrong way. Please see this question and read more about ENTRYPOINT and CMD. In short, what you specify after image name when you run docker, will be passed as CMD and means will be passed to the ENTRYPOINT as a list of arguments. See the next example:

Dockerfile:

FROM python:3.8-slim WORKDIR /app COPY run.py . ENTRYPOINT [ "python", "run.py"] 

run.py:

import sys print(sys.argv[1:]) 

When you run it:

> docker run -it --rm run-docker-image-with-json-file-as-variable arg1 arg2 arg3 ['arg1', 'arg2', 'arg3'] > docker run -it --rm run-docker-image-with-json-file-as-variable python3 run.py arg1 arg2 arg3 ['python3', 'run.py', 'arg1', 'arg2', 'arg3'] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.