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?

Jeanne Chaverot
  • 147
  • 3
  • 11
  • 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. – David Maze Feb 07 '23 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? – David Maze Feb 07 '23 at 16:57

2 Answers2

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
Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
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']
morkot
  • 86
  • 3