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

What is the use of CMD and ENTRYPOINT instructions in Dockerfile

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly shows you "what is the use of CMD and ENTRYPOINT instructions in Dockerfile". It is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "what is the use of CMD and ENTRYPOINT instructions in Dockerfile".

Dockerfile is used to automate the construction of a docker image. There are two instructions, CMD and ENTRYPOINT, in Dockerfile, and their functions are somewhat similar. Here is an explanation:

One: CMD instruction

The main purpose of a CMD is to provide defaults for an executing container.

CMD provides some commands and parameters when the container is running. The usage is as follows:

CMD ["executable", "param1", "param2"] (exec form, this is the preferred form)

CMD ["param1", "param2"] (as default parameters to ENTRYPOINT)

CMD command param1 param2 (shell form)

The first usage: run an executable file and provide parameters.

The second use: specify parameters for ENTRYPOINT.

The third usage (shell form): a command executed in the "/ bin/sh-c" method.

For example:

CMD ["/ bin/echo", "this is an echo test"]

Run after build (assuming the image name is ec):

Docker run ec

It will output: this is an echo test

Note:

The docker run command overrides the parameters in the CMD if the parameter is specified: (for example, the parameter of the docker run-it ubuntu / bin/bash command refers to / bin/bash instead of-it, and-it is only the parameter of docker, not the parameter of the container, as described below. )

The same ec image above starts:

Docker run ec / bin/bash

There is no output: this is an echo test, because the CMD command is overridden by "/ bin/bash".

Two: ENTRYPOINT command

An ENTRYPOINT allows you to configure a container that will run as an executable.

It can make your container function behave like an executable program.

Example 1:

ENTRYPOINT ["/ bin/echo"]

Then the container function after the image from docker build is like a / bin/echo program:

For example, the name of the image from my build is imageecho, so I can use it like this:

Docker run-it imageecho "this is a test"

The string "this is a test" is output here, and the container corresponding to the imageecho image behaves like an echo program. The parameter "this is a test" you add will be added after ENTRYPOINT, and it will be like this / bin/echo "this is a test".

Example 2:

ENTRYPOINT ["/ bin/cat"]

You can run the constructed image like this (assuming it's called st):

Docker run-it st / etc/fstab

This is equivalent to the function of the command / bin/cat / etc/fstab. After running, output the contents of / etc/fstab.

ENTRYPOINT can be written in two ways:

Write method 1:

ENTRYPOINT ["executable", "param1", "param2"] (the preferred exec form)

Writing method 2:

ENTRYPOINT command param1 param2 (shell form)

You can also use-entrypoint to specify it in the docker run command (but you can only use method one).

Here's what happens when I set ENTRYPOINT to ["/ bin/sh-c"]:

Docker run-it T2 / bin/bash

Ps

PID TTY TIME CMD

1? 00:00:00 sh

9? 00:00:00 bash

19? 00:00:00 ps

You can see that the process with PID 1 is running sh, while bash is just a child of sh, and / bin/bash is just a parameter after / bin/sh-c.

CMD can provide parameters for ENTRYPOINT, and ENTRYPOINT itself can also contain parameters, but you can write parameters that may need to be changed into CMD and parameters that do not need to be changed into ENTRYPOINT.

For example:

FROM ubuntu:14.10

ENTRYPOINT ["top", "- b"]

CMD ["- c"]

Write the parameters that may need to be changed into CMD. Then you can specify the parameters in docker run so that the parameters in CMD (- c here) will be overwritten and those in ENTRYPOINT will not be overwritten.

These are all the contents of the article "what are the CMD and ENTRYPOINT instructions in Dockerfile?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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

Internet Technology

Wechat

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

12
Report