Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand RUN, CMD and ENTRYPOINT in Dockerfile instructions

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to understand RUN and CMD and ENTRYPOINT in Dockerfile instructions. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

The three Dockerfile instructions, RUN, CMD, and ENTRYPOINT, look similar and can be easily confused. The editor will discuss their differences in detail through practice.

To put it simply:

RUN executes commands and creates new mirror layers, and RUN is often used to install software packages.

CMD sets the commands and their parameters that are executed by default after the container starts, but CMD can be replaced by command-line arguments followed by docker run.

ENTRYPOINT configures the commands that the container runs when it starts.

Let's analyze it in detail.

Shell and Exec format

We can specify the commands to be run by RUN, CMD, and ENTRYPOINT in two ways: Shell format and Exec format, with slight differences in use.

Shell format

For example:

RUN apt-get install python3

CMD echo "Hello world"

ENTRYPOINT echo "Hello world"

/ bin/sh-c is called at the bottom of the shell format when the instruction is executed.

For example, the following Dockerfile snippet:

ENV name Cloud Man

ENTRYPOINT echo "Hello, $name"

Executing docker run will output:

Hello, Cloud Man

Notice that the environment variable name has been replaced by the value Cloud Man.

Let's take a look at the Exec format.

Exec format

["executable", "param1", "param2",...]

For example:

RUN ["apt-get", "install", "python3"]

CMD ["/ bin/echo", "Hello world"]

ENTRYPOINT ["/ bin/echo", "Hello world"]

When the instruction is executed, it will be called directly and will not be parsed by shell.

For example, the following Dockerfile snippet:

ENV name Cloud Man

ENTRYPOINT ["/ bin/echo", "Hello, $name"]

Running the container will output:

Hello, $name

Notice that the environment variable "name" is not replaced.

If you want to use environment variables, modify them as follows

ENV name Cloud Man

ENTRYPOINT ["/ bin/sh", "- c", "echo Hello, $name"]

Running the container will output:

Hello, Cloud Man

CMD and ENTRYPOINT recommend using the Exec format because instructions are more readable and easier to understand. RUN is fine in both formats.

RUN

The RUN directive is typically used to install applications and software packages.

RUN executes the command at the top of the current mirror and creates a new mirror layer. Dockerfile often contains multiple RUN instructions.

RUN has two formats:

Shell format: RUN

Exec format: RUN ["executable", "param1", "param2"]

Here is an example of using RUN to install multiple packages:

RUN apt-get update & & apt-get install-y\

Bzr\

Cvs\

Git\

Mercurial\

Subversion

Note: apt-get update and apt-get install are executed in a RUN directive, which ensures that the latest package is installed each time. If apt-get install is executed in a separate RUN, the mirror layer created by apt-get update may be used, which may have been cached a long time ago.

CMD

The CMD directive allows the user to specify the default command to be executed by the container.

This command runs when the container starts and docker run does not specify another command.

If docker run specifies another command, the default command specified by CMD is ignored.

If there is more than one CMD instruction in the Dockerfile, only the last CMD is valid.

There are three formats for CMD:

Exec format: CMD ["executable", "param1", "param2"]

This is the recommended format for CMD.

CMD ["param1", "param2"] provides additional parameters for ENTRYPOINT, in which case ENTRYPOINT must be in Exec format.

Shell format: CMD command param1 param2

The Exec and Shell formats have been introduced earlier.

The second format, CMD ["param1", "param2"], is used in conjunction with the ENTRYPOINT instruction in Exec format, and its purpose is to set default parameters for ENTRYPOINT. We will give an example when we discuss ENTRYPOINT later.

Let's see how CMD works. The Dockerfile segment is as follows:

CMD echo "Hello world"

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

Hello world

But when a command, such as docker run-it [image] / bin/bash,CMD, is ignored, the command bash is executed:

Root@10a32dc7d3d3:/#

ENTRYPOINT

The ENTRYPOINT directive allows the container to run as an application or service.

ENTRYPOINT looks a lot like CMD in that you can specify the command to execute and its arguments. The difference is that ENTRYPOINT will not be ignored and will certainly be executed, even if other commands are specified when running docker run.

ENTRYPOINT has two formats:

Exec format: ENTRYPOINT ["executable", "param1", "param2"] this is the recommended format for ENTRYPOINT.

Shell format: ENTRYPOINT command param1 param2

You must be careful when choosing a format for ENTRYPOINT, because the effects of the two formats are very different.

Exec format

The Exec format of ENTRYPOINT is used to set the command to be executed and its parameters, while additional parameters can be provided through CMD.

The parameters in ENTRYPOINT are always used, while the extra parameters in CMD can be replaced dynamically when the container starts.

For example, the following Dockerfile snippet:

ENTRYPOINT ["/ bin/echo", "Hello"]

CMD ["world"]

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

Hello world

If started through docker run-it [image] CloudMan, the output is as follows:

Hello CloudMan

Shell format

ENTRYPOINT's Shell format ignores any parameters provided by CMD or docker run.

Best practic

Use the RUN directive 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 ENTRYPOINT directive in Exec format should be preferred. CMD provides additional default parameters for ENTRYPOINT and replaces them with the docker run command line.

If you want to set the default startup command for the container, use the CMD directive. You can replace this default command on the docker run command line.

At this point, we already have the ability to write Dockerfile. If you still feel unsure, recommend a quick way to master Dockerfile: go to Docker Hub to refer to the Dockerfile of those official images.

The above is how to understand RUN, CMD and ENTRYPOINT in Dockerfile instructions. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report