Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of nginx Log in docker

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of nginx log in docker, which is very detailed and has certain reference value. Friends who are interested must finish reading it!

Docker usage

1. Docker ps to view running containers

2. Docker images view docker image

3. Docker rm id (container id) Delete the container (the container id can be viewed through docker ps, and the container must be stopped before it can be deleted)

Delete all containers docker rm `containers-a-q`

4. Docker stop id (container id) stops the container from running

5. Docker rmi id (Image id) Delete an image

6. Docker pull ubuntu:16.04 (image name: version number) download image

7. Docker run-it ubuntu:16.04 creates and runs a container container

-t means to specify a pseudo terminal or terminal in the new container

-I means to allow us to interact with (STDIN) in the container.

-p specifies the mapped port

-d runs the container in the background and prints the container ID

7.1 docker run-dit ubuntu:16.04 creates and runs the container in the background

7.2 docker run-ditp 8080 ubuntu:16.04 8080 (host port: container port) creates and runs the container in the background and maps the container's port

8. Docker attach id (Container id) enters the running container environment

9. Exit the container

9.1 exit exits the container directly and terminates the container

9.2 [ctrl+p] + [ctrl+q] (shortcut key) exits the container, but does not stop the container from running

10. Docker commit-m 'version ID' id (container id) ubuntu:16.04 (image and version number) submit the image and generate the image (you can package the built container into a new image or overwrite the original image through this command (that is, modify the content of the original image, and the generated image name is the same as the version number)

All right, now that you know about docker, here is the focus of this article. Let's take a look at it.

Nginx does not handle the scrolling problem of the log itself, it kicks the ball to the user. In general, you can use the logrotate tool to accomplish this task, or if you like, you can write a variety of scripts to accomplish the same task. In this article, the author describes how to scroll the nginx log file running in docker (the following figure is from the Internet).

Train of thought

The Nginx official actually gives instructions on how to scroll the log:

Rotating Log-files

In order to rotate log files, they need to be renamed first. After that USR1 signal should be sent to the master process. The master process will then re-open all currently open log files and assign them an unprivileged user under which the worker processes are running, as an owner. After successful re-opening, the master process closes all open files and sends the message to worker process to ask them to re-open files. Worker processes also open new files and close old files right away. As a result, old files are almost immediately available for post processing, such as compression.

The main idea of this paragraph is:

Rename the old log file first

Then send the USR1 signal to the nginx master process

After receiving the signal, the nginx master process will do some processing, and then ask the worker process to reopen the log file.

The worker process opens the new log file and closes the old one

In fact, the only work we really need to do is the first two points!

Create a test environment

Assuming that docker is already installed on your system, let's run a nginx container directly:

$docker run-d\-p 80:80\-v $(pwd) / logs/nginx:/var/log/nginx\-- restart=always\-- name=mynginx\ nginx:1.11.3

Notice that we mounted the log bindings of nginx to the logs directory under the current directory.

Save the following to the test.sh file:

#! / bin/bashfor ((iSuppli _ I / dev/null sleep 1done

Then run the script to simulate the generation of continuous logging.

A script to create a scrolling log

Create a rotatelog.sh file with the following contents:

#! / bin/bashgetdatestring () {TZ='Asia/Chongqing' date "+% Y%m%d%H%M"} datestring=$ (getdatestring) mv / var/log/nginx/access.log / var/log/nginx/access.$ {datestring} .logmv / var/log/nginx/error.log / var/log/nginx/error.$ {datestring} .logkill-USR1 `cat / var/run/ nginx.pid`

The getdatestring function takes the current time and formats it as a string, such as "201807241310". I prefer to name files with date and time. Note that the time zone is specified through TZ='Asia/Chongqing', because the UTC time is formatted by default, which is weird to use (real-time brain supplement + 8 hours). The following two mv commands are used to rename log files. Finally, the USR1 signal is sent to the nginx master process by kill command.

Add executable permissions to the rotatelog.sh file and copy it to the $(pwd) / logs/nginx directory with the following command:

$chmod + x rotatelog.sh$ sudo cp rotatelog.sh$ (pwd) / logs/nginx

Perform scrolling operations on a regular basis

Our nginx runs in the container, so we need to send USR1 signals to the nginx master process in the container. So we need to execute the rotatelog.sh script in the mynginx container through the docker exec command:

$docker exec mynginx bash / var/log/nginx/rotatelog.sh

Execute the above command once, and a new batch of log files will be generated as scheduled:

Let's configure this command in a scheduled task to be executed at 1 o'clock every morning. Execute the crontab-e command and add the following line at the end of the file:

* 1 * docker exec mynginx bash / var/log/nginx/rotatelog.sh

Just save and exit. The following figure shows the effect of scrolling every 5 minutes during the test:

Why not directly mv log files in the host?

This is possible in theory, because the contents of the data volume mounted by binding are the same from the host as from the container. But when you do this, you are likely to have access issues. In the host, you generally use ordinary users, while the owners of log files generated in the container are special users and generally do not give other users permission to write and execute:

Of course, there will be no problem if you are using root users in your host.

Can you send a signal from the host?

In fact, the full name of this question should be: can you send a signal to the nginx master process in the docker container from the host?

The answer is yes.

Docker A kill command that sends signals to processes in the container. We can do this by order:

$docker container kill mynginx-s USR

Send a USR1 signal to process 1 (nginx master) in the container (this method can only send a signal to process 1):

Combining the above two questions, we can write another way to scroll the nginx log in docker. In this way, you do not need to execute the command in the container through the docker exec command, but complete all the operations in the host:

Rename the log file in the container data volume first

Send a USR1 signal to process 1 in the container

The above is all the contents of the article "sample Analysis of nginx logs in docker". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report