【问题标题】:What does -c mean as an argument to bash?-c 作为 bash 的参数是什么意思?
【发布时间】:2014-12-24 05:21:52
【问题描述】:
  docker run -d ubuntu:14.04 /bin/bash -c "while true; do echo hello world;done"

  docker run -d ubuntu:14.04 /bin/bash "while true; do echo hello world; done"

我都试过了。

在情况 2 中,容器立即停止。所以docker ps 什么也不返回。而docker ps -a 只返回它自己。

在情况 1 中,docker ps 列出容器。它没有停止。

那么 -c 标志有什么作用呢?

【问题讨论】:

    标签: docker boot2docker


    【解决方案1】:

    来自the bash manual page

    bash 在调用时解释以下选项:

    -c 字符串

    如果存在 -c 选项,则从字符串中读取命令。如果字符串后面有参数,则将它们分配给位置参数,从 $0 开始。

    如果没有-c"while true..." 字符串将被视为bash 打开的文件名。

    【讨论】:

      【解决方案2】:

      -c 标志告诉 Bash 下一个参数是要运行的命令字符串,而不是脚本的文件名。在第一种情况下,Bash 在-c 之后执行下一个参数。在第二种情况下,-c 标志未通过,Bash 尝试运行名为 while true; do echo hello world; done 的脚本,但找不到该脚本,因此退出。

      来自man bash

      -c 字符串

      如果存在 -c 选项,则从 string 中读取命令。如果 string 后面有参数,则将它们分配给位置参数,以 $0 开头。

      【讨论】:

        【解决方案3】:

        在这种情况下(docker run -d ubuntu:14.04 /bin/bash -c "while true; do echo hello world;done"), 你已经使用 docker CMD 运行了 docker,其值为:

        /bin/bash -c "while true; do echo hello world;done"
        

        -c/bin/bash 的参数,而不是 docker 的参数。

        你可以在 docker 外运行 /bin/bash -c "while true; do echo hello world;done" 看看发生了什么。

        【讨论】:

        • 由于容器中有while-loop在运行,所以bash进程不会退出,你可以在docker ps看到这个。在另一种情况下,/bin/bash "while true; do echo hello world; done" 快速退出。
        猜你喜欢
        • 2013-12-13
        • 1970-01-01
        • 2013-09-22
        • 1970-01-01
        • 1970-01-01
        • 2012-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多