A brief discussion on why daemon off is used when running nginx in docker

A brief discussion on why daemon off is used when running nginx in docker

I'm very happy. When encountering this problem, I have to talk about the process principle of the Docker container. Basically, people who have learned about Docker are aware of several isolation methods of Docker, and the process is also isolated.

question

1. Why does the docker container crash while running?

By default, the docker container will use the first process inside the container, that is, the program with pid=1, as the basis for whether the docker container is running. If the docker container pid hangs, the docker container will exit directly.

2.When docker run, use command as the internal command of the container. If you use nginx, the nginx program will run in the background. At this time, nginx is not a program with pid 1, but bash is executed. This bash hangs after executing the nginx command, so the container also exits. The same reason is true for you. After pm2 start, the pid of bash is 1. At this time, bash will exit after execution, so the container also exits.

Below I will use examples to tell you why we do this! !

touch file get_pid

echo "PID of this script: $$"
echo "PPID of this script: $PPID"
echo "UID of this script: $UID"
#nginx -g 'daemon off;'

At this point we start the container to execute this sh file

odtoy:~ zhaojunlike$ eval `docker-machine env default`
godtoy:~ zhaojunlike$ cd WorkSpace/
godtoy:WorkSpace zhaojunlike$ ls
docker nodejs php pid_get
godtoy:WorkSpace zhaojunlike$ vim pid_get 
godtoy:WorkSpace zhaojunlike$ docker run -v `pwd`/pid_get:/pid_get:ro --rm --workdir=/ nginx bash /pid_get
PID of this script: 1
PPID of this script: 0
UID of this script: 0
godtoy:WorkSpace zhaojunlike$

After the container executes pid_get, the container automatically exits. At this time, the pid of the current bash execution is printed as 1.

Therefore, if we want the container not to crash, non-daemon execution is a must. Of course, we can also execute inside a container.

godtoy:WorkSpace zhaojunlike$ docker run -it nginx bash
root@a8baa5fe77f0:/# nginx
root@a8baa5fe77f0:/# godtoy:WorkSpace zhaojunlike$

We use the -it parameter to connect to the pipeline inside the container, and then we use the nginx command inside the container. Finally, after exiting the container Ctrl+P+Q , the container is still running.

Summarize

If you need to use node in docker, there is no need to install tools such as pm2. Just run node directly. If you are afraid that your container will crash, you can add restart and other related parameters, such as ` docker run .... --restart=always

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to configure nginx+php+mysql in docker
  • Solution to invalid Nginx cross-domain setting Access-Control-Allow-Origin
  • Example method of deploying react project on nginx
  • Use nginx.vim tool for syntax highlighting and formatting configuration nginx.conf file
  • Detailed explanation of the pitfalls of add_header in nginx configuration tutorial
  • How to configure two-way certificate verification on nginx proxy server
  • Solution to the problem of information loss with "_" in header when using Nginx proxy
  • Shell script nginx automation script
  • How to create an Nginx server with Docker
  • nginx proxy_cache batch cache clearing script introduction

<<:  React uses routing to redirect to the login interface

>>:  How to implement Mysql switching data storage directory

Recommend

How to implement remote automatic backup of MongoDB in Linux

Preface After reading the previous article about ...

HTML+CSS to achieve simple navigation bar function

Without further ado, I'll go straight to the ...

How to use resident nodes for layer management in CocosCreator

CocosCreator version: 2.3.4 Most games have layer...

MySQL 8.0.21 installation tutorial under Windows system (illustration and text)

Installation suggestion : Try not to use .exe for...

A brief discussion on the $notify points of element

My original intention was to encapsulate the $not...

Detailed explanation of Xshell common problems and related configurations

This article introduces common problems of Xshell...

VMware Workstation 14 Pro installs CentOS 7.0

The specific method of installing CentOS 7.0 on V...

Four completely different experiences in Apple Watch interaction design revealed

Today is still a case of Watch app design. I love...

CSS HACK for IE6/IE7/IE8/IE9/FF (summary)

Since I installed the official version of IE8.0, ...

Implementation of element multiple form validation

In the project, form testing is often encountered...

Use vertical-align to align input and img

Putting input and img on the same line, the img ta...

Introduction to the B-Tree Insertion Process

In the previous article https://www.jb51.net/arti...

How to install and configure mysql 5.7.19 under centos6.5

The detailed steps for installing mysql5.7.19 on ...

Sending emails in html is easy with Mailto

Recently, I added a click-to-send email function t...

MySQL transaction autocommit automatic commit operation

The default operating mode of MySQL is autocommit...