Home Operation and Maintenance Nginx How to scroll nginx logs in docker

How to scroll nginx logs in docker

May 29, 2023 pm 09:40 PM
docker nginx

Docker usage

1. docker ps to view running containers

2. docker images to view docker images

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

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

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

 5. docker rmi id (mirror id) deletes the image

 6. docker pull ubuntu:16.04 (mirror name: version number) downloads the image

 7. docker run -it ubuntu:16.04 Create and run the container container

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

 -i means to allow us Interact with (stdin) in the container

 -p specifies the mapped port

 -d Run the container in the background and print the container id

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

 7.2 docker run -ditp 8080:8080 (host port: container port) ubuntu:16.04 Create and run the container in the background and map the port of the container

 8. docker attach id (Container id) Enter the running container environment

 9. Exit the container

 9.1 exit Directly exit the container and terminate the container running

 9.2 [ctrl p] [ctrl q ] (shortcut key) Exit the container, but will not terminate the container running

 10. docker commit -m'version identification' id (container id) ubuntu:16.04 (image and version number) Submit the image and generate the image ( You can use this command to package the built container into a new image or overwrite the original image (that is, modify the content of the original image, and the generated image name can be directly overwritten if the name of the generated image is the same as the version number))

Okay, everyone knows about docker. Here are the key points of this article. Let’s take a look.

nginx itself did not handle the log rolling problem, it kicked the ball to the user. Typically, you can use the logrotate tool to accomplish this task, or if you prefer, you can write a variety of scripts to accomplish the same task. The author of this article introduces how to scroll the nginx log file running in docker (the picture below is from the Internet).

How to scroll nginx logs in docker

Thinking

nginx official actually gives instructions on how to roll 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 general idea of ​​this description is:

•First Rename the old log file
•Then send the usr1 signal to the nginx master process
•The nginx master process will do some processing after receiving the signal, and then ask the worker process to reopen the log file
•Worker The process opens a new log file and closes the old log file

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

Create a test environment

Assuming that docker has been installed in your system, here we run an nginx container directly:

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

Note that we bind the nginx log Mounted to the logs directory in the current directory.

Save the following content to the test.sh file:

#!/bin/bash
for ((i=1;i<=100000;i++))
do
 curl http://localhost > /dev/null
 sleep 1
done
Copy after login

Then run this script to simulate the generation of continuous log records.

Script to create rolling log

Create the rotatelog.sh file with the following content:

#!/bin/bash
getdatestring()
{
 tz=&#39;asia/chongqing&#39; date "+%y%m%d%h%m"
}
datestring=$(getdatestring)
mv /var/log/nginx/access.log /var/log/nginx/access.${datestring}.log
mv /var/log/nginx/error.log /var/log/nginx/error.${datestring}.log
kill -usr1 `cat /var/run/nginx.pid`
Copy after login

getdatestring function takes the current time and formats it as a string, such as "201807241310 ", the author prefers to name files with date and time. Note that the time zone is specified here through tz='asia/chongqing', because by default the format is UTC time, which is weird to use (you need to make up for 8 hours in real time). The following two mv commands are used to rename log files. Finally, send the usr1 signal to the nginx master process through the kill command.

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

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

Perform rolling operations regularly

Our nginx runs in a container, so we need to send the usr1 signal to the nginx master process in the container. Therefore 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

Executing the above command once will generate a batch of new log files as scheduled:

How to scroll nginx logs in docker

Below we configure this command in a scheduled task and let it be executed once every morning at 1 o'clock. Execute the crontab -e command and add the following lines at the end of the file:

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

How to scroll nginx logs in docker

Save and exit. The following picture is the effect of scrolling every 5 minutes during the author's test process:

How to scroll nginx logs in docker

Why not mv the log file directly in the host?

Theoretically, this is possible, because the contents of the data volume mounted through binding are the same when viewed from the host and from the container. But when you actually do this you are likely to run into permission issues. In the host machine, you generally use an ordinary user, while the owner of the log file generated in the container will be a special user, and generally other users will not be given write and execution permissions:


How to scroll nginx logs in docker

Of course, if you are using the root user on the host machine, there will be no problem.

Can the signal be sent from the host?

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

The answer is, yes.

The author introduces the problem of signal capture in containers in this article. Interested friends can take a look. In that article we introduced docker’s kill command that sends signals to processes in a container. We can use the command:

$ docker container kill mynginx -s usr

to send the usr1 signal to process No. 1 (nginx master) in the container (this method only Can send a signal to process No. 1):

How to scroll nginx logs in docker

Combining the above two questions, we can write another way to scroll the nginx log in docker. This method does not require executing commands in the container through the docker exec command, but completes all operations on the host:

•First rename the log file in the container data volume
• Send usr1 signal to process No. 1 in the container

The above is the detailed content of How to scroll nginx logs in docker. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to change the docker image source in China How to change the docker image source in China Apr 15, 2025 am 11:30 AM

You can switch to the domestic mirror source. The steps are as follows: 1. Edit the configuration file /etc/docker/daemon.json and add the mirror source address; 2. After saving and exiting, restart the Docker service sudo systemctl restart docker to improve the image download speed and stability.

How to read the docker version How to read the docker version Apr 15, 2025 am 11:51 AM

To get the Docker version, you can perform the following steps: Run the Docker command "docker --version" to view the client and server versions. For Mac or Windows, you can also view version information through the Version tab of the Docker Desktop GUI or the About Docker Desktop menu.

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to view logs from docker How to view logs from docker Apr 15, 2025 pm 12:24 PM

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

How to build a private repository by docker How to build a private repository by docker Apr 15, 2025 am 11:06 AM

You can build Docker private repositories to securely store and manage container images, providing strict control and security. The steps include: creating a repository, granting access, deploying a repository, pushing an image, and pulling an image. Advantages include security, version control, reduced network traffic and customization.

How to run the docker command How to run the docker command Apr 15, 2025 am 11:24 AM

How to run Docker commands? Install Docker and start the daemon. Common Docker commands: docker images: display image docker ps: display container docker run: run container docker stop: stop container docker rm: delete container interact with container using Docker command: docker exec: execute command docker attach: attach console docker logs: display log docker commit: commit change to mirror stop Docker daemon: sudo systemctl stop doc

See all articles