简体   繁体   English

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

[英]How to pass json file as an argument using docker run command

Below is my Dockerfile content: 以下是我的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"]

I want to pass this file(t_1.sjon) as argument with docker run command at runtime so that CMD ["python", "t_1.py", "RUN TIME ARGUMENT"]. 我想在运行时使用docker run命令将此文件(t_1.sjon)作为参数传递,以便CMD [“ python”,“ t_1.py”,“ RUN TIME ARGUMENT”]。 I tried mounting volumes but fails as json file is independent and I want as argument. 我尝试挂载卷,但由于json文件是独立的,因此我想作为参数失败。

Please help. 请帮忙。

What you should use is ENTRYPOINT 您应该使用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"]

Now when you run the docker command 现在,当您运行docker命令时

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

This will make it equivalent to python t_1.py /data/t_1.json 这将使其等同于python t_1.py /data/t_1.json

You can use bash to run any command inside docker container. 您可以使用bash在docker容器中运行任何命令。

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

I am assuming that the json file is in the directory where you are having the dockerfile. 我假设json文件位于您拥有dockerfile的目录中。 So it being copied inside the container at /app , can be run using the bash command inside container. 因此,它可以在/app的容器内复制,可以使用容器内的bash命令运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 docker run 将本地文件作为参数传递? - How do I pass a local file as an argument with docker run? 如何从 docker run 命令将命令行参数传递给 python 脚本? - How to pass command line argument to python script from docker run command? 如何使用python将json字符串作为命令行参数传递 - How to pass json string as a command line argument using python 如何使用getopt在命令行中传递参数 - how to pass argument in command line using getopt 当列表来自 json 文件时,如何在 Python 中将整个列表作为命令行参数传递 - How to pass an entire list as command line argument in Python when the list comes from a json file 如何使用自定义参数在Docker中运行命令? - How to run a command in Docker using custom arguments? 如何在 Docker 中将参数(文件路径)传递给 python 应用程序 - how to pass argument (file path) to python app in Docker 如何使用气流的 DockerOperator 将参数传递给 docker 容器 - How to pass an argument to a docker container using airflow's DockerOperator 如何通过将外部文件作为参数来运行 docker 容器 - How to run docker container by giving an external file as argument 如何在linux命令中从文本文件传递参数 - How to pass argument from a text file in mid of linux command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM