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

Solve MySQL login error: 'Access denied for user 'root'@'localhost'

First of all, I don't know why I can't lo...

How to solve the mysql error 1033 Incorrect information in file: 'xxx.frm'

Problem Description 1. Database of the collection...

Complete steps to solve 403 forbidden in Nginx

The webpage displays 403 Forbidden Nginx (yum ins...

The pitfalls and solutions caused by the default value of sql_mode in MySQL 5.7

During normal project development, if the MySQL v...

Detailed explanation of react setState

Table of contents Is setState synchronous or asyn...

A MySQL migration plan and practical record of pitfalls

Table of contents background Solution 1: Back up ...

Summary of Seven Basic XHTML Coding Rules

1. All tags must have a corresponding end tag Prev...

Pure HTML+CSS to achieve typing effect

This article mainly introduces the typing effect ...

Detailed explanation of mkdir command in Linux learning

Table of contents Preface 1. Basic knowledge of f...

Implementation of select multiple data loading optimization in Element

Table of contents Scenario Code Implementation Su...

How to make a List in CocosCreator

CocosCreator version: 2.3.4 Cocos does not have a...

Installation process of CentOS8 Linux 8.0.1905 (illustration)

As of now, the latest version of CentOS is CentOS...