skip to Main Content

I have a problem with loading a file to a Docker image. I would like to pass the file path as an argument and then be able to access (read) it from inside the image.

model.py

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('--image-path', type=str)
  parser.add_argument('--model-dir', type=str)
  args, _ = parser.parse_known_args()

  sample_image = args.image_path
  print("Sample:", sample_image)
  print("Model:", args.model_dir)
  print("Done.")

Dockerfile

FROM python:3.10

WORKDIR /usr/src/app

RUN apt update && apt install -y python3-pip

COPY model.py model.pth.tar ./

ENTRYPOINT ["python3", "./model.py", "--model_dir"]
CMD ["./model.pth.tar"]

From my understanding the combination of these ENTRYPOINT and CMD should have worked. But I have no luck.

I managed to pass the required image (picture) by mounting:

docker build -t my_model --progress=plain .

docker run -v /tmp:/tmp/myfilesdocker my_model --image-path=happy.jpg

However, I don’t want to do that with my model.pth.tar (model weight). I want it to be passed by default (or any other way that works and doesn’t require mounting).

2

Answers


  1. In general, containers don’t have access to the host file system without being given explicit access through mounts. This is for security reasons.

    The only way I can think of giving a container access to a file without mounting the file, is to pipe it into the container and then read it from stdin inside the container. You need to use the -i option on docker run when you do that.

    As an example, you can take the contents of /etc/os-release on the host and pass it to cat inside the container like this

    cat /etc/os-release | docker run --rm -i debian cat
    

    The container will then print out the contents of the host’s /etc/os-relase.

    Of course, this way only lets you read the content of the file. You can’t update it. But if I’m reading your question correctly, you don’t need to update the model data.

    Login or Signup to reply.
  2. Your Dockerfile has a typo in the --model_dir part. Try the following
    ENTRYPOINT ["python3", "./model.py", "--model-dir"]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search