docker logs - view the implementation of docker container logs

docker logs - view the implementation of docker container logs

You can view the container logs through the docker logs command.

Command format:

$ docker logs [OPTIONS] CONTAINER
 Options:
    --details Display more information -f, --follow Track real-time logs --since string Display logs after a certain timestamp, 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 timestamps --until string Display logs before a certain timestamp, or relative time, such as 42m (i.e. 42 minutes)

example:

View the logs after the specified time and only display the last 100 lines:

$ docker logs -f -t --since="2018-02-08" --tail=100 CONTAINER_ID

View the logs for the last 30 minutes:

$ docker logs --since 30m CONTAINER_ID

View the logs after a certain time:

$ docker logs -t --since="2018-02-08T13:23:37" CONTAINER_ID

View logs for a certain period of time:

$ docker logs -t --since="2018-02-08T13:23:37" --until "2018-02-09T12:23:37" CONTAINER_ID

Supplement: Debugging tips for Docker containers: docker logs and docker service logs

Debugging containers

Many students who are new to Docker often encounter problems with the Docker container not being able to start, or they keep starting it repeatedly without knowing what to do.

Docker provides a series of simple commands that make it easy to debug problems in container operation.

The principle is very simple, that is, you can directly output the logs of the container runtime (or past tense).

There are usually 4 ways:

docker run (start the container on the console)

docker exec (attach to background container)

docker logs

docker service logs

The following will introduce

Start the container from the console

For example, debug and start the redis container

docker run -it -rm redis redis-server [redis startup parameters omitted...]

In this way, 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 expired containers.

Docker exec attaches to the background container

Sometimes you need to enter the container to check the system operation status. At this time, you can use docker exec.

The premise of using docker exec is that the container is running. Therefore, when the container does not work properly, this command often cannot be used.

docker logs

In fact, no matter what state the docker container is in, you can use docker logs to get all the logs of the container.

docker logs [container name]

Docker logs also has limitations, that is, it is not possible to obtain container logs that failed to start in Docker swarm mode.

docker service logs

For Docker swarm mode, get the command for container logs.

Generally, execute the following commands in sequence to get the container name of a service

docker service ls
docker service ps [service name]

Then you can get its log by container name

docker service logs [container name]

Docker service logs shows that the log is empty

To make docker service logs work properly, you need to set some docker configuration

vi /etc/docker/daemon.json

Add to this file:

{
  "log-driver": "json-file",
  "log-opts": {
    "labels": "production_status,geo",
    "env": "os,customer"
  }
}

Then restart docker

service docker restart

For a detailed introduction to docker service logs, please refer to the official documentation

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Solution to the problem of not being able to obtain the hostname of the host in the docker container
  • Each time Docker starts a container, the IP and hosts specified operations
  • Steps to restore code from a Docker container image
  • Docker container custom hosts network access operation
  • Solve the problem that some configuration files in /etc are reset after the docker container is restarted
  • A brief discussion on the problem of Docker run container being in created state
  • Docker container accesses the host's MySQL operation

<<:  Specific use of CSS content attribute

>>:  React component communication routing parameter transfer (react-router-dom)

Recommend

Detailed explanation of Vue's monitoring method case

Monitoring method in Vue watch Notice Name: You s...

Summary of basic knowledge and operations of MySQL database

This article uses examples to explain the basic k...

Recommend some useful learning materials for newbies in web design

Many people also asked me what books I read when ...

Detailed explanation of the use of MySQL paradigm

1. Paradigm The English name of the paradigm is N...

Example of implementing grouping and deduplication in MySQL table join query

Table of contents Business Logic Data table struc...

MySQL 8.0.24 installation and configuration method graphic tutorial

This article shares the installation tutorial of ...

Pay attention to the use of HTML tags in web page creation

This article introduces some issues about HTML ta...

Issues and precautions about setting maxPostSize for Tomcat

1. Why set maxPostSize? The tomcat container has ...

Detailed graphic tutorial on installing and uninstalling Tomcat8 on Linux

[ Linux installation of Tomcat8 ] Uninstall Tomca...

MySQL 8.0.13 installation and configuration method graphic tutorial

This article shares the installation and configur...

Analysis of Mysql transaction characteristics and level principles

1. What is a transaction? A database transaction ...

MySQL index coverage example analysis

This article describes MySQL index coverage with ...