{"id":242587,"date":"2021-05-05T15:00:34","date_gmt":"2021-05-05T12:00:34","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-monitor-docker-container-logs-cloudsavvy-it\/"},"modified":"2021-05-05T15:00:34","modified_gmt":"2021-05-05T12:00:34","slug":"how-to-monitor-docker-container-logs-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-docker-container-logs-cloudsavvy-it\/","title":{"rendered":"#How to Monitor Docker Container Logs \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a389a6756539\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a389a6756539\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-docker-container-logs-cloudsavvy-it\/#Viewing_Container_Logs\" >Viewing Container Logs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-docker-container-logs-cloudsavvy-it\/#Customising_Whats_Displayed\" >Customising What\u2019s Displayed<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-docker-container-logs-cloudsavvy-it\/#Docker_Logging_Drivers\" >Docker Logging Drivers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-docker-container-logs-cloudsavvy-it\/#Specifying_a_Logging_Driver\" >Specifying a Logging Driver<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-docker-container-logs-cloudsavvy-it\/#Driver_Delivery_Modes\" >Driver Delivery Modes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-docker-container-logs-cloudsavvy-it\/#Logging_Best_Practices\" >Logging Best Practices<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-docker-container-logs-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Monitor Docker Container Logs \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-9034\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1602\" height=\"902\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Docker automatically aggregates container standard output and error streams (<code>stdout<\/code>\/<code>stderr<\/code>) into log feeds which are retained by the Docker daemon. You can easily monitor logs to understand what\u2019s going on in your containers.<\/p>\n<p>The logs contain the output you\u2019d see in your terminal when attached to a container in interactive mode (<code>-it<\/code>). Logs will only be available if the foreground process in your container actually emits some output. You should make sure your containers log errors to <code>stderr<\/code> so Docker commands are able to expose them.<\/p>\n<p>You can view container logs at any time during a container\u2019s lifecycle. When a container\u2019s running, you can stream logs in realtime. For stopped containers, you can access all the logs captured prior to the termination.<\/p>\n<h2 id=\"viewing-container-logs\"><span class=\"ez-toc-section\" id=\"Viewing_Container_Logs\"><\/span>Viewing Container Logs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To view <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/reference\/commandline\/logs\">container logs<\/a>, use the <code>docker logs<\/code> command:<\/p>\n<pre>docker logs my-container<\/pre>\n<p>Replace <code>my-container<\/code> with the name or ID of the container you want to inspect. You can use <code>docker ps -a<\/code> to get the IDs and names of your containers.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10957\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/29fe589e.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"842\" height=\"385\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The <code>logs<\/code> command prints the container\u2019s entire log output to your terminal. The output will not be continuous. If you\u2019d like to keep streaming new logs, add the <code>--follow<\/code> flag to the command. This is equivalent to using <code>tail -f<\/code> with regular log files on your machine.<\/p>\n<h2 id=\"customising-whats-displayed\"><span class=\"ez-toc-section\" id=\"Customising_Whats_Displayed\"><\/span>Customising What\u2019s Displayed<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>docker logs<\/code> command supports several flags that let you adjust its output:<\/p>\n<ul>\n<li><strong>\u2013timestamps<\/strong> \u2013 Display complete timestamps at the start of each log line.<\/li>\n<li><strong>\u2013until and <code>--since<\/code><\/strong> \u2013 These flags let you fetch lines logged during a particular time period. Either pass a complete timestamp (<code>2021-04-30T20:00:00Z<\/code>) or a friendly relative time (e.g.\u00a0<code>1h<\/code> = 1 hour ago).<\/li>\n<li><strong>\u2013tail<\/strong> \u2013 Fetch a given number of lines from the log. <code>--tail 10<\/code> will display the last ten lines logged by the container.<\/li>\n<li><strong>\u2013details<\/strong> \u2013 This is a special flag which adds extra information to the log output, based on the options passed to the logging driver. We\u2019ll look at logging drivers in the next section. Typical values displayed with <code>--details<\/code> include container labels and environment variables.<\/li>\n<\/ul>\n<p>You can combine these flags to get logs in the format you require. The <code>until<\/code>, <code>since<\/code> and <code>tail<\/code> flags won\u2019t take effect if you\u2019re using <code>follow<\/code> to continuously stream log data.<\/p>\n<h2 id=\"docker-logging-drivers\"><span class=\"ez-toc-section\" id=\"Docker_Logging_Drivers\"><\/span>Docker Logging Drivers<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker collects and stores container logs using one of several <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/config\/containers\/logging\/configure\/#supported-logging-drivers\">logging drivers<\/a>. You can set the active logging driver on a per-container basis. When no logging driver is specified, Docker uses the <code>json-file<\/code> driver.<\/p>\n<p>This driver stores container logs in a JSON file. This format is fairly human-readable and can be readily consumed by third-party tools. If you\u2019re not going to access log files directly, switching to the <code>local<\/code> driver will save you some storage space. It uses a custom log storage format.<\/p>\n<p>Other built-in log drivers include <code>syslog<\/code> (write to the <code>syslog<\/code> daemon running on your machine), <code>journald<\/code> (use a running <code>journald<\/code> instance) and <code>fluentd<\/code> (to use a <code>fluentd<\/code> daemon). Drivers are also available for Amazon CloudWatch, Google Cloud Platform, Event Tracing for Windows and other log monitoring solutions.<\/p>\n<p>Docker supports third-party logging drivers via plugins. You can find drivers on Docker Hub. To install a plugin driver, run <code>docker plugin install plugin-name<\/code>. You\u2019ll then be able to reference it as a logging driver as <code>plugin-name<\/code>.<\/p>\n<h2 id=\"specifying-a-logging-driver\"><span class=\"ez-toc-section\" id=\"Specifying_a_Logging_Driver\"><\/span>Specifying a Logging Driver<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can specify the logging driver for a container by passing the <code>--log-driver<\/code> flag to <code>docker run<\/code>:<\/p>\n<pre>docker run --log-driver systemd my-image:latest<\/pre>\n<p>You can change the default logging driver globally by updating your Docker daemon configuration. Edit (or create) <code>\/etc\/docker\/daemon.json<\/code>. Set the <code>log-driver<\/code> key to the name of a logging driver. Docker will use this driver for all containers created without a <code>--log-driver<\/code> flag.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"json\">\n<pre class=\"de1\">{&#13;\n    \"log-driver\": \"systemd\"&#13;\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Many logging drivers come with their own configuration options. These are set using the <code>--log-opts<\/code> container flag, or <code>log-opts<\/code> in <code>daemon.json<\/code>. Here\u2019s an example relevant to the default <code>json-file<\/code> driver. It instructs Docker to rotate log files once they\u2019re larger than 8MB. Only five files will be retained at any time.<\/p>\n<p><strong><code>docker run<\/code><\/strong><\/p>\n<pre>docker run --log-driver json-file --log-opts max-size=8M --log-opts max-file=5<\/pre>\n<p><strong><code>\/etc\/docker\/daemon.json<\/code><\/strong><\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"json\">\n<pre class=\"de1\">{&#13;\n    \"log-driver\": \"json-file\",&#13;\n    \"log-opts\": {&#13;\n        \"max-size\": \"8M\",&#13;\n        \"max-file\": 5&#13;\n    }&#13;\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2 id=\"driver-delivery-modes\"><span class=\"ez-toc-section\" id=\"Driver_Delivery_Modes\"><\/span>Driver Delivery Modes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Logs can be delivered in either blocking or non-blocking modes. Docker defaults to blocking delivery. Logs from the container will be sent to the driver im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely. This guarantees log delivery but could impact performance. The <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lication will wait until the log write is complete. This can cause a perceptible delay if the logging driver is busy.<\/p>\n<p>When in non-blocking mode, Docker writes logs to an in-memory buffer. The container doesn\u2019t need to wait for the logging driver to complete its write. This can significantly improve performance on active machines with slow storage.<\/p>\n<p>The tradeoff with non-blocking mode is the possibility of lost logs. This can occur when logs are emitted more quickly than the driver can process them. The in-memory buffer could be filled, causing cached logs to be cleared before they\u2019ve been handed to the driver.<\/p>\n<p>You can enable non-blocking delivery by setting the <code>mode<\/code> logging option, either with <code>--log-opts<\/code> or <code>daemon.json<\/code>. You can set the size of the in-memory log buffer with the <code>max-buffer-size<\/code> option. Setting this high reduces the risk of lost logs, provided you\u2019ve got sufficient RAM available.<\/p>\n<pre>docker run --log-opt mode=non-blocking --log-opt max-buffer-size=8M my-image:latest<\/pre>\n<h2 id=\"logging-best-practices\"><span class=\"ez-toc-section\" id=\"Logging_Best_Practices\"><\/span>Logging Best Practices<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Your containers should work with Docker\u2019s logging system wherever possible. Emitting logs to <code>stdout<\/code> and <code>stderr<\/code> allows Docker and other tools to aggregate them in a standardised way.<\/p>\n<p>Log output doesn\u2019t need to include timestamps. Docker\u2019s logging drivers will automatically record the time at which an event occurred.<\/p>\n<p>Sometimes you might have complex logging requirements that <code>docker logs<\/code> alone can\u2019t satisfy. If that\u2019s the case, you might need to implement your own logging solution within your container. You can store logs directly on the filesystem, using a Docker volume, or call an external API service.<\/p>\n<p>Some stacks call for a dedicated logging container that sits alongside your application containers. The logging container, often called a \u201csidecar\u201d, reads temporary log files which your application containers create in a shared Docker volume. The sidecar handles the aggregation of these logs into a format which can be uploaded to a log monitoring service.<\/p>\n<p>This approach can be useful for more complex deployments, although it\u2019s trickier to setup and scale. It typically leaves you without the immediate convenience of Docker\u2019s built-in log commands.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker has versatile log monitoring capabilities provided by a suite of logging drivers. Each container can use a unique logging driver, letting you store logs in a format appropriate to each app\u2019s requirements.<\/p>\n<p>Logs include anything emitted by a container\u2019s standard output streams. You can use <code>echo<\/code>, <code>print<\/code>, <code>console.log()<\/code> or your programming language\u2019s equivalent to add lines to the <code>docker logs<\/code> output. Logs are retained until your container is deleted with <code>docker rm<\/code>.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/10953\/how-to-monitor-docker-container-logs\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Monitor Docker Container Logs \u2013 CloudSavvy IT&#8221; Docker automatically aggregates container standard output and error streams (stdout\/stderr) into log feeds which are retained by the Docker daemon. You can easily monitor logs to understand what\u2019s going on in your containers. The logs contain the output you\u2019d see in your terminal when attached to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":242588,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-242587","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/242587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=242587"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/242587\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/242588"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=242587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=242587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=242587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}