<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Manish Demblani</title>
    <description>The latest articles on DEV Community by Manish Demblani (@manish_demblani).</description>
    <link>https://dev.to/manish_demblani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F14213%2Fc990bba8-2642-404c-b70a-fb41e67cd9f4.jpeg</url>
      <title>DEV Community: Manish Demblani</title>
      <link>https://dev.to/manish_demblani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manish_demblani"/>
    <language>en</language>
    <item>
      <title>Docker Container: Uncaught Kill Signal</title>
      <dc:creator>Manish Demblani</dc:creator>
      <pubDate>Wed, 08 Aug 2018 14:54:32 +0000</pubDate>
      <link>https://dev.to/mdemblani/docker-container-uncaught-kill-signal-10l6</link>
      <guid>https://dev.to/mdemblani/docker-container-uncaught-kill-signal-10l6</guid>
      <description>&lt;p&gt;Has it ever happened, that you are unable to terminate/stop a running Docker container instance. Trying to press the Ctrl+C (Cmd+C) in your terminal multiple times, but being hapless all the time. &lt;/p&gt;

&lt;p&gt;Ultimately you resort to killing the Docker process and in this way, resulting in your container stopping successfully with the famous &lt;code&gt;exit code 0&lt;/code&gt;(if you are lucky) or it getting killed forcefully with an exit error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpjx4kkadmdbik94b13lo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpjx4kkadmdbik94b13lo.jpg" alt="Docker kill corrupts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In-case an exit error occurs due to killing the container, it results in corruption of the data in the volume associated with that container and hence, you have to re-build your containers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhsbm3de099i654v7ilsg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhsbm3de099i654v7ilsg.png"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;There are two reasons due to which this happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The container does not listen to the kill signals such as SIGINT that is generated by Ctrl+C (Cmd+C) key combination&lt;/li&gt;
&lt;li&gt;The container shutdown process exceeds the timeout time specified by the docker process which it allots a container to shutdown, resulting in the docker process forcibly killing the container and hence the corruption.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;Now there are two known ways to solve the above stated problem:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Increase the Timeout&lt;/li&gt;
&lt;li&gt;Patch the Entry Script&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Increase the Timeout
&lt;/h4&gt;

&lt;p&gt;In this case, you simply inform the docker process to wait for some more time until the container performs a graceful shutdown. The value be which the timeout should be increased, should be a little more than the time the container takes to shutdown after detecting the signal. To get started, you can set the timeout to around 30 seconds as a safe-bet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stop &lt;span class="nt"&gt;-t&lt;/span&gt; 30 CONTAINER_NAME
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Patch the Entry Script
&lt;/h4&gt;

&lt;p&gt;In this method, we basically monkey-patch the entry file of the container with custom code, that allows us to detect the &lt;code&gt;SIGINT&lt;/code&gt; signal sent to the container. &lt;/p&gt;

&lt;p&gt;To patch the entry script, we will have to create a listener file(&lt;code&gt;signal-listener.sh&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;# A wrapper around /entrypoint.sh to trap the SIGINT signal (Ctrl+C) and forwards it to the mysql daemon&lt;/span&gt;
&lt;span class="c"&gt;# In other words : traps SIGINT and SIGTERM signals and forwards them to the child process as SIGTERM signals&lt;/span&gt;

signalListener&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &amp;amp;
    &lt;span class="nv"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$!&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="nb"&gt;trap&lt;/span&gt; &lt;span class="s2"&gt;"echo 'Stopping PID &lt;/span&gt;&lt;span class="nv"&gt;$pid&lt;/span&gt;&lt;span class="s2"&gt;'; kill -SIGTERM &lt;/span&gt;&lt;span class="nv"&gt;$pid&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; SIGINT SIGTERM

    &lt;span class="c"&gt;# A signal emitted while waiting will make the wait command return code &amp;gt; 128&lt;/span&gt;
    &lt;span class="c"&gt;# Let's wrap it in a loop that doesn't end before the process is indeed stopped&lt;/span&gt;
    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nb"&gt;kill&lt;/span&gt; &lt;span class="nt"&gt;-0&lt;/span&gt; &lt;span class="nv"&gt;$pid&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null 2&amp;gt;&amp;amp;1&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
        &lt;/span&gt;&lt;span class="nb"&gt;wait
    &lt;/span&gt;&lt;span class="k"&gt;done&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

signalListener /entrypoint.sh &lt;span class="nv"&gt;$@&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure the file is in the same folder as the &lt;code&gt;Dockerfile&lt;/code&gt; you will build.&lt;/p&gt;

&lt;p&gt;The next step, is to wrap the &lt;code&gt;entrypoint.sh&lt;/code&gt; file with the &lt;code&gt;signal-listener.sh&lt;/code&gt; file. In your &lt;code&gt;Dockerfile&lt;/code&gt; add the following lines at the very end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; signal-listener.sh /run.sh&lt;/span&gt;

&lt;span class="c"&gt;# Entrypoint overload to catch the ctrl+c and stop signals&lt;/span&gt;
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; ["/bin/bash", "/run.sh"]&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; [ADD_THE_DEFAULT_DOCKER_CMD_IF_ANY]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you observe, after running the docker container, you could press the Ctrl+C(Cmd+C) combination and pass it the SIGINT command. This command would now be detected by the container and it would terminate gracefully.&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;em&gt;Credits:&lt;/em&gt;
&lt;/h5&gt;

&lt;h5&gt;
  
  
  &lt;em&gt;This post is based on the solution provided in the following Github issue: &lt;a href="https://github.com/docker-library/mysql/issues/47" rel="noopener noreferrer"&gt;Container does not catch signals and exit (Ctrl+C)&lt;/a&gt;. Thanks to user &lt;a href="https://github.com/jeremyVignelles" rel="noopener noreferrer"&gt;Jérémy VIGNELLES&lt;/a&gt; for the solution provided.&lt;/em&gt;
&lt;/h5&gt;

</description>
      <category>docker</category>
      <category>dockercontainer</category>
      <category>dockerkill</category>
      <category>dockerstop</category>
    </item>
    <item>
      <title>Breaking out from the MySQL character-set hell</title>
      <dc:creator>Manish Demblani</dc:creator>
      <pubDate>Sat, 10 Mar 2018 16:49:25 +0000</pubDate>
      <link>https://dev.to/manish_demblani/breaking-out-from-the-mysql-character-set-hell--4k9</link>
      <guid>https://dev.to/manish_demblani/breaking-out-from-the-mysql-character-set-hell--4k9</guid>
      <description>&lt;h3&gt;
  
  
  The MySQL charset hell, a dangerous place to be in. Read to know how you fall in it, what to do if you find yourself in it and how to migrate from latin1 to utf8mb4.
&lt;/h3&gt;

&lt;p&gt;If you ever encountered databases and SQL (or tried to know what they are), you must’ve have stumbled upon MySQL (An open source relational database management system (RDBMS) based on Structured Query Language (SQL)). In MySQL, have you ever wondered what Character-Set and Collation mean?&lt;/p&gt;

&lt;p&gt;Has it ever occurred, that while querying for data in your database (using any MySQL GUI or CLI) you see &lt;a href="https://en.wikipedia.org/wiki/Gibberish"&gt;gobbledygook&lt;/a&gt;(garbled data)? For eg. You enter the following text in your application:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;English to Arabic….الإنجليزية إلى العربية&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;but on querying for the same data in your database table you see:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;English to Arabicâ€¦.Ø§Ù„Ø¥Ù†Ø¬Ù„ÙŠØ²ÙŠØ© Ø¥Ù„Ù‰ Ø§Ù„Ø¹Ø±Ø¨ÙŠØ©&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: Although this article focuses on the fact on how to move your database from latin1 to utf8mb4, you can follow this article to move between any two character-sets and collation.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  . . .
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Emergence and Rise of UTF-8
&lt;/h2&gt;

&lt;p&gt;Those who know how computer encoding works would know that for a long period of time, ASCII (American Standard Code for Information Interchange) was the default encoding standard. The 8-bit/1 byte character encoding covered all the characters in the English Language, Numbers and the most commonly used special characters (!,.* and so on…). But with time, many Non-English speakers also started using computers and eventually computers started supporting these foreign languages.&lt;/p&gt;

&lt;p&gt;So as to support these foreign languages, a good byte stream encoding system was required, which would support a wide-range of characters (English and foreign languages), since the ASCII encoding was just 1-byte and was already filled up from 0–255 (To see a list of supporting characters), a new multi-byte character set system was required. The search for a new system started in 1992 and in that year, &lt;strong&gt;UTF-1&lt;/strong&gt; was introduced, but due to its incompetence led to the development of &lt;strong&gt;UTF-8&lt;/strong&gt; in January 1993.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"When I set out to create MOS 865, do you think it just happened overnight? No. There was MOS 1, that burnt down my Dad’s garage. There was MOS 2 that would only schedule appointments in January. And 862 others that I learned from…": Pete Becker (Friends)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;UTF-8 (a.k.a. Unicode) quickly rose to prominence and major computer systems started adopting it mainly due to its backward compatibility with the existing ASCII system. In 2008, Google reported that Unicode became the most common encoding for HTML files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1qzHQwe4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/xidnnng2j6i4sdtj1t8q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1qzHQwe4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/xidnnng2j6i4sdtj1t8q.png" alt="Shows the usage of the main encodings on the web from 2001 to 2012 as recorded by Google."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to know more about what Character-Sets and encoding mean and how they work and help in Computers understand languages and Software Developers develop international software, then read what &lt;a href="https://www.joelonsoftware.com/about-me/"&gt;Joel Spolsky&lt;/a&gt; has to say in his blog &lt;a href="https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/"&gt;The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!).&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  . . .
&lt;/h1&gt;

&lt;h4&gt;
  
  
  This post was originally published on my &lt;a href="https://medium.com/@manish_demblani/breaking-out-from-the-mysql-character-set-hell-24c6a306e1e5"&gt;blog here&lt;/a&gt;. The reminder of this article covers the following topics:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;MySQL — Collation and Character Set&lt;/li&gt;
&lt;li&gt;Latin1 and MySQL&lt;/li&gt;
&lt;li&gt;MySQL Character-Set Hell — Reproducing The Problem&lt;/li&gt;
&lt;li&gt;Fixing The Problem&lt;/li&gt;
&lt;li&gt;Nth Encoded Characters&lt;/li&gt;
&lt;li&gt;Application Precautions&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>mysql</category>
      <category>unicode</category>
      <category>database</category>
      <category>charset</category>
    </item>
  </channel>
</rss>
