skip to Main Content

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

Answers


  1. 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']
    
    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search