10. Docker's CMD command

When creating and starting a container through docker run, at the end of the command, you can specify the command to be executed in the container immediately after the container is started, such as: docker run -i -t image /bin/bash , which means that the container will be opened immediately in the container when it starts. A shell pseudo terminal.

In addition to this way, we can completely specify the command to be executed when the container starts through the CMD instruction in the dockerfile file.

First, write a simple dockfile, based on centos:

#Base image
FROM centos

#MAINTAINER,this dockerfile creater
MAINTAINER [email protected]

#Use the CMD command
CMD ["/bin/bash"]

Second, we build an image based on this dockerfile:

Next, we look at the built image:

Finally, let's experience the role of CMD:

1. The parameter of the last line of CMD instruction in the above dockerfile file is to specify the command to be executed when the container is started . Here it is set to the bin/bash command. When we use the docker run -i -t image/bin/bash command to interact, it is in the new Specifying a pseudo terminal or terminal in the container will finally help us generate this new container; at this time, since we specified /bin/bash in CMD, when we should have executed the docker run -i -t image /bin/bash When interacting with commands, you can omit /bin/bash, that is, we only need to type docker run -i -t image:

Let's check whether the container generated for us is the same as the container ID pointed to by the arrow above:

By comparison, it is the same!

2. Even if there is a CMD instruction in the dockerfile, we can still bring the command executed when the container starts in the docker run command, which will overwrite the command specified by the CMD instruction in the dockerfile.

For example: docker run -i -t images /bin/ps (here we specify the view process command, so that the interactive shell interface cannot be opened, so as to test whether the CMD command in the dockerfile is overwritten)

3. In dockerfile, there can be multiple CMD commands, but multiple CMD commands are not all valid, and the last CMD command is the main one

4. The parameter format of the CMD command is generally recommended to be written as a string array, such as:

CMD ["echo","dockerfile.cmd"]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325097225&siteId=291194637