{"id":489073,"date":"2022-08-31T03:48:47","date_gmt":"2022-08-31T00:48:47","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-clear-logs-of-running-docker-containers\/"},"modified":"2022-08-31T03:48:47","modified_gmt":"2022-08-31T00:48:47","slug":"how-to-clear-logs-of-running-docker-containers","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-clear-logs-of-running-docker-containers\/","title":{"rendered":"#How to Clear Logs of Running Docker Containers"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 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-6a27d22597fd4\" 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-6a27d22597fd4\" 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-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-clear-logs-of-running-docker-containers\/#%E2%80%9CHow_to_Clear_Logs_of_Running_Docker_Containers%E2%80%9D\" >&#8220;How to Clear Logs of Running Docker Containers&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-clear-logs-of-running-docker-containers\/#Understanding_the_Problem\" >Understanding the Problem<\/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-clear-logs-of-running-docker-containers\/#Discovering_the_Log_Files_Path\" >Discovering the Log File\u2019s Path<\/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-clear-logs-of-running-docker-containers\/#Clearing_the_Log_File\" >Clearing the Log File<\/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-clear-logs-of-running-docker-containers\/#Limiting_Log_Output\" >Limiting Log Output<\/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-clear-logs-of-running-docker-containers\/#Setting_Up_Log_Rotation\" >Setting Up Log Rotation<\/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-clear-logs-of-running-docker-containers\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Clear_Logs_of_Running_Docker_Containers%E2%80%9D\"><\/span>&#8220;How to Clear Logs of Running Docker Containers&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-805981\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Docker-New.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graphic showing the Docker logo\" width=\"1600\" height=\"900\"\/><\/p>\n<p>Long-running Docker containers can rapidly accumulate a large number of log lines. These consume storage capacity and reduce performance when accessing and filtering the data. While Docker includes integrated tools for viewing the logs, there\u2019s no built-in mechanism for cleaning them up.<\/p>\n<p>In this article you\u2019ll learn how to clear up the logs of running Docker containers, without restarting or replacing them. You\u2019ll also see some techniques for more efficiently handling large logs while retaining old data.<\/p>\n<h2 id=\"understanding-the-problem\"><span class=\"ez-toc-section\" id=\"Understanding_the_Problem\"><\/span>Understanding the Problem<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker collects logs from the standard output and error streams of container foreground processes. The <code>docker logs<\/code> command is used to retrieve these logs but it doesn\u2019t let you delete old lines.<\/p>\n<p>Docker supports many different logging drivers so it\u2019s not possible to offer a standardized clean-up mechanism. This article focuses on the standard <code>json-file<\/code> logging driver, where log lines are stored in a JSON file on your container host\u2019s filesystem. Refer to your storage driver\u2019s documentation if you need to clean up logs streamed to a remote host.<\/p>\n<p>Log files created by the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/config\/containers\/logging\/configure\"><code>json-file<\/code> driver<\/a> are stored under the <code>\/var\/lib\/docker\/containers<\/code> directory. Each container gets its own log file which is used throughout its lifetime. There\u2019s no log rotation configured by default.<\/p>\n<p>Simply deleting the log file isn\u2019t an effective solution. Docker expects the file to be available continually and won\u2019t recreate it automatically if it\u2019s removed. It\u2019s safer to <em>clear<\/em> the existing log\u2019s contents to avoid affecting any current writes.<\/p>\n<h2 id=\"discovering-the-log-files-path\"><span class=\"ez-toc-section\" id=\"Discovering_the_Log_Files_Path\"><\/span>Discovering the Log File\u2019s Path<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>First find the path to your target container\u2019s log file. You can retrieve the log file path for a container called <code>my-<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><\/code> by running the following command:<\/p>\n<pre>$ docker inspect --format=\"{{.LogPath}}\" my-app&#13;\n\/var\/lib\/containers\/1380d936...-json.log<\/pre>\n<h2 id=\"clearing-the-log-file\"><span class=\"ez-toc-section\" id=\"Clearing_the_Log_File\"><\/span>Clearing the Log File<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can clear out the contents of the log without deleting it by echoing an empty string into its content. The file will be owned by <code>root<\/code> so you\u2019ll need to perform this operation in a root shell. The following command will empty the log file for you:<\/p>\n<pre>$ sudo sh -c 'echo \"\" &gt; $(docker inspect --format=\"{{.LogPath}}\" my-app)'<\/pre>\n<p>Shell interpolation is being used to dynamically retrieve the log file path for the <code>my-app<\/code> container. You could manually substitute in the path retrieved earlier instead.<\/p>\n<p>When you run <code>docker logs my-app<\/code>, you\u2019ll now see empty output unless the container has resumed writing lines in the interim.<\/p>\n<h2 id=\"limiting-log-output\"><span class=\"ez-toc-section\" id=\"Limiting_Log_Output\"><\/span>Limiting Log Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>docker logs<\/code> command accepts a few arguments that can be used to limit the output from noisy logs. These help mitigate the need to clean the log while preventing your terminal from being inundated with old lines each time you access the file.<\/p>\n<ul>\n<li><strong><code>--tail<\/code><\/strong> \u2013 This flag instructs Docker to emit only a specific number of old log lines, starting with the most recent.<\/li>\n<li><strong><code>--until<\/code> and <code>--since<\/code><\/strong> \u2013 These flags restrict the output to log lines emitted during a specific time period. They\u2019re useful when you know the approximate timeframe in which an event occurred.<\/li>\n<\/ul>\n<p>Here are a few examples:<\/p>\n<p><strong>Display the 100 most recent log lines<\/strong><\/p>\n<p><code>docker logs my-app --tail 100<\/code><\/p>\n<p><strong>Display log lines written in the past hour<\/strong><\/p>\n<p><code>docker logs my-app --since 1h<\/code><\/p>\n<p><strong>Display up to 100 log lines written in the past hour<\/strong><\/p>\n<p><code>docker logs my-app --tail 100 --since 1h<\/code><\/p>\n<h2 id=\"setting-up-log-rotation\"><span class=\"ez-toc-section\" id=\"Setting_Up_Log_Rotation\"><\/span>Setting Up Log Rotation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Regularly having to clean up log files in this manner usually signals that your application\u2019s logs are either excessively verbose or there\u2019s simply too much activity to store in one file.<\/p>\n<p>Many Docker logging drivers, including <code>json-file<\/code>, have optional log rotation support that you can enable globally for the Docker daemon or on a per-container basis.<\/p>\n<p>Daemon settings are configured in <code>\/etc\/docker\/daemon.json<\/code>. Here\u2019s an example that rotates container logs once they reach 8MB. Up to five files will be retained at any time, with old ones automatically deleted as new rotations occur.<\/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-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<p>Restart the Docker daemon to apply the change:<\/p>\n<pre>$ sudo service docker restart<\/pre>\n<p>Daemon-level rotation applies to all newly created containers. Changes won\u2019t affect any containers that already exist on your host.<\/p>\n<p>Rotation can be configured for individual containers using <code>--log-opts<\/code> flags. These will override your default Docker daemon settings.<\/p>\n<pre>docker run --name app \\&#13;\n    --log-driver json-file \\&#13;\n    --log-opts max-size=8M \\&#13;\n    --log-opts max-file=5 \\&#13;\n    app-image:latest<\/pre>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker container logs can get noisy when busy applications write extensive output. Access to verbose logs is helpful for debugging but they consume storage space, make it harder to sift through data, and could reduce performance.<\/p>\n<p>You can clean up log files on-demand by using shell commands to empty their content. However it\u2019s better practice to set up log rotation so this occurs automatically once files reach a specific size. The <code>docker logs<\/code> command also supports flags that can help tame unwieldy logs at the point of access.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\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.howtogeek.com\/devops\/how-to-clear-logs-of-running-docker-containers\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Clear Logs of Running Docker Containers&#8221; Long-running Docker containers can rapidly accumulate a large number of log lines. These consume storage capacity and reduce performance when accessing and filtering the data. While Docker includes integrated tools for viewing the logs, there\u2019s no built-in mechanism for cleaning them up. In this article you\u2019ll learn&#8230;<\/p>\n","protected":false},"author":1,"featured_media":489074,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Docker-New.jpeg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-489073","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\/489073","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=489073"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/489073\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/489074"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=489073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=489073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=489073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}