Detailed explanation of the difference between run/cmd/entrypoint in docker

Detailed explanation of the difference between run/cmd/entrypoint in docker

In Dockerfile, run, cmd, and entrypoint can all be used to execute commands. Here are their main uses:

  • The run command executes commands and creates a new image layer, usually used to install software packages
  • The cmd command sets the default command and its parameters to be executed after the container is started, but the command set by CMD can be replaced by the command line parameters after the docker run command
  • The entrypoint configuration command to be executed when the container is started will not be ignored and will be executed, even if other commands are specified when running docker run .

Run commands in Shell and Exec formats

We can specify the commands to be run by run, cmd, and entrypoint in the following two formats:

  • Shell format: . For example: yum install -y wget
  • Exec format: ["executable", "param1", "param2", …]. For example: [“yum”, “install”, “-y”, “wget”]

It is recommended to use the exec format for cmd and entrypoint because the instructions are more readable and easier to understand, while both formats are acceptable for run.

Exec format pitfalls

The contents of the dockerfile are as follows:

env name morris

entrypoint ["echo", "$name"]

This way of writing will only print out $name and will not replace the variable because it is only executing the echo command, not the shell. This means that we are not executing echo in the shell, but simply executing echo, so the variable will not be replaced.

To change to an executable shell, you need to rewrite it into the following form

env name morris

entrypoint ["/bin/bash", "-c", "echo $name"]

Run Command

The run command is often used to install applications and software packages. run executes the command on top of the current image and creates a new image layer. Dockerfile often contains multiple run instructions. Here is an example:

run yum update && yum install -y \  
 bzr \
 cvs \
 git \
 mercurial \
 subversion

yum update and yum install are executed in one run command, which ensures that the latest package is installed each time. If yum install is executed in a separate run, the image layer created by yum update will be used, which may be cached long ago.

cmd command

The cmd directive allows the user to specify the default command executed by the container. This command is run when the container is started and no other command is specified with docker run. Here is an example:

cmd echo "Hello world"

Running the container docker run -it [image] will output:

Hello world

But when a command is added after it, such as docker run -it [image] echo hi , cmd will be ignored and the command echo hi will be executed:

hi

If there are multiple cmd commands, only the last cmd command will be executed.

entrypoint command

The exec format of entrypoint is used to set the command and its parameters to be executed when the container is started. Additional parameters can be provided through the cmd command or command line parameters. The parameters in entrypoint are always used, which is different from the cmd command. Here is an example:

entrypoint ["echo", "Hello"]

When the container is started via docker run -it [image] , the output is:

Hello

If started by docker run -it [image] morris , the output is:

Hello morris

Let's look at another example. The Dockerfile is:

entrypoint ["echo", "Hello"]
cmd ["world"]

When the container is started via docker run -it [image] , the output is:

Hello world

If you start it with docker run -it [image] morris , the output is:

Hello morris

The parameters in entrypoint are always used, while the additional parameters of cmd can be dynamically replaced when the container is started.

Similarly, if there are multiple entrypoint commands, only the last entrypoint command will be executed.

Summarize

  • Use the run command to install applications and software packages and build images.
  • If the purpose of the Docker image is to run an application or service, such as running a MySQL, the Exec format entrypoint instruction should be used first. cmd can provide additional default parameters for entrypoint, and the default parameters can be replaced by the docker run command line.
  • If you want to set a default startup command for the container, use the cmd command. Users can replace this default command in the docker run command line.

This is the end of this article about the differences between run/cmd/entrypoint in docker. For more relevant docker run/cmd/entrypoint content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Solution to the problem that docker CMD/ENTRYPOINT executes the sh script: not found/run.sh:
  • Execute multiple commands after docker run
  • Ubuntu vps installs docker and reports an error: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Problem solved
  • Docker uses CMD or ENTRYPOINT commands to start multiple services at the same time
  • Detailed explanation of CMD and ENTRYPOINT commands in Dockerfile

<<:  Summary of the three stages of visual designer growth

>>:  Detailed explanation of the mysql database LIKE operator in python

Recommend

isPrototypeOf Function in JavaScript

Table of contents 1. isPrototypeOf() Example 1, O...

Detailed tutorial on installing centos8 on VMware

CentOS official website address https://www.cento...

Example of how to use CSS3 to layout elements around a center point

This article introduces an example of how CSS3 ca...

Web project development JS function anti-shake and throttling sample code

Table of contents Stabilization Introduction Anti...

Detailed explanation of the process of building and running Docker containers

Simply pull the image, create a container and run...

Share 8 MySQL pitfalls that you have to mention

MySQL is easy to install, fast and has rich funct...

How to add abort function to promise in JS

Table of contents Overview Promise Race Method Re...

Basic usage tutorial of MySQL slow query log

Slow query log related parameters MySQL slow quer...

Optimization of MySQL thread_stack connection thread

MySQL can be connected not only through the netwo...

A brief discussion on how to learn JS step by step

Table of contents Overview 1. Clearly understand ...

border-radius method to add rounded borders to elements

border-radius:10px; /* All corners are rounded wi...

Use vue3 to implement a human-cat communication applet

Table of contents Preface Initialize the project ...