Python 如何使用docker run命令将json文件作为参数传递

Python 如何使用docker run命令将json文件作为参数传递,python,file,docker,Python,File,Docker,以下是我的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"] 我想在运行时通过docke

以下是我的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"]
我想在运行时通过docker run命令将此文件(t_1.sjon)作为参数传递,以便CMD[“python”、“t_1.py”、“运行时参数”]。我尝试装载卷,但失败了,因为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
docker run-v./t_1.json:/data/t_1.json/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"
docker运行bash-c“python/app/t_1.json”

我假设json文件位于dockerfile所在的目录中。因此,在
/app
处复制到容器内的文件可以在容器内使用bash命令运行。

是将“t_1.json”存储在Docker映像内,还是希望将文件本身(而不仅仅是文件名)作为运行时参数传递?@user3758302:我希望传递json文件(每次文件内容可能不同)虽然文件名相同,但它本身作为运行时参数,但cmd应该是python t_1.py t_1.json。键是装入卷或“文件”卷(即docker run-v./t_1.json:/data/t_1.json,如Tarun Lalwani的回答所述)。如果所有内容都保持不变,只有文件内容发生更改,则入口点不会造成损害,但不是强制性的。