Default command for a docker image

679 views Asked by At

How can I know the default command a container runs using a base image without actually creating a new container from the image?

is some concept I am missing here? I am writing a dockerfile using a base image but I dont know what to put in CMD section.

2

There are 2 answers

0
Rohith V On BEST ANSWER

You can always do docker image inspect <image> on the terminal where you will get a bunch of information.

I run docker image inspect busybox (Just some random image), and I got the following result

[
    {
        
        "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"
        }
    }
]

Here you can see the CMD field

0
Philipp Claßen On

docker image inspect <IMAGE> will give you more details.

For example:

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

So, for Nginx it is [nginx, -g, daemon off;]

Or, if you have jqinstalled, the output for ubuntu:20.04:

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