In Dockerfile, run, cmd, and entrypoint can all be used to execute commands. Here are their main uses:
Run commands in Shell and Exec formatsWe can specify the commands to be run by run, cmd, and entrypoint in the following two formats:
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 pitfallsThe 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 CommandThe 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 commandThe 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
But when a command is added after it, such as
If there are multiple cmd commands, only the last cmd command will be executed. entrypoint commandThe 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
If started by
Let's look at another example. The Dockerfile is: entrypoint ["echo", "Hello"] cmd ["world"] When the container is started via
If you start it with
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
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:
|
<<: Summary of the three stages of visual designer growth
>>: Detailed explanation of the mysql database LIKE operator in python
Table of contents Preface 1. Paste Events and Cli...
When mysql is running normally, it is not difficu...
A while ago, I wrote a blog post titled "Can...
1. Introduction Recently I found that there are m...
Introduction As mentioned in the previous article...
This article example shares the specific code of ...
CEP - Complex Event Processing. The payment has n...
Table of contents 1. Scene introduction 2 Code Op...
Without further ado, I will post the code for you...
Preface First, let's see how to identify a TC...
Form validation is one of the most commonly used ...
Table of contents 1. Array flattening (also known...
Nginx supports three ways to configure virtual ho...
Preface As we all know, JavaScript is single-thre...
Index condition pushdown (ICP) is introduced in M...