diff --git a/api/client/commands.go b/api/client/commands.go index e10bee3b18..8a52b94941 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -1929,6 +1929,10 @@ func (cli *DockerCli) CmdLogs(args ...string) error { return err } + if env.GetSubEnv("HostConfig").GetSubEnv("LogConfig").Get("Type") != "json-file" { + return fmt.Errorf("\"logs\" command is supported only for \"json-file\" logging driver") + } + v := url.Values{} v.Set("stdout", "1") v.Set("stderr", "1") diff --git a/daemon/container.go b/daemon/container.go index 604fbe882f..57c0071a5d 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -1489,3 +1489,12 @@ func (container *Container) getNetworkedContainer() (*Container, error) { func (container *Container) Stats() (*execdriver.ResourceStats, error) { return container.daemon.Stats(container) } + +func (c *Container) LogDriverType() string { + c.Lock() + defer c.Unlock() + if c.hostConfig.LogConfig.Type == "" { + return c.daemon.defaultLogConfig.Type + } + return c.hostConfig.LogConfig.Type +} diff --git a/daemon/logs.go b/daemon/logs.go index db977ddac1..356d08c5c8 100644 --- a/daemon/logs.go +++ b/daemon/logs.go @@ -44,6 +44,9 @@ func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status { if err != nil { return job.Error(err) } + if container.LogDriverType() != "json-file" { + return job.Errorf("\"logs\" endpoint is supported only for \"json-file\" logging driver") + } cLog, err := container.ReadLog("json") if err != nil && os.IsNotExist(err) { // Legacy logs diff --git a/docs/man/docker-create.1.md b/docs/man/docker-create.1.md index d5166ca93a..bf6c1a0002 100644 --- a/docs/man/docker-create.1.md +++ b/docs/man/docker-create.1.md @@ -26,6 +26,7 @@ docker-create - Create a new container [**--ipc**[=*IPC*]] [**--link**[=*[]*]] [**--lxc-conf**[=*[]*]] +[**--log-driver**[=*[]*]] [**-m**|**--memory**[=*MEMORY*]] [**--memory-swap**[=*MEMORY-SWAP*]] [**--mac-address**[=*MAC-ADDRESS*]] @@ -108,6 +109,10 @@ IMAGE [COMMAND] [ARG...] **--lxc-conf**=[] (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1" +**--log-driver**="|*json-file*|*none*" + Logging driver for container. Default is defined by daemon `--log-driver` flag. + **Warning**: `docker logs` command works only for `json-file` logging driver. + **-m**, **--memory**="" Memory limit (format: , where unit = b, k, m or g) diff --git a/docs/man/docker-logs.1.md b/docs/man/docker-logs.1.md index d55e8d8365..01a15f54dc 100644 --- a/docs/man/docker-logs.1.md +++ b/docs/man/docker-logs.1.md @@ -22,6 +22,8 @@ The **docker logs --follow** command combines commands **docker logs** and **docker attach**. It will first return all logs from the beginning and then continue streaming new output from the container’s stdout and stderr. +**Warning**: This command works only for **json-file** logging driver. + # OPTIONS **--help** Print usage statement diff --git a/docs/man/docker-run.1.md b/docs/man/docker-run.1.md index 587b9a2cd5..36ae9ce422 100644 --- a/docs/man/docker-run.1.md +++ b/docs/man/docker-run.1.md @@ -27,6 +27,7 @@ docker-run - Run a command in a new container [**--ipc**[=*IPC*]] [**--link**[=*[]*]] [**--lxc-conf**[=*[]*]] +[**--log-driver**[=*[]*]] [**-m**|**--memory**[=*MEMORY*]] [**--memory-swap**[=*MEMORY-SWAP]] [**--mac-address**[=*MAC-ADDRESS*]] @@ -209,6 +210,10 @@ which interface and port to use. **--lxc-conf**=[] (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1" +**--log-driver**="|*json-file*|*none*" + Logging driver for container. Default is defined by daemon `--log-driver` flag. + **Warning**: `docker logs` command works only for `json-file` logging driver. + **-m**, **--memory**="" Memory limit (format: , where unit = b, k, m or g) diff --git a/docs/man/docker.1.md b/docs/man/docker.1.md index bad6cf0494..c322c79ec9 100644 --- a/docs/man/docker.1.md +++ b/docs/man/docker.1.md @@ -82,6 +82,10 @@ unix://[/path/to/socket] to use. **--label**="[]" Set key=value labels to the daemon (displayed in `docker info`) +**--log-driver**="*json-file*|*none*" + Container's logging driver. Default is `default`. + **Warning**: `docker logs` command works only for `json-file` logging driver. + **--mtu**=VALUE Set the containers network mtu. Default is `1500`. diff --git a/docs/sources/reference/api/docker_remote_api_v1.18.md b/docs/sources/reference/api/docker_remote_api_v1.18.md index 2573f0be49..7395e824c7 100644 --- a/docs/sources/reference/api/docker_remote_api_v1.18.md +++ b/docs/sources/reference/api/docker_remote_api_v1.18.md @@ -252,6 +252,7 @@ Json Parameters: - **LogConfig** - Logging configuration to container, format `{ "Type": "", "Config": {"key1": "val1"}} Available types: `json-file`, `none`. + `json-file` logging driver. Query Parameters: @@ -441,6 +442,9 @@ Status Codes: Get stdout and stderr logs from the container ``id`` +> **Note**: +> This endpoint works only for containers with `json-file` logging driver. + **Example request**: GET /containers/4fa6e0f0c678/logs?stderr=1&stdout=1×tamps=1&follow=1&tail=10 HTTP/1.1 diff --git a/docs/sources/reference/commandline/cli.md b/docs/sources/reference/commandline/cli.md index f6b7491d06..da274ca109 100644 --- a/docs/sources/reference/commandline/cli.md +++ b/docs/sources/reference/commandline/cli.md @@ -1405,6 +1405,9 @@ For example: -t, --timestamps=false Show timestamps --tail="all" Number of lines to show from the end of the logs +NOTE: this command is available only for containers with `json-file` logging +driver. + The `docker logs` command batch-retrieves logs present at the time of execution. The `docker logs --follow` command will continue streaming the new output from diff --git a/docs/sources/reference/run.md b/docs/sources/reference/run.md index 6ccfc5c3fe..c578307032 100644 --- a/docs/sources/reference/run.md +++ b/docs/sources/reference/run.md @@ -565,13 +565,15 @@ familiar with using LXC directly. You can specify a different logging driver for the container than for the daemon. -### Log driver: none +### Logging driver: none -Disables any logging for the container. +Disables any logging for the container. `docker logs` won't be available with +this driver. ### Log driver: json-file -Default logging driver for Docker. Writes JSON messages to file. +Default logging driver for Docker. Writes JSON messages to file. `docker logs` +command is available only for this logging driver ## Overriding Dockerfile image defaults diff --git a/integration-cli/docker_cli_daemon_test.go b/integration-cli/docker_cli_daemon_test.go index aa2b228cd1..552342a687 100644 --- a/integration-cli/docker_cli_daemon_test.go +++ b/integration-cli/docker_cli_daemon_test.go @@ -703,3 +703,26 @@ func TestDaemonLoggingDriverNoneOverride(t *testing.T) { } logDone("daemon - 'none' logging driver override in run") } + +func TestDaemonLoggingDriverNoneLogsError(t *testing.T) { + d := NewDaemon(t) + + if err := d.StartWithBusybox("--log-driver=none"); err != nil { + t.Fatal(err) + } + defer d.Stop() + + out, err := d.Cmd("run", "-d", "busybox", "echo", "testline") + if err != nil { + t.Fatal(out, err) + } + id := strings.TrimSpace(out) + out, err = d.Cmd("logs", id) + if err == nil { + t.Fatalf("Logs should fail with \"none\" driver") + } + if !strings.Contains(out, `\"logs\" command is supported only for \"json-file\" logging driver`) { + t.Fatalf("There should be error about non-json-file driver, got %s", out) + } + logDone("daemon - logs not available for non-json-file drivers") +}