docker logs Viewing the implementation of docker container logs
- 2021-10-25 00:08:43
- OfStack
The docker logs command allows you to view the container's log.
Command format:
$ docker logs [OPTIONS] CONTAINER
Options:
--details Show more information
-f, --follow Trace real-time log
--since string Display from a certain timestamp After the log, or relative time, such as 42m (I. E. 42 Minutes)
--tail string How many lines of log are displayed from the end of the log, The default is all
-t, --timestamps Display timestamp
--until string Display from a certain timestamp Previous log, or relative time, such as 42m (I. E. 42 Minutes)
Examples:
View the log after the specified time, showing only the last 100 lines:
$ docker logs -f -t --since="2018-02-08" --tail=100 CONTAINER_ID
Check the log for the last 30 minutes:
$ docker logs --since 30m CONTAINER_ID
View the log after a certain time:
$ docker logs -t --since="2018-02-08T13:23:37" CONTAINER_ID
View the log of a certain time period:
$ docker logs -t --since="2018-02-08T13:23:37" --until "2018-02-09T12:23:37" CONTAINER_ID
Supplement: Debugging skills of Docker container: docker logs and docker service logs
Debug container
Many students who have just come into contact with Docker usually can't get up in the face of docker container, or they don't know how to take measures during repeated startup.
docker provides 1 series of simple commands, which makes it easy to debug problems in container operation.
The principle is very simple, that is, you can directly output the log of the container runtime (or past tense).
There are usually four ways:
docker run
(Container startup on console)
docker exec
(Attached to Background Container)
docker logs
docker service logs
They are introduced in turn below
Console startup container
Start the redis container by debugging, for example
docker run -it -rm redis redis-server [redis Startup parameters abbreviated ...]
So the log output of redis-server is printed directly to the console
The disadvantage is that this method is only used when starting container debugging, and cannot operate containers running in the background or failed containers.
docker exec Attached to Background Container
Sometimes you need to go into the container to check the running status of the system. You can use docker exec at this time.
The premise of using docker exec is that the container is running. Therefore, when the container does not work properly, it is often impossible to use this command
docker logs
docker logs can be used to get all the logs of the docker container, regardless of its state.
docker logs [ Container name ]
docker logs also has a limitation, that is, it cannot obtain the container log of startup failure in docker swarm mode
docker service logs
Command to get the container log for docker swarm mode.
1, execute the following commands in turn to get the container name of a service
docker service ls
docker service ps [ Service name ]
Then you can get its log by the container name
docker service logs [container name]
docker service logs Display log is empty
For docker service logs to work properly, you need to set some configurations of docker1
vi /etc/docker/daemon.json
Add to the file:
{
"log-driver": "json-file",
"log-opts": {
"labels": "production_status,geo",
"env": "os,customer"
}
}
Then restart docker
$ docker logs -f -t --since="2018-02-08" --tail=100 CONTAINER_ID
0
For a detailed description of docker service logs, please refer to the official document