Docker `CMD a b c` VS `CMD ["a", "b", "c"]`

标签 docker ipython jupyter

当命令指定为 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

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

最佳答案

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

由于 exec 形式中没有 shell 解析(在本例中,删除了 shell 形式中的空引号),因此最后一个参数应该是,例如,"--NotebookApp.token="。这以 --NotebookApp.token= 形式提供给程序,不带两个撇号。


来自手册:

Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.

关于Docker `CMD a b c` VS `CMD ["a", "b", "c"]`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42353048/

相关文章:

python - 单元格运行时更改 ipython 中的输出

python - Matplotlib 在 Ipython 中不显示交互式图表

python - 为当前 python 版本 2.x 安装 ipython

python - 是否可以在 nbviewer 中使用 jupyter_contrib_nbextensions?

docker - docker-compose.yml中的图像从何而来?

Docker Compose 如何通过构建扩展服务以使用图像代替

python - Python 中的显式(前向)和隐式(后向)欧拉方法

pandas - 将 Pandas 数据帧转换为具有多个键的字典

windows - 无法在 Windows 7 上的 docker 中运行默认的 hello-world 程序

docker-compose - 添加失败 : Forbidden path outside the build context