The CMD instruction

The CMD instruction can run any command (or application), which is similar to the RUN instruction. However, the major difference between these two is the time of execution. The command supplied through the RUN instruction is executed during the build time, whereas the command specified by the CMD instruction is executed when the container is launched from the newly created image. Thus, the CMD instruction provides a default execution for this container. However, it can be overridden by the docker run subcommand arguments. When the application terminates, the container will also terminate along with the application and vice versa.

The CMD instruction has three types of syntax, as shown here:

  • The first syntax type is the shell type, as shown here:
      CMD <command>

Here, <command> is the shell command, which has to be executed during the launch of the container. If this type of syntax is used, then the command is always executed using /bin/sh -c.

  • The second type of syntax is exec or the JSON array, as shown here:
      CMD ["<exec>", "<arg-1>", ..., "<arg-n>"] 

Here, the code terms mean the following:

    • <exec>: This is the executable, which is to be run during the launch of the container
    • <arg-1>, ..., <arg-n>: These are the variable numbers (zero or more) of arguments for the executable
  • The third type of syntax is also exec or the JSON array, which is similar to the previous type. However, this type is used for setting the default parameters to the ENTRYPOINT instruction, as shown here:
      CMD ["<arg-1>", ..., "<arg-n>"] 

Here, the code terms mean the following:

<arg-1>, ..., <arg-n>: These are the variable numbers (zero or more) of arguments for the ENTRYPOINT instruction, which will be explained in the next section.

Syntactically, you can add more than one CMD instruction in Dockerfile. However, the build system will ignore all the CMD instructions except for the last one. In other words, in the case of multiple CMD instructions, only the last CMD instruction will be effective.

Here, in this example, let's craft an image using Dockerfile with the CMD instruction for providing a default execution and then launching a container using the crafted image. The following is Dockerfile with a CMD instruction to echo a text:

######################################################## 
# Dockerfile to demonstrate the behavior of CMD
########################################################
# Build from base image busybox:latest
FROM busybox:latest
# Author: Dr. Peter
MAINTAINER Dr. Peter <[email protected]>
# Set command for CMD
CMD ["echo", "Dockerfile CMD demo"]

Now, let's build a Docker image using the docker build subcommand and cmd-demo as the image name. The docker build system will read the instruction from the Dockerfile that is stored in the current directory (.), and craft the image accordingly, as shown here:

$ sudo docker build -t cmd-demo .

Having built the image, we can launch the container using the docker run subcommand, as shown here:

$ sudo docker run cmd-demo
Dockerfile CMD demo

Cool, isn't it? We have given a default execution for our container and our container has faithfully echoed Dockerfile CMD demo. However, this default execution can be easily overridden by passing another command as an argument to the docker run subcommand, as shown in the following example:

$ sudo docker run cmd-demo echo Override CMD demo
Override CMD demo
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset