Docker传递命令行参数

时间:2020-02-26 15:14:36

标签: python docker

我真的只想通过docker run传递参数 我的Dockerfile:

FROM python:3

# set a directory for the app
WORKDIR /usr/src/app

# copy all the files to the container
COPY . .

# install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# tell the port number the container should expose
EXPOSE 5000

# run the command
CMD ["python", "./app.py"]

我的python文件:

import sys
print(sys.argv)

我尝试过:

docker run myimage foo

我遇到一个错误:

  flask-app git:(master) ✗ docker run myimage foo
docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"foo\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled

1 个答案:

答案 0 :(得分:2)

docker run 命令的末尾编写 foo 时,您将覆盖整个命令。因此,代替

python app.py

您致电

foo

使用参数调用脚本的正确方法是:

docker run myimage python app.py foo

或者,您可以使用ENTRYPOINT代替CMD,然后您的 docker run 命令可能在映像名称之后仅包含 foo

Dockerfile:

FROM python:3

# set a directory for the app
WORKDIR /usr/src/app

# copy all the files to the container
COPY app.py .

# run the command
ENTRYPOINT ["python", "./app.py"]

称呼它:

docker run myimage foo
相关问题