如何使用docker run命令将json文件作为parameter passing

以下是我的Dockerfile内容:

FROM python:2.7-slim # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app ADD . /app RUN pip install numpy==1.12.0 CMD ["python", "t_1.py", "t_1.json"] 

我想在运行时将这个文件(t_1.sjon)作为parameter passing给docker run命令,以便CMD [“python”,“t_1.py”,“RUN TIME ARGUMENT”]。 我试着装卷,但失败,因为JSON文件是独立的,我想作为参数。

请帮忙。

你应该使用的是ENTRYPOINT

 FROM python:2.7-slim # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app ADD . /app RUN pip install numpy==1.12.0 ENTRYPOINT ["python", "t_1.py"] 

现在,当你运行docker命令

 docker run -v ./t_1.json:/data/t_1.json <dockerimage> /data/t_1.json 

这将使它相当于python t_1.py /data/t_1.json

您可以使用bash在Docker容器中运行任何命令。

 docker run <your_image> bash -c "python /app/t_1.json" 

我假设json文件在你有dockerfile的目录中。 所以它被复制到/app的容器中,可以使用容器内的bash命令运行。