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

Centos7.5 configuration java environment installation tomcat explanation

Tomcat is a web server software based on Java lan...

Simple tutorial on using Navicat For MySQL

recommend: Navicat for MySQL 15 Registration and ...

How to insert 10 million records into a MySQL database table in 88 seconds

The database I use is MySQL database version 5.7 ...

MySQL infobright installation steps

Table of contents 1. Use the "rpm -ivh insta...

Use javascript to create dynamic QQ registration page

Table of contents 1. Introduction 1. Basic layout...

Analyze Tomcat architecture principles to architecture design

Table of contents 1. Learning Objectives 1.1. Mas...

Tutorial on installing AutoFs mount service under Linux

Whether it is Samba service or NFS service, the m...

25 fresh useful icon sets for download abroad

1. E-Commerce Icons 2. Icon Sweets 2 3. Mobile Ph...

Some problems you may encounter when installing MySQL

Question 1: When entering net start mysql during ...

Summary of MySQL5 green version installation under Windows (recommended)

1 Download MySQL Download address: http://downloa...

The problem of Vue+tsx using slot is not replaced

Table of contents Preface Find the problem solve ...

Detailed explanation of Excel parsing and exporting based on Vue

Table of contents Preface Basic Introduction Code...

Mysql delete duplicate data to keep the smallest id solution

Search online to delete duplicate data and keep t...