Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to write docker file so that the container does not exit when running bash scripts

Shulou Source: shulou.com Published: 2022-06-01 03:02:44 10月25日 Update

This article will explain in detail how to write docker file so that the container does not exit when running bash scripts. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

To write docker file, you need to master some basic knowledge:

Keyword 1:FROM specifies the base image

The so-called custom image, it must be based on an image, customized on it. Just as we run a container of nginx images and then modify them, the underlying image must be specified. FROM specifies the basic image, so FROM is a necessary instruction in a Dockerfile, and it must be the first instruction.

Keyword 2:ENTRYPOINT

The format of ENTRYPOINT is the same as that of RUN instructions, which is divided into exec format and shell format.

The purpose of ENTRYPOINT, like CMD, is to start the program and parameters in the specified container. ENTRYPOINT can also be replaced at run time.

When ENTRYPOINT is specified, the meaning of CMD changes. Instead of running its command directly, it passes the contents of the CMD as an argument to the ENTRYPOINT instruction.

Keyword 3:CMD

Docker is either a virtual machine or a container is a process. Since it is a process, you need to specify the program and parameters to run when you start the container. The CMD directive is used to specify the default startup command for the container main process.

The principle of CMD command execution script is essentially / bin/sh-c command to execute user script, so the current container's main process is actually / bin/sh, not the user's script.

/ bin/sh exits after executing the user script, and as the main process, the Docker container exits when it exits, which is what makes the container special and different from the virtual machine.

We usually say that the container is a single process, not only that there should be only one main process running in the container, in fact, we can think of the whole container as our main process, that is, do not think of the container as a virtual machine. Instead, think of the container as an executable file.

So when we execute a script, if we want the container not to exit, we can set "/ bin/bash" in ENTRYPOINT to ensure that the container does not exit.

For example, the following dockerfile:

FROM ccccccccccc

CMD ["/ data/main.sh"]

ENTRYPOINT ["/ bin/bash"]

Then use the docker build command to build the image:

Docker build-t test:v1.

On "how to write docker file so that the container does not exit when running bash scripts" this article shares here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Tags: Container process script run image base instruction command namely format key keyword parameter user article content more knowledge program good Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Docker Shulou Information MySQL Redmi