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