docker 图像的默认命令

Default command for a docker image

我如何知道容器使用基础镜像运行的默认命令,而无需从该镜像实际创建新容器?

我在这里缺少一些概念吗?我正在使用基本图像编写一个 dockerfile,但我不知道在 CMD 部分放什么。

docker image inspect <IMAGE>将为您提供更多详细信息。

例如:

$ docker pull nginx
$ docker image inspect nginx:latest
...
"0":
  "Config": {
    ...
    "Cmd": [
      "nginx",
       "-g",
       "daemon off;"
    ],
    ...
}

因此,对于 Nginx,它是 [nginx, -g, daemon off;]

或者,如果您安装了 jqubuntu:20.04 的输出:

$ docker pull ubuntu:20.04
$ docker image inspect ubuntu:20.04 | jq '.[0].Config.Cmd'
[
  "/bin/bash"
]

您可以随时在终端上执行 docker image inspect <image>,您将获得大量信息。

I 运行 docker image inspect busybox(只是一些随机图像),我得到了以下结果

[
    {
        
        "Parent": "",
        "Comment": "",
        "Created": "2021-05-04T00:19:49.531663542Z",
        "Container": "22c781af50cbbf2c31a3fb57c21d4dcfb266d6691441657088db6750d4068d22",
        "ContainerConfig": {
            "Hostname": "22c781af50cb",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"sh\"]"
            ],
            
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "19.03.12",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "sh"
            ],
            
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 1235829,
        "VirtualSize": 1235829,
        "GraphDriver": {
            "Data": {
      },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

在这里您可以看到 CMD 字段