The combination and difference between ENTRYPOINT and CMD in dockerfile

The combination and difference between ENTRYPOINT and CMD in dockerfile

In the previous article [Detailed explanation of dockerfile for docker container], we have a more comprehensive understanding of dockerfile. We also mentioned that both `ENTRYPOINT` and `CMD` can specify the container startup command. Because these two commands are the core of mastering Dockerfile writing, they are discussed separately here.

1. Write in front

In the previous article, we have a comprehensive understanding of dockerfile. We also mentioned that ENTRYPOINT and CMD can specify the container startup command. Because these two commands are the core of mastering Dockerfile writing, they are discussed separately here.

2. The main difference between CMD and ENTRYPOINT

Let's get straight to the point. Both CMD and ENTRYPOINT are used to specify the command to start the container execution. The difference is:

  • When there are arguments in the docker run command, the daemon ignores the CMD command.
  • Using the ENTRYPOINT instruction will not be ignored, and will receive the docker run parameters appended to the command line.

In order for the built container to start normally, the Dockerfile file we write must contain a CMD or ENTRYPOINT instruction.

3. Combination of CMD and ENTRYPOINT

1.CMD

CMD instruction has three forms:

  1. CMD ["executable","param1","param2"] ( exec form, which is the preferred form)
  2. CMD ["param1","param2"] (as default parameters of ENTRYPOINT )
  3. CMD command param1 param2 (shell format)

When the Dockerfile contains multiple CMDs, only the last one is loaded and used.

We search for the centos official image in dockerhub and take a look at the official dockerfile file.

Basically, each official image will provide us with the Dockerfile link of their own version, as follows:

Let's look at the Dockerfile of the latest tag.

FROM scratch
ADD centos-8-x86_64.tar.xz /
LABEL org.label-schema.schema-version="1.0" org.label-schema.name="CentOS Base Image" org.label-schema.vendor="CentOS" org.label-schema.license="GPLv2" org.label-schema.build-date="20201204"
CMD ["/bin/bash"]

There are only four lines, which is all the content of the Dockerfile to build a latest version of centos8.3.2011 image. Specify the base image (here we start from the empty image scratch), add the rootfs content, add a label, and specify the startup command through CMD.

Not only centos, but other debian, ubuntu, busybox and other images only need to specify the startup command through CMD. For example, busybox is more concise:

FROM scratch
ADD busybox.tar.xz /
CMD ["sh"]

To build this kind of basic and tool images, we only need to specify a necessary CMD to start the container. But we don't write a Dockerfile just to start a container. Most of the time we want to run our app and service in the container.

Of course, it can also be started through CMD, but there is a flaw in this. The CMD startup command we mentioned above will be replaced by the docker run parameter.

We have the following Dockerfile

[root@localhost dockerfiles]# cat Dockerfile 
FROM centos
CMD ["/bin/top","-b"]

After building, start the container with the parameter ps.

[root@localhost dockerfiles]# docker run -it centos_top:v1 ps
  PID TTY TIME CMD
    1 pts/0 00:00:00 ps

You can see that after starting the container, top -b has been replaced by ps, and the parameter replacement is not achieved. Obviously this is not what we want. Is there any way to start the application by default and load it into the docker run parameters? This is the magic of ENTRYPOINT and CMD.

2. ENTRYPOINT combined with CMD

ENTRYPOINT 's exec and shell forms:

  • ENTRYPOINT ["executable", "param1", "param2"]
  • ENTRYPOINT command param1 param2

As mentioned above CMD ["param1","param2"] format can be used as an ENTRYPOINT parameter. At the same time, the command specified by ENTRYPOINT cannot be replaced by docker run parameters. If we combine the two instructions CMD and ENTRYPOINT, we can receive the docker run parameters through CMD and then pass the parameters to ENTRYPOINT for execution.

We take the official nginx dockerfile latest version 1.21 as an example

First, let's look at Dockerfile . Here we only focus on the startup command, as follows:

...
COPY docker-entrypoint.sh /
COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
COPY 30-tune-worker-processes.sh /docker-entrypoint.d
ENTRYPOINT ["/docker-entrypoint.sh"]

EXPOSE 80

STOPSIGNAL SIGQUIT

CMD ["nginx", "-g", "daemon off;"]

From the above we can see that when starting the nginx container, the docker-entrypoint.sh script is first run and the parameter nginx -g "daemon off;" in the CMD command is passed in. That is, starting the container with docker run without adding parameters is equivalent to executing the following script with default parameters.

#docker-entrypoint.sh nginx -g "daemon off;"

What happens when we pass in parameters using docker run?

I passed in nginx-debug

#docker run -dt nginx nginx-debug -g "daemon off;"

At this time, starting the container is equivalent to executing the following script and parameters

#docker-entrypoint.sh nginx-debug -g "daemon off;"

Let's take a look at the container we started through ps

[root@localhost dockerfiles]# ps -ef|grep nginx
root 6327 6306 0 Aug12 pts/0 00:00:00 nginx: master process nginx -g daemon off;
101 6384 6327 0 Aug12 pts/0 00:00:00 nginx: worker process
101 6385 6327 0 Aug12 pts/0 00:00:00 nginx: worker process
root 16800 16780 3 12:51 pts/0 00:00:00 nginx: master process nginx-debug -g daemon off;
101 16857 16800 0 12:51 pts/0 00:00:00 nginx: worker process
101 16858 16800 0 12:51 pts/0 00:00:00 nginx: worker process

Obviously, our two containers with parameters nginx and nginx-debug were started successfully!

That is to say, the command we specified through ENTRYPOINT ["/docker-entrypoint.sh"] will be executed at startup anyway, and can receive the parameters of docker run.

What is docker-entrypoint.sh? docker-entrypoint.sh This is a preprocessing script usually used to filter command line parameters or execute exec to start the process of container 1.

Implementing command default parameters or receiving docker run parameters through ENTRYPOINT+CMD is a very popular and useful way to write dockerfile.

This is the end of this article about the combination of ENTRYPOINT and CMD in dockerfile. For more information about ENTRYPOINT and CMD in dockerfile, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The combination and difference between ENTRYPOINT and CMD in dockerfile
  • The difference between ENTRYPOINT and CMD in Dockerfile
  • Detailed explanation of Dockerfile to create a custom Docker image and comparison of CMD and ENTRYPOINT instructions
  • Detailed explanation of CMD and ENTRYPOINT commands in Dockerfile

<<:  Solution for converting to inline styles in CSS (css-inline)

>>:  What is a MySQL index? Ask if you don't understand

Recommend

Vue implements file upload and download functions

This article example shares the specific code of ...

Web skills: Multiple IE versions coexistence solution IETester

My recommendation Solution for coexistence of mul...

Installation and use of Apache stress testing tools

1. Download Go to the Apache official website htt...

MySQL 8.X installation tutorial under Windows

I had been using MySQL 5.7 before, but because My...

Docker cleanup environment operation

Start cleaning carefully! List unused volumes doc...

Super detailed MySQL8.0.22 installation and configuration tutorial

Hello everyone, today we are going to learn about...

Detailed analysis of binlog_format mode and configuration in MySQL

There are three main ways of MySQL replication: S...

HTML sample code for implementing tab switching

Tab switching is also a common technology in proj...

WePY cloud development practice in Linux command query applet

Hello everyone, today I will share with you the W...

Install Percona Server+MySQL on CentOS 7

1. Environmental Description (1) CentOS-7-x86_64,...

Example of how to mosaic an image using js

This article mainly introduces an example of how ...

ffmpeg Chinese parameter description and usage examples

1. When ffmpeg pushes video files, the encoding f...