{"id":9299,"date":"2023-02-10T10:13:31","date_gmt":"2023-02-10T02:13:31","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9299"},"modified":"2023-02-10T10:13:31","modified_gmt":"2023-02-10T02:13:31","slug":"wheres-that-log-file-debugging-failed-docker-builds","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9299","title":{"rendered":"Where\u2019s that log file? Debugging failed Docker builds"},"content":{"rendered":"\n<p>You\u2019ve got a nice new <code>Dockerfile<\/code>, and it\u2019s time to try it out:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ docker build -t mynewimage .\nSending build context to Docker daemon  3.072kB\nStep 1\/3 : FROM python:3.8-slim-buster\n ---> 3d8f801fc3db\nStep 2\/3 : COPY build.sh .\n ---> 541b65a7b417\nStep 3\/3 : RUN .\/build.sh\n ---> Running in 9917e3865f96\nBuilding...\nBuilding some more...\nBuild failed, see \/tmp\/builderr024321.log for details\nThe command '\/bin\/sh -c .\/build.sh' returned a non-zero code: 1\n<\/pre>\n\n\n\n<p>So\u2014now what? That log file isn\u2019t on your host computer\u2019s filesystem, it\u2019s in the temporary image that build was creating.<\/p>\n\n\n\n<p>How do you read that log file? Let\u2019s find out.<\/p>\n\n\n\n<p><strong>Debugging the build<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>That log file isn\u2019t on your host computer\u2019s filesystem, it\u2019s in the temporary image that build was creating. In some versions of Docker you can access this temporary image, but not in the <a href=\"https:\/\/pythonspeed.com\/articles\/docker-buildkit\/\">new and mostly-improved BuildKit build system<\/a>. So we\u2019re going to take a different approach.<\/p>\n\n\n\n<p>Specifically, what we\u2019re going to do is rebuild the image, disabling the failed line and all lines after it. Our <code>Dockerfile<\/code> currently looks like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">FROM python:3.8-slim-buster\nCOPY build.sh .\nRUN chmod +x build.sh\nRUN .\/build.sh\n<\/pre>\n\n\n\n<p>You want to make sure changes to <code>Dockerfile<\/code> don\u2019t impact layer caching; you can do so by having a <code>.dockerignore<\/code> that includes the <code>Dockerfile<\/code>. In the case of our example, we\u2019re only <code>COPY<\/code>ing in <code>build.sh<\/code> so changes to <code>Dockerfile<\/code> won\u2019t invalidate the cache.<\/p>\n\n\n\n<p>We comment out the failed line, and the <code>Dockerfile<\/code> now looks like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">FROM python:3.8-slim-buster\nCOPY build.sh .\nRUN chmod +x build.sh\n#RUN .\/build.sh\n<\/pre>\n\n\n\n<p>We can now rebuild the image:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ docker build -t mynewimage .\n...\nSuccessfully built 7759cef14db8\nSuccessfully tagged mynewimage:latest<\/pre>\n\n\n\n<p>Now we can run the image, and then manually run the failing step, and since we\u2019re inside the container we can also access the resulting log file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ docker run -it mynewimage \/bin\/bash\nroot@c6060c04282d:\/# .\/build.sh \nBuild failed, see \/tmp\/builderr024321.log for details\nroot@c6060c04282d:\/# cat \/tmp\/builderr024321.log \nONO MISSING FLUX CAPACITOR\nroot@c6060c04282d:\/# <\/pre>\n\n\n\n<p>And now we know the problem: apparently there\u2019s a missing flux capacitor.<\/p>\n\n\n\n<p>Once we\u2019ve fixed the problem, we can uncommit the last line(s) of the <code>Dockerfile<\/code> and continue with the build.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You\u2019ve got a nice new Dockerfile, and it\u2019s time to try  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9299"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9299"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9299\/revisions"}],"predecessor-version":[{"id":9300,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9299\/revisions\/9300"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}