Docker log sharing

View logs for a container or service

  1. the docker service logs command shows information logged by all containers participating in a service. The information that is logged and the format of the log depends almost entirely on the container’s endpoint command.( this is for docker swarm)
  2. [By default], docker logs or docker service logs shows the command’s output just as it would appear if you ran the command interactively in a terminal. UNIX and Linux commands typically open three I/O streams when they run, called STDIN, STDOUT, and STDERR. STDIN is the commmand’s input stream, which may include input from the keyboard or input from another command. STDOUT is usually a command’s normal output, and STDERR is typically used to output error messages. By default, docker logs shows the command’s STDOUT and STDERR.

Configure logging drivers

Each Docker daemon has a default logging driver, which each container uses unless you configure it to use a different logging driver.

Configure the default logging driver

To configure the Docker daemon to default to a specific logging driver, set the value of log-driver to the name of the logging driver in the daemon.json file, which is located in /etc/docker/ on Linux hosts.
The default logging driver is json-file. The following example explicitly sets the default logging driver to syslog:

{
  "log-driver": "syslog"
}

If the logging driver has configurable options, you can set them in the daemon.json file as a JSON array with the key log-opts. The following example sets two configurable options on the json-file logging driver:

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

Configure the logging driver for a container

When you start a container, you can configure it to use a different logging driver than the Docker daemon’s default, using the –log-driver** flag. If the logging driver has configurable options, you can set them using one or more instances of the –log-opt = flag. Even if the container uses the default logging driver, it can use different configurable options.**

The following example starts an Alpine container with the none logging driver.

$ docker run -it --log-driver none alpine ash

To find the current logging driver for a running container, if the daemon is using the json-file logging driver, run the following docker inspect command, substituting the container name or ID for < container>:

$ docker inspect -f '{{.HostConfig.LogConfig.Type}}' <CONTAINER>

json-file

Use a logging driver plugin

Docker logging plugins allow you to extend and customize Docker’s logging capabilities beyond those of the built-in logging drivers. A logging service provider can implement their own plugins and make them available on Docker Hub, Docker Store, or a private registry. This topic shows how a user of that logging service can configure Docker to use the plugin.

Configure a container to use the plugin as the logging driver

After the plugin is installed, you can configure a container to use the plugin as its logging driver by specifying the –log-driver flag to docker run, as detailed in the logging overview. If the logging driver supports additional options, you can specify them using one or more –log-opt flags with the option name as the key and the option value as the value.

Install the logging driver plugin

To install a logging driver plugin, use docker plugin install < org/image>, using the information provided by the plugin developer.
You can list all installed plugins using docker plugin ls, and you can inspect a specific plugin using docker inspect.

Log tags for logging driver

The tag log option specifies how to format a tag that identifies the container’s log messages. By default, the system uses the first 12 characters of the container ID. To override this behavior, specify a tag option:

$ docker run --log-driver=fluentd --log-opt fluentd-address=myhost.local:24224 --log-opt tag="mailer"

Docker supports some special template markup you can use when specifying a tag’s value:

MarkupDescription
{{.ID}}The first 12 characters of the container ID.
{{.FullID}}The full container ID.
{{.Name}}The container name.
{{.ImageID}}The first 12 characters of the container’s image ID.
{{.ImageFullID}}The container’s full image ID.
{{.ImageName}}The name of the image used by the container.
{{.DaemonName}}The name of the docker program (docker).

For example, specifying a –log-opt tag=”{{.ImageName}}/{{.Name}}/{{.ID}}” value yields syslog log lines like:

Aug  7 18:33:19 HOSTNAME hello-world/foobar/5790672ab6a0[9103]: Hello from Docker.

At startup time, the system sets the container_name field and {{.Name}} in the tags. If you use docker rename to rename a container, the new name is not reflected in the log messages. Instead, these messages continue to use the original container name.

available log driver

C:\Users\mcong\Desktop\github\docker-ce\components\engine\daemon\logdrivers_linux.go

import (
    // Importing packages here only to make sure their init gets called and
    // therefore they register themselves to the logdriver factory.
    _ "github.com/docker/docker/daemon/logger/awslogs"
    _ "github.com/docker/docker/daemon/logger/fluentd"
    _ "github.com/docker/docker/daemon/logger/gcplogs"
    _ "github.com/docker/docker/daemon/logger/gelf"
    _ "github.com/docker/docker/daemon/logger/journald"
    _ "github.com/docker/docker/daemon/logger/jsonfilelog"
    _ "github.com/docker/docker/daemon/logger/logentries"
    _ "github.com/docker/docker/daemon/logger/splunk"
    _ "github.com/docker/docker/daemon/logger/syslog"
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值