【问题标题】:Docker `CMD a b c` VS `CMD ["a", "b", "c"]`Docker `CMD a b c` VS `CMD ["a", "b", "c"]`
【发布时间】:2017-02-20 19:46:17
【问题描述】:

当命令指定为 CMD a b c 时,一切都按预期工作,而使用 CMD ["a", "b", "c"] 指定相同的命令 - 它会产生意想不到的结果。 我正在尝试在 docker 中运行 Jupyter (ipython)。我的CMD 命令是启动它。似乎无论我如何指定它 - Jupyter 都会启动。但是,只有当我将其指定为 CMD a b c 时,jupyter 才能真正正常工作并且可以启动内核。 在这种情况下,“新建笔记本”命令有效

    FROM debian:stable
    RUN apt-get update && apt-get install -y wget bzip2
    RUN wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh && \
        bash Miniconda2-latest-Linux-x86_64.sh -b -p /anaconda2
    RUN /anaconda2/bin/conda install jupyter

    #CMD ["/anaconda2/bin/jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--NotebookApp.token=''"]
    CMD /anaconda2/bin/jupyter notebook --port=8888 --no-browser --ip=0.0.0.0 --NotebookApp.token=''

    # docker build -t IMAGE_NAME .
    # docker run --rm -it -p 8888:8888 IMAGE_NAME

在这种情况下,“新建笔记本”命令不起作用

    FROM debian:stable
    RUN apt-get update && apt-get install -y wget bzip2
    RUN wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh && \
        bash Miniconda2-latest-Linux-x86_64.sh -b -p /anaconda2
    RUN /anaconda2/bin/conda install jupyter

    CMD ["/anaconda2/bin/jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--NotebookApp.token=''"]
    #CMD /anaconda2/bin/jupyter notebook --port=8888 --no-browser --ip=0.0.0.0 --NotebookApp.token=''

    # docker build -t IMAGE_NAME .
    # docker run --rm -it -p 8888:8888 IMAGE_NAME

我真的很困惑,想不出有什么区别!

【问题讨论】:

    标签: docker ipython jupyter


    【解决方案1】:

    shell form (CMD a b c) 使用经过解析的字符串调用 shell,而 exec 形式 (CMD [a, b, c]) 使用指定的参数直接启动可执行文件。

    由于 exec 表单中没有 shell 解析(在这种情况下,它删除了 shell 表单中的空引号),最后一个参数应该是,例如,"--NotebookApp.token="。这是给程序的--NotebookApp.token=,没有两个撇号。


    来自手册:

    与 shell 形式不同,exec 形式不调用命令 shell。这意味着不会发生正常的外壳处理。例如,CMD [ "echo", "$HOME" ] 不会对 $HOME 进行变量替换。如果你想要 shell 处理,那么要么使用 shell 形式,要么直接执行 shell,例如:CMD [ "sh", "-c", "echo $HOME" ]。当使用 exec 形式并直接执行 shell 时,与 shell 形式一样,是 shell 进行环境变量扩展,而不是 docker。

    【讨论】:

    • tbh,我在怀疑类似的事情。我什至试过CMD ["bash", "a", "b", "c"] - 但显然它应该是CMD ["bash", "a b c"]
    • @avloss CMD ["bash", "-c", "a b c"]
    • 谢谢!好吧,实际上CMD ["bash", "-c", "a b c"] 没用!但是CMD ["sh", "-c", "a b c"] 做到了!所以jupyter 似乎依赖于一些env 变量,而它们在bash 中不存在。
    猜你喜欢
    • 1970-01-01
    • 2018-01-29
    • 2011-08-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2015-11-28
    • 2011-05-30
    相关资源
    最近更新 更多