<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tai Le Tien on Medium]]></title>
        <description><![CDATA[Stories by Tai Le Tien on Medium]]></description>
        <link>https://medium.com/@letientai299?source=rss-13e4a60befbc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*GYNze53i2hPQmkne.jpg</url>
            <title>Stories by Tai Le Tien on Medium</title>
            <link>https://medium.com/@letientai299?source=rss-13e4a60befbc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 11:08:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@letientai299/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Handle docker stop properly for containers that start with shell script]]></title>
            <link>https://medium.com/@letientai299/handle-docker-stop-properly-for-containers-that-start-with-shell-script-2ef4a1941e35?source=rss-13e4a60befbc------2</link>
            <guid isPermaLink="false">https://medium.com/p/2ef4a1941e35</guid>
            <category><![CDATA[unix]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[shell-script]]></category>
            <category><![CDATA[shell]]></category>
            <dc:creator><![CDATA[Tai Le Tien]]></dc:creator>
            <pubDate>Fri, 14 Dec 2018 12:36:03 GMT</pubDate>
            <atom:updated>2018-12-14T12:36:03.240Z</atom:updated>
            <content:encoded><![CDATA[<p>As I’m writing more and more wiki page on both Gitlab and Github, I’ve started to build <a href="https://github.com/letientai299/gollum-reload">gollum-reload</a>, a docker image to run Gollum and Guard that will monitor our wiki folder and reload the web browser on file change.</p><p>Since gollum-reload is basically a docker image, during building it, I need to rebuild, stop and start it a lot. Build image and start new container it very fast. But stop the running container is painfully slow. I’ve added a <a href="https://github.com/letientai299/gollum-reload/blob/f464b73b57c51fc9da2d794017b0c1dc9f010fdd/scripts/stop.sh">dirty work around</a> to make stopping the container faster. But end users of gollum-reload image won’t be able to use it easily, unless they embedded my project into their wiki. So, there’s the need to fix that problem properly.</p><p>For the rest of this post, we will use another image to demonstrate and explain several approaches.</p><h3>Problem</h3><p>docker stop command can takes quite long time to stop a container that running a script.</p><h3>Example</h3><p>Let’s say we want to have a customized redis container that need to do something before starting redis-server.</p><p>Here’s our Dockerfile</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98fbdc43d5d8bdbe3c71c6c9ef06c45e/href">https://medium.com/media/98fbdc43d5d8bdbe3c71c6c9ef06c45e/href</a></iframe><p>And here’s the runner.sh</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e26fd161f2fb8a213e2f5e3b5679832b/href">https://medium.com/media/e26fd161f2fb8a213e2f5e3b5679832b/href</a></iframe><p>Pretty simple! Now, let’s build the image</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b0f85a6a877538899ab7118fd928235/href">https://medium.com/media/1b0f85a6a877538899ab7118fd928235/href</a></iframe><p>And run it, without detaching to see redis log</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72c447b558ffa8b4dff5d6b6208d875f/href">https://medium.com/media/72c447b558ffa8b4dff5d6b6208d875f/href</a></iframe><p><strong>Note</strong></p><ul><li>Since this is just a throw away container, we use --rm to remove it once it&#39;s stopped.</li><li>It’s a best practice to give giving the container a proper name (actually, everything should have a proper name). So that we quickly recognize what is it used for, and we can write other script that target to exactly that container.</li></ul><p>Now, try stopping that container with the usual docker stop, also timing how long until it done:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f62cb89098c9768af5293c55f08aaa81/href">https://medium.com/media/f62cb89098c9768af5293c55f08aaa81/href</a></iframe><p>I don’t know about you, but to me, 10s to stop the containers is pretty long.</p><h3>Why docker stop take that much time?</h3><p>docker stop attempts <a href="https://turnoff.us/geek/dont-sigkill/">to be nice</a>. It first sendsSIGTERM, expect our applications to catch the signal, clean thing up and kill themselves. Most application will catch that and stop nicely.</p><p>In this case, however, redis-sever is started within our script runner.sh, which is ran by /bin/sh. Check that using docker stop</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/298ed42d331be28b4965b21da1218b53/href">https://medium.com/media/298ed42d331be28b4965b21da1218b53/href</a></iframe><p>The process that will handle SIGTERM is actually /bin/sh. It won&#39;t forward the signal to child process. That&#39;s why redis-server won&#39;t stop.</p><p>And after some time (<a href="https://docs.docker.com/engine/reference/commandline/stop/">default timeout is 10s</a>), docker will forcefully kill the application by sending SIGKILL. /bin/sh and all its child will be killed.</p><h3>Work around</h3><p>If our applications don’t need cleaning up on shutdown, we can just use docker kill.</p><p>But for some application, like database container that save to data to mounted volume, graceful shutdown is a must.</p><h3>Make docker stop work properly</h3><p>There’s 2 ways, depends on your actual situation</p><h3>Script that spawn only one application</h3><p>We can use exec to run redis-server, like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8ef8ec13b514c7b2f64771b7f20747f/href">https://medium.com/media/b8ef8ec13b514c7b2f64771b7f20747f/href</a></iframe><p>exec will <a href="https://stackoverflow.com/a/18351547/3869533">replaces the current program in the current process</a>, so redis-server will completely in control for the process that starts it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15749fb6ee50a865b5a2b30eed1deb7e/href">https://medium.com/media/15749fb6ee50a865b5a2b30eed1deb7e/href</a></iframe><p>After that, SIGTERM from docker stop will go into redis-server and is handled nicely.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ffd6c789052f86f52069daeaede6010/href">https://medium.com/media/3ffd6c789052f86f52069daeaede6010/href</a></iframe><h3>Script that will start multiple processes</h3><p>Unix have a <a href="http://man7.org/linux/man-pages/man1/trap.1p.html">trap</a> function that can be used to catches and handle a signal using some commands.</p><h4>Trap example</h4><p>Create a file named trap_example.sh with following content</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f897f330d5bfa6d2d4c31647e7fe6dc/href">https://medium.com/media/1f897f330d5bfa6d2d4c31647e7fe6dc/href</a></iframe><p>Try run it and then stop it, you will find that it won’t top on Ctrl-C, which will send SIGINT, because we already handle that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94c0e41cbbc535f00cb6f75463fedad1/href">https://medium.com/media/94c0e41cbbc535f00cb6f75463fedad1/href</a></iframe><p>It also won’t stop on SIGNTERM, which is sent from kill or pkill by default:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/231077f273dc8d997c80acb283be95e9/href">https://medium.com/media/231077f273dc8d997c80acb283be95e9/href</a></iframe><p>So, how to kill it then? You will need to use SIGKILL, which is, by design, can&#39;t be trapped by the application. All application receive that signal will be ... killed. Use following command to kill it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93a78fd70df2c637e069558bcaf35ac3/href">https://medium.com/media/93a78fd70df2c637e069558bcaf35ac3/href</a></iframe><h4>Handling SIGTERM properly</h4><p>If we add trap handling naively, like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a331926c7c7e95d3ce8d34e93346587/href">https://medium.com/media/5a331926c7c7e95d3ce8d34e93346587/href</a></iframe><p>It won’t work, because redis-server run in the foreground and doesn’t return control to the script. Thus, sh can&#39;t execute it&#39;s trap handler. We need to run redis in background instead.</p><p>Our final implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8955686f846fa7a2c9e3d23d0cfbdba9/href">https://medium.com/media/8955686f846fa7a2c9e3d23d0cfbdba9/href</a></iframe><p>Now, docker stop should be quick as usual.</p><h3>Conclusion</h3><p>As usual, we learn many thing as we keep try to improve what we have. I’ve learn some new things about Unix shell and I hope that this post also help you so.</p><p>Here’s is my <a href="https://github.com/letientai299/gollum-reload/blob/7bbefec2b3c70a31441c941fb0481f156732aa9e/docker/run.sh">current implementation</a> for run.sh in gollum-reload, to serve as an example of a real project.</p><p>The source code for this post can be found <a href="https://github.com/letientai299/learn-docker/tree/master/handle-sigterm">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2ef4a1941e35" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>