current position:Home>About the problem that the container will hang up after starting nginx in the docker container

About the problem that the container will hang up after starting nginx in the docker container

2022-05-15 02:55:16Common program ape

Recently tried based on nginx Made a mirror image , stay Dockerfile Call a script in , This script is used to modify... According to the parameters passed in at startup nginx Default profile , And then restart nginx service .Dockerfile It's about :

FROM nginx:alpine
ENV backend_api_url xxx
ADD launch.sh /launch.sh
EXPOSE 80
ENTRYPOINT [ "sh", "/launch.sh" ]

then launch.sh The main content of the script is to modify nginx.conf Configuration in file , perform nginx Start command . But after writing, the deployment found that the image can build success , But when you start a container, it always fails , Tried all kinds of start-up methods, but it didn't work .

Finally, after consulting the data, we found an effective method : Use... In scripts nginx -g "daemon off;" To start up nginx.

Principle analysis

stay docker In the container pid=1 After the process of hangs up, the whole container will hang up , Based on nginx In terms of containers , Default pid=1 This is the process of nginx, But once you are Dockerfile Use in CMD perhaps ENTRYPOINT after , The corresponding program will replace nginx Turn into pid=1 The process of , For example, in my example above launch.sh, And once the script is executed, exit , The container will stop . When using nginx -g "daemon off;" Then tell me nginx Do not exit after startup , So as to ensure the normal operation of the generated container .

copyright notice
author[Common program ape],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/132/202205120035143569.html

Random recommended