<?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 Sameer Bhatt on Medium]]></title>
        <description><![CDATA[Stories by Sameer Bhatt on Medium]]></description>
        <link>https://medium.com/@sameerbhatt?source=rss-bebf1f6e74b1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3KdA46OBfgCf1i8mHprA7w.jpeg</url>
            <title>Stories by Sameer Bhatt on Medium</title>
            <link>https://medium.com/@sameerbhatt?source=rss-bebf1f6e74b1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 12:20:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sameerbhatt/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[Time Management Hacks]]></title>
            <link>https://sameerbhatt.medium.com/time-management-hacks-844d4038c862?source=rss-bebf1f6e74b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/844d4038c862</guid>
            <category><![CDATA[time-management]]></category>
            <category><![CDATA[planning]]></category>
            <category><![CDATA[leadership]]></category>
            <category><![CDATA[decision-making]]></category>
            <category><![CDATA[ikigai]]></category>
            <dc:creator><![CDATA[Sameer Bhatt]]></dc:creator>
            <pubDate>Thu, 26 May 2022 07:08:50 GMT</pubDate>
            <atom:updated>2022-05-26T07:08:50.845Z</atom:updated>
            <content:encoded><![CDATA[<p>Distractions have been around for ages. However, with work from home, and the digital evolution in terms of our handy companion (yes, the smartphone!), distractions are now omnipresent. Imagine you start your work and get a WhatsApp message or a ping on your office chat or a call. Or you are a senior member of a team and keep getting messages about a meeting, or someone is asking for help, or your manager pinging you for something important.</p><p>Well, doing something deep requires focus and undivided attention. However, with such distractions, how do we concentrate?</p><p>And in this World of digital distractions — a ping here, a message there, a mail, there are a lot of context switches. Are these context switches really worth or do they just create un-necessary stress?<br>Some people adapt to it and figure out a way to deal with these distractions. While, some gets bogged down and are unable to find a way through managing their Time effectively.</p><p>It’s very important to maintain your Flow and you should strive to continue in the state of Flow despite these distractions.</p><blockquote>Flow is defined as — “<strong>the state in which people are so involved in an activity that nothing else seems to matter”</strong></blockquote><p>I would recommend to read the Book <a href="https://amzn.to/3KWRLQz">Ikigai</a> to understand it more. You can get it <a href="https://amzn.to/3KWRLQz">here</a>.</p><p>To maintain the state of Flow and use your Time effectively, here are some practical tips based on my learnings &amp; observations and what seemed to have worked for me -</p><p><strong>Conscious use of Eisenhower Matrix</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/477/1*1KgqfPHF1IA4c_lv_VQseQ.png" /></figure><p>Source: <a href="https://commons.wikimedia.org/w/index.php?curid=74528894%20https://en.wikipedia.org/wiki/First_Things_First_(book)">By Davidjcmorris — Own work, CC BY-SA 4.0</a></p><blockquote><strong>Take-away</strong> — If you are distracted, decide where the distraction lies?</blockquote><p><strong>Quadrant I</strong> — super urgent and important — leave everything and do it right away. Else, it will continue to occupy space in your mind and you will see yourself thinking about it more and more.</p><p><strong>Quadrant II</strong> — Not urgent but important — Take a note. I prefer a simple ToDo list — either simple text or a note in my diary.</p><p><strong>Quadrant III</strong> — Urgent but not important — Delegate it to someone else as it needs to be taken care of urgently but perhaps, not that important for you.</p><p><strong>Quadrant IV</strong> — If it’s not urgent and not important, ignore it unless someone reminds you again about a change in any of the dimension.</p><p><strong>Time to complete</strong></p><p>Along with the dimension of Urgent and Important, I would like you to consider another dimension of Time to complete. It’s not only important for you but also if you lead a team, for your team members too. As you would not want to break your team’s state of flow.</p><p>If the Time to complete is negligible and the task lies in Quadrant I, get done with it as soon as you can. However, if the Time to complete is significant, and perhaps require more folks to get involved, I would suggest to do some planning on how best you want to accomplish it, before discussing it with others.</p><p><strong>Silent mode</strong></p><p>Silent all your devices — It’s very important to achieve and be in a state of flow. Your WhatsApp messages, team chats, emails can wait for a few mins unless you are working on a time critical project.</p><p><strong>Block time</strong></p><p>Keep focus time in your calendar in addition to time for lunch, small breaks. This ensures no meetings (or reduced meetings) in this period.</p><p><strong>Scheduled breaks</strong></p><p>Make sure you have scheduled breaks to standup &amp; stretch, drink water, take a walk, etc.. Do take your health seriously and at times it’s really essential to give you a creative break-through on a task which you have been trying to figure out for a while. You can also try <a href="https://en.wikipedia.org/wiki/Pomodoro_Technique">Pomodoro technique</a> to break work into intervals.</p><p><strong>Caveats</strong></p><p>Lastly, you might find that Time Management isn’t working for you and it’s becoming a vicious circle — that is, the more you are trying to manage, the more out of hands time is going. You continue to make it work and fail.<br>Well, continue the efforts and make changes according to your life style. Everyone is different and has to find the right mix for themselves. The above suggestions will help you find your own way of managing time. So, keep trying and keep evolving.</p><p>Hope, you’ll follow some of the tips. Do comment on what worked for you and if you have any more tips 🙂</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=844d4038c862" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting started with Apache Kafka]]></title>
            <link>https://sameerbhatt.medium.com/getting-started-with-apache-kafka-aa9fb61e5a78?source=rss-bebf1f6e74b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa9fb61e5a78</guid>
            <category><![CDATA[getting-started]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[apache-kafka-tutorial]]></category>
            <category><![CDATA[event-driven-systems]]></category>
            <category><![CDATA[apache-kafka]]></category>
            <dc:creator><![CDATA[Sameer Bhatt]]></dc:creator>
            <pubDate>Wed, 29 Sep 2021 17:35:02 GMT</pubDate>
            <atom:updated>2021-09-29T17:35:02.401Z</atom:updated>
            <content:encoded><![CDATA[<p>This is an ultra short hands-on guide to get you started with <a href="https://kafka.apache.org/">Apache Kafka</a>.<br>(Note that there is so much more to Kafka that you can explore &amp; learn)</p><p>We will learn -</p><ol><li>What is Kafka and its use-cases</li><li>Setting up and getting started with Kafka — producing and consuming events</li><li>Setting up a Java App for producing and consuming events</li></ol><h3><strong>What is Kafka</strong></h3><p>Quite simply, Kafka is a distributed event streaming platform. Now, you might be wondering what is event streaming. Consider various events that happen in real-time — stock exchanges, banks, Shipping, IoT devices. Usually, it’s required to store these events, process them, react to them, or retrieve them later. Event streaming ensures that these events are flowing and are processed continuously, and the right information is at the right place, at the right time.</p><p>It’s also used as a foundation for data platforms and event driven architectures and micro-services.<br>Refer <a href="https://kafka.apache.org/">https://kafka.apache.org/</a> for details.</p><p>Many well known companies such as Airbnb, Linkedin, Netflix, Shopify, Zalando, etc.. use it already, some sample use-cases are listed here — <br><a href="https://kafka.apache.org/uses">https://kafka.apache.org/uses</a></p><h3><strong>Basic Entities in Kafka</strong></h3><p>There are multiple entities, but the most noteworthy are -</p><p><strong>Producer</strong> — applications that publish events to Kafka<br><strong>Consumer</strong> — applications that subscribe to and process these events<br><strong>Topic &amp; Event</strong> — An event denotes something happened. Events need to be grouped such that the producer tells which Topic an event should goto and Consumer to make sure to subscribe to the relevant topic to consume events. Topics are usually partitioned or spread over for scalability. Topics are also replicated to make your data fault-tolerant and highly-available.<br><strong>Broker</strong> — There are one of more broker instances running. They are essentially servers that form a storage layer, receiving events from the Producers, storing them and allowing Consumers to retrieve them based on the various parameters such as Topics, Partitions, &amp; Offset.<br><strong>Zookeeper</strong> — Refer <a href="https://zookeeper.apache.org/">here</a></p><h3>Setting it up</h3><p>Download the latest version of Kafka from <a href="https://kafka.apache.org/downloads">https://kafka.apache.org/downloads</a><br>You can download the binary and unzip it.</p><p>Starting up various services (use different terminal windows) -</p><p><strong>Start zookeeper<br></strong>It will start on localhost:2181</p><pre><em>./bin/zookeeper-server-start.sh config/zookeeper.properties</em></pre><p><strong>Start Kafka Broker</strong><br>It will start on localhost:9092</p><pre><em>./bin/kafka-server-start.sh config/server.properties</em></pre><p><em>zookeeper.properties</em> and <em>server.properties</em> are default configurations provided in the Kafka package. For this article, we will continue with the default configs.</p><blockquote><strong>Note</strong>: In case you forcefully terminate Zookeeper, it might give you the following error on launch -<br>java.io.IOException: No snapshot found, but there are log entries. Something is broken!</blockquote><p>To fix this, delete the tmp directory (mac command below) -<br>(For win, delete the equivalent tmp directory)</p><pre>rm -rf /tmp/zookeeper/*</pre><p><strong>Create a Topic</strong></p><pre><em>./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 </em><strong><em>--topic my_topic</em></strong></pre><p><strong>Describe a Topic</strong></p><pre><em>./kafka-topics.sh </em><strong><em>--describe --topic my_topic </em></strong><em>--zookeeper localhost:2181</em></pre><p><strong>See all Topics</strong></p><pre><em>./kafka-topics.sh --list --zookeeper localhost:2181</em></pre><p><strong>Start the Producer</strong></p><pre><em>./kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic</em></pre><p><strong>Start the Consumer</strong></p><pre><em>./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my_topic --from-beginning</em></pre><p>That’s it, you can see how any message produced in the producer terminal comes up in the consumer terminal.</p><p><strong>Somethings to try</strong></p><ol><li>Create a new topic and increment the replication-factor and partitions.</li><li>Start more than 1 consumer instances.</li><li>Produce messages and see which consumers they are going to.</li></ol><h3>Setting up the Java Apps</h3><p>Use your IDE of choice (I used the <a href="https://www.jetbrains.com/idea/download/">IntelliJ Community Edition</a>) and create a new Java project (with maven)<br>In the pom.xml file, add kafka-clients as dependency -</p><pre>&lt;dependencies&gt;<br>    &lt;dependency&gt;<br>        &lt;groupId&gt;org.apache.kafka&lt;/groupId&gt;<br>        &lt;artifactId&gt;kafka-clients&lt;/artifactId&gt;<br>        &lt;version&gt;2.8.0&lt;/version&gt;<br>    &lt;/dependency&gt;<br>&lt;/dependencies&gt;</pre><p>Refresh and wait for build changes to take place.<br>Head to the external packages in the Project view and see kafka-clients listed there. Feel free to explore the various classes here -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*ARBp34xiDkG9E8jdLg9b5A.png" /></figure><h4>Create Producer and Consumer Java Apps</h4><p><strong>ProducerApp</strong><br>Here are the high level steps —</p><ol><li>Set the configuration in a properties file. That is, the broker server address and key/value serializers.</li><li>Instantiate a KafkaProducer Object.</li><li>Create ProducerRecords and use the Producer object to send them.</li><li>Once done, close the Producer Object.</li></ol><p>The code is pretty straightforward here —</p><pre>public class ProducerApp {</pre><pre>    public static void main(String[] args) {<br>        Properties props = new Properties();<br>        props.put(&quot;bootstrap.servers&quot;, &quot;localhost:9092&quot;);<br>        props.put(&quot;key.serializer&quot;, &quot;org.apache.kafka.common.serialization.StringSerializer&quot;);<br>        props.put(&quot;value.serializer&quot;, &quot;org.apache.kafka.common.serialization.StringSerializer&quot;);</pre><pre>        KafkaProducer&lt;String, String&gt; producer = new KafkaProducer&lt;String, String&gt;(props);</pre><pre>        try {<br>            for (int i = 0; i &lt; 100; i++) {<br>                producer.send(new ProducerRecord&lt;String, String&gt;(&quot;my_topic&quot;,&quot;My Message:&quot;, Integer.toString(i)));<br>            }<br>        } catch (Exception e) {<br>            e.printStackTrace();<br>        } finally {<br>            producer.close();<br>        }<br>    }<br>}</pre><p><strong>ConsumerApp<br></strong>Here are the high level steps —</p><ol><li>Set the configuration in a properties file. That is, the broker server address, key/value de-serializer and don’t forget the group id.</li><li>Instantiate a KafkaConsumer Object and subscribe to a list of topics.<br><em>Note that you can subscribe to more than one topics.</em></li><li>Poll the Consumer to get ConsumerRecords.</li><li>Iterate over the records and read the messages.</li><li>Once done reading, <a href="https://kafka.apache.org/10/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html#commitAsync-org.apache.kafka.clients.consumer.OffsetCommitCallback-">commit</a> that we are done reading the specified records (offset).</li><li>Finally, close the Consumer Object.</li></ol><p>The code is pretty straightforward here as well —</p><pre>public class ConsumerApp {</pre><pre>    public static void main(String[] args) {<br>        Properties props = new Properties();<br>        props.put(&quot;bootstrap.servers&quot;, &quot;localhost:9092&quot;);<br>        props.put(&quot;key.deserializer&quot;, &quot;org.apache.kafka.common.serialization.StringDeserializer&quot;);<br>        props.put(&quot;value.deserializer&quot;, &quot;org.apache.kafka.common.serialization.StringDeserializer&quot;);<br>        props.put(&quot;group.id&quot;, &quot;my_group&quot;);</pre><pre>        KafkaConsumer consumer = new KafkaConsumer(props);<br>        List&lt;String&gt; topics = new ArrayList&lt;String&gt;();<br>        topics.add(&quot;my_topic&quot;);<br>        consumer.subscribe(topics);</pre><pre>        try {<br>            while (true) {<br>                ConsumerRecords&lt;String, String&gt; records = consumer.poll(Duration.ofMillis(100));<br>                for (ConsumerRecord&lt;String, String&gt; record : records) {<br>                    System.out.println(<br>                            String.format(&quot;Topic: %s, Partition: %d, Offset: %d, Key: %s, Value: %s&quot;,<br>                                    record.topic(), record.partition(), record.offset(), record.key(), record.value())<br>                    );<br>                }<br>                consumer.commitAsync();<br>            }<br>        } catch (Exception e) {<br>            e.printStackTrace();<br>        } finally {<br>            consumer.close();<br>        }<br>    }<br>}</pre><p>You can keep experimenting with adding more topics, more producers, more consumers and various other parameters.</p><p>Hope, this brief hands-on tutorial helped you get started and running with Kafka. As I mentioned earlier, there is a lot more to explore and deep dive into. Happy learning 🙂</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa9fb61e5a78" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Yes, you can become a morning person!]]></title>
            <link>https://sameerbhatt.medium.com/yes-you-can-become-a-morning-person-3202865c16d7?source=rss-bebf1f6e74b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/3202865c16d7</guid>
            <category><![CDATA[habits]]></category>
            <category><![CDATA[life]]></category>
            <category><![CDATA[mindfulness]]></category>
            <category><![CDATA[motivation]]></category>
            <category><![CDATA[meditation]]></category>
            <dc:creator><![CDATA[Sameer Bhatt]]></dc:creator>
            <pubDate>Wed, 14 Jul 2021 12:27:20 GMT</pubDate>
            <atom:updated>2021-07-14T15:23:13.715Z</atom:updated>
            <content:encoded><![CDATA[<p>I tried to be a morning person earlier and went to the Gym at 6:30am in the morning and I could manage it fairly well. However, since the birth of my child four years back, I have slowly and gradually started to wake up late (~7:45am) and sleep much later, usually around ~1:30am. I tried to change it since the last year, but couldn’t as working from home, with the kid around, there was absolute no time left for me — whether it’s time for reflection or time to spend on something else. Everything was revolving around work, kid and related activities. So, I wanted to improve and as <strong>Dale Carnegie</strong> said —</p><p><strong>“Feeling sorry for yourself, and your present condition, is not only a waste of energy but the worst habit you could possibly have.”</strong></p><p><em>Here is my motivation</em> — My previous routine sucked and I wanted to have some quality time for myself. Waking up late, doing Yoga (that’s absolutely necessary to keep me going considering my slip-disc), and then work. Late evenings were reserved as playtime with my kid. I wasn’t doing any workout as well. By the time, I get some time for myself, usually late night, I was too tired to do anything. So, I use to wrap up some work tasks or binge watch. I knew this won’t work for long…</p><p>Then, in June, I stumbled upon Rajan’s (<a href="https://www.habitstrong.com/">HabitStrong</a>) <a href="https://www.habitstrong.com/morning-person"><strong>Become A Morning Person</strong></a> Bootcamp. It’s a four weeks program, wherein individuals with a common goal come together to beat the habit of waking up late. There are two options 5am and 6am. I didn’t want to be too adventurous and enrolled for the 6am Bootcamp starting from 14th June.</p><figure><img alt="Become a Morning Person Bootcamp" src="https://cdn-images-1.medium.com/max/1024/1*qRQhfTjBpsUqz2HLg5DRwQ.png" /></figure><p>The program is about creating discipline in life and making waking up early a Habit. It starts sharp at 6am with meditation, then journaling, and followed up with a workout of your choice. In this duration you are also encouraged to maintain some Habits — reading a book, etc..</p><p>My experience with the program was really great! Everything is provided for us to become successful — guidance by Rajan, Meditation and support by expert coaches and answers to some frequently asked questions that we would have in mind when starting on such a journey.</p><p>I started to look forward to the morning sessions and woke up at ~5:45am in the morning to be present in the session starting at sharp 6am. The Meditation session is about focus on your breadth, letting the thoughts come and go. Accept them as they inundate your mind without judging and continue to focus. After the Meditation was the most interesting part — journaling. You can continue to write your thoughts or work on a prompt given by the coach. At times, you will feel you can just continue to write the whole day, other times, you might feel short of words. Both are fine, no one is judging 🙂 On weekends, you can continue to practice on your own by using guided meditation by Rajan.</p><p>Now, waking up early is a Big task, and it can’t be done without sleeping well on time. I was pretty sure that the previous routine of sleeping at 1:30am won’t work. So, I had to make a deliberate attempt to switch off the lights early. I did this by trying to goto sleep by 11pm but could only manage to sleep by 12am. The side-effects — groggy feeling in the beginning, tired-ness the entire day. The key here is to find your own routine that works and I’m still getting better at mine. For me, after the morning meditation, workout and Yoga, if I feel groggy, I do <a href="https://en.wikipedia.org/wiki/Shavasana">Shavasana</a> and at times sleep for 15–20 mins to wake-up with renewed energy to go through the entire day. At times, I feel active and continue to read a book before starting work. Other times, afternoons are awful and 10 mins of <a href="https://www.headspace.com/">Headspace</a> works wonder.</p><p><strong>How did I continue on weekends and even continuing now?</strong> Referring to the guided meditation by Rajan helped a lot on the weekends. I also use <a href="https://www.headspace.com/">Headspace</a>, it has a good wake-up routine to start your day. And yes, don’t forget the workout. <em>Even if you feel like skipping it for a day.. Don’t!</em></p><figure><img alt="Headspace Wake-up Routine" src="https://cdn-images-1.medium.com/max/1024/1*ddtQ_ZKi1X0AcLZCTpMGTw.jpeg" /></figure><p>Again, this has been my experience so far and I’m getting better at it. You have to find what works for you.<br>If you are serious on becoming a morning person, I would highly recommend the <a href="https://www.habitstrong.com/morning-person">HabitStrong Bootcamp</a>. At least give it a try for a month. I’m pretty sure, you won’t be dis-appointed 🙂 I’ll end with what <strong>Jim Rohn</strong> said —</p><p><strong>“Motivation is what gets you started. Habit is what keeps you going.”</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3202865c16d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting started with Kubernetes]]></title>
            <link>https://sameerbhatt.medium.com/getting-started-with-kubernetes-d4d33f19f511?source=rss-bebf1f6e74b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/d4d33f19f511</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[introduction]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[apps]]></category>
            <dc:creator><![CDATA[Sameer Bhatt]]></dc:creator>
            <pubDate>Wed, 09 Jun 2021 06:20:03 GMT</pubDate>
            <atom:updated>2021-06-09T06:20:03.636Z</atom:updated>
            <content:encoded><![CDATA[<p>Alright, we saw how to get started with development using docker <a href="https://sameerbhatt.medium.com/getting-started-with-building-apps-using-node-and-docker-d41ac42b8774">here</a>. Now, let’s setup <a href="https://kubernetes.io/">Kubernetes</a> (K8s) and move our existing app to K8s.</p><p><em>Again, the intent here is an introduction of K8s. Hence, I will not be going into the details but just touching the surface and moving our existing App to K8s.</em> So, let’s get started.</p><p>Here is what we will be covering —</p><ol><li>Setting up K8s using Docker Desktop or manually using hyperkit and minikube</li><li>Moving the mysql database to K8s</li><li>Moving the redis instance to K8s</li><li>Moving the App to K8s</li><li>Getting it all together by Kubernetes magic</li></ol><p>Source code in Github: <a href="https://github.com/sameerbhatt/url-shortener-app">https://github.com/sameerbhatt/url-shortener-app</a></p><p><strong>Setting up K8s -</strong></p><p>Install <a href="https://www.docker.com/products/docker-desktop">Docker Desktop</a> to get most of the things automatically. You can Enable Kubernetes from Docker Desktop → Settings → Kubernetes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W1FCaixn5cfQF4oTxG5EoQ.png" /><figcaption>Enabling Kubernetes in Docker Desktop</figcaption></figure><p>If not using Docker Desktop, install them manually — <a href="https://minikube.sigs.k8s.io/docs/drivers/hyperkit/">hyperkit</a> and <a href="https://minikube.sigs.k8s.io/docs/">minikube</a></p><pre>brew update<br>brew install hyperkit<br>brew install minikube</pre><pre>// Creating a minikube cluster<br>minikube start --vm-driver=hyperkit</pre><p>Now, you might not be able to use your local images from Docker Desktop. See details here on how to fix -<a href="https://github.com/kubernetes/minikube/blob/0c616a6b42b28a1aab8397f5a9061f8ebbd9f3d9/README.md#reusing-the-docker-daemon">https://github.com/kubernetes/minikube/blob/0c616a6b42b28a1aab8397f5a9061f8ebbd9f3d9/README.md#reusing-the-docker-daemon</a></p><p>For this example, we will continue to use Docker Desktop. For this, you have to set the correct context now —</p><pre>// Check the context<br>kubectl config current-context</pre><pre>// Set context as docker desktop<br>kubectl config use-context docker-desktop</pre><pre>// check version to see you get both Client and Server Versions<br>kubectl version</pre><p>Let’s move to the next step. We will try to minimize the changes to our existing apps so that there is no change when used directly with docker or with Kubernetes.</p><blockquote><strong><em>Kubenetes Basics<br></em></strong><em>Before going forward, I would highly encourage you to learn about </em><a href="https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/"><em>Kubernetes</em></a><em> and its </em><a href="https://kubernetes.io/docs/concepts/overview/components/"><em>components</em></a><em>.</em></blockquote><h3><strong>Moving Mysql database to K8s</strong></h3><p>Create a directory <strong>kube-config</strong> in the url-shortener-app and create a <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/kube-config/mysql.yaml">mysql.yaml</a> file as follows -</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: mysql-url-shortener<br>spec:<br>  selector:<br>    matchLabels:<br>      app: mysql-url-shortener<br>  template:<br>    metadata:<br>      labels:<br>        app: mysql-url-shortener<br>    spec:<br>      containers:<br>      - image: mysql:8<br>        name: mysql-url-shortener<br>        imagePullPolicy: Never<br>        env:<br>        - name: MYSQL_ROOT_PASSWORD<br>          valueFrom:<br>            secretKeyRef:<br>              name: mysecrets<br>              key: mysql-password<br>        ports:<br>        - containerPort: 3306<br>        volumeMounts:<br>        - name: mysql-volume<br>          mountPath: /var/lib/mysql<br>      volumes:<br>      - name: mysql-volume<br>        hostPath:<br>          # directory location on host<br>          path: /Users/sameer/personal/mysql-data<br>---<br>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: mysql-url-shortener<br>spec:<br>  selector:<br>    app: mysql-url-shortener<br>  ports:<br>    - port: 3306<br>      targetPort: 3306</pre><p>Let’s analyze it now -</p><ol><li>apiVersion, kind, metadata and other common fields, read here— <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/">https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/</a> <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/">https://kubernetes.io/docs/concepts/workloads/controllers/deployment/</a></li><li>Image related — <br><em>image: mysql:8</em> — image name and tag<br><em>imagePullPolicy: Never</em> — this is important since our image is available locally, we don’t want to fetch it from the global docker registry for now. There are various options to use here based on your requirements</li><li>Volume — Similar to how we used a local host directory for storing all mysql data for persistence in Docker, we will need the same path so that our existing data remains. <em>hostPath</em> is used here as I wanted to continue from our previous example. You can use <em>persistentVolumeClaim</em> as well. <a href="https://kubernetes.io/docs/concepts/storage/volumes/">https://kubernetes.io/docs/concepts/storage/volumes/</a></li><li>Service — you now need to expose the mysql database as a network service. <a href="https://kubernetes.io/docs/concepts/services-networking/service/">https://kubernetes.io/docs/concepts/services-networking/service/</a> <br>The specification shown above creates a new Service object named “<em>mysql-url-shortener</em>”, which targets port 3306 on any Pod with the <em>app=mysql-url-shortener</em> label.</li></ol><blockquote><strong><em>Note:</em></strong><em> There is no service type here, which mean it will default to </em>ClusterIP<em>. </em>ClusterIP<em>: Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. For our Node App, we will be using a different type. Stay tuned to read more!</em></blockquote><p><strong>Setting up Secrets</strong> If you are wondering, we missed to discuss about secretKeyRef, don’t worry, we will go over it now. Here is the snippet that I’m talking about in mysql.yaml file-</p><pre>name: MYSQL_ROOT_PASSWORD<br>valueFrom:<br>  secretKeyRef:<br>    name: mysecrets<br>    key: mysql-password</pre><p>Basically, Kubernetes Secrets let you store and manage sensitive information, such as passwords, OAuth tokens, and ssh keys.</p><p>Refer details here — <a href="https://kubernetes.io/docs/concepts/configuration/secret/">https://kubernetes.io/docs/concepts/configuration/secret/</a> <a href="https://kubernetes.io/docs/tasks/configmap-secret/managing-secret-using-config-file/">https://kubernetes.io/docs/tasks/configmap-secret/managing-secret-using-config-file/</a></p><p>So, we will get the base64 value of our mysql password and use it as the value of the <em>mysql-password</em> field in the secrets file (see below). And then refer the secret in our mysql yaml file (as shown above).</p><pre>echo -n &#39;root123&#39; | base64<br>// outputs cm9vdDEyMw==</pre><p>Here is the content of the <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/kube-config/secrets.yaml">secrets.yaml</a> file —</p><pre>apiVersion: v1<br>kind: Secret<br>metadata:<br>  name: mysecrets<br>type: Opaque<br>data:<br>  mysql-password: cm9vdDEyMw==</pre><h3><strong>Moving Redis to K8s</strong></h3><p>This is mostly similar to the mysql setup above. So, in the directory <strong>kube-config</strong> in the url-shortener-app, create a <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/kube-config/redis.yaml">redis.yaml</a> file as follows -</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: redis-url-shortener<br>spec:<br>  selector:<br>    matchLabels:<br>      app: redis-url-shortener<br>  template:<br>    metadata:<br>      labels:<br>        app: redis-url-shortener<br>    spec:<br>      containers:<br>      - name: redis-url-shortener<br>        image: redis:alpine<br>        imagePullPolicy: Never<br>        ports:<br>        - containerPort: 6379<br>---<br>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: redis-url-shortener<br>spec:<br>  selector:<br>    app: redis-url-shortener<br>  ports:<br>    - port: 6379<br>      targetPort: 6379</pre><p>If you notice, most of the things are same as mysql yaml file, except the name, image and ports. So, let’s not spend more time on it.</p><h3><strong>Moving the App to K8s</strong></h3><p>This is also similar to the above two setups, except a few fields that we will cover. So, in the directory <strong>kube-config</strong> in the url-shortener-app, create a <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/kube-config/app.yaml">app.yaml</a> file as follows -</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: url-shortener-deployment<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      app: url-shortener-app<br>  template:<br>    metadata:<br>      labels:<br>        app: url-shortener-app<br>    spec:<br>      containers:<br>      - name: url-shortener-app<br>        image: url-shortener-app:1.0<br>        imagePullPolicy: Never<br>        ports:<br>        - containerPort: 3000<br>---<br>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: url-shortener-app<br>spec:<br>  selector:<br>    app: url-shortener-app<br>  ports:<br>    - port: 3000<br>      targetPort: 3000<br>  type: LoadBalancer</pre><p>Fields to notice here -</p><ol><li><em>replicas: 1</em> — here, you can specify the number of replicas you want for your app depending on the expected traffic. Default is 1.</li><li><em>image: url-shortener-app:1.0</em> — This is our App image that we built using docker.</li><li><em>Service type: LoadBalancer</em> — This is an important field. Since, we want to expose our service externally, we have changed to type <em>LoadBalancer</em>. From the <a href="https://kubernetes.io/docs/concepts/services-networking/service/">documentation</a> -</li></ol><blockquote><strong><em>LoadBalancer:</em></strong><em> Exposes the Service externally using a cloud provider’s load balancer. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.</em></blockquote><h3><strong>Getting everything together</strong></h3><p>It’s time for some Kubernetes magic now. Make sure your <a href="https://github.com/sameerbhatt/url-shortener-app/tree/master/kube-config">kube-config</a> directory have the following files — <em>app.yaml, mysql.yaml, redis.yaml and secrets.yaml</em></p><pre><strong>$ kubectl apply -f kube-config</strong><br>deployment.apps/url-shortener-deployment created<br>service/url-shortener-app created<br>deployment.apps/mysql-url-shortener created<br>service/mysql-url-shortener created<br>deployment.apps/redis-url-shortener created<br>service/redis-url-shortener created<br>secret/mysecrets created</pre><p>Once, this is done, you can see the status as follows -</p><pre>$ <strong>kubectl get all</strong><br>NAME                                            READY   STATUS    RESTARTS   AGE<br>pod/mysql-url-shortener-bcb64d6fd-hcgfd         1/1     Running   0          65s<br>pod/redis-url-shortener-9768b8985-9qzbv         1/1     Running   0          65s<br>pod/url-shortener-deployment-77bd88587d-525b2   1/1     Running   0          65s</pre><pre>NAME                          TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE<br>service/kubernetes            ClusterIP      10.96.0.1        &lt;none&gt;        443/TCP          2d21h<br>service/mysql-url-shortener   ClusterIP      10.107.253.197   &lt;none&gt;        3306/TCP         65s<br>service/redis-url-shortener   ClusterIP      10.109.49.50     &lt;none&gt;        6379/TCP         65s<br>service/url-shortener-app     LoadBalancer   10.105.241.67    localhost     3000:30059/TCP   65s</pre><pre>NAME                                       READY   UP-TO-DATE   AVAILABLE   AGE<br>deployment.apps/mysql-url-shortener        1/1     1            1           65s<br>deployment.apps/redis-url-shortener        1/1     1            1           65s<br>deployment.apps/url-shortener-deployment   1/1     1            1           65s</pre><pre>NAME                                                  DESIRED   CURRENT   READY   AGE<br>replicaset.apps/mysql-url-shortener-bcb64d6fd         1         1         1       65s<br>replicaset.apps/redis-url-shortener-9768b8985         1         1         1       65s<br>replicaset.apps/url-shortener-deployment-77bd88587d   1         1         1       65s</pre><p>The above is mostly self-explanatory. There are pods, services, deployments and replicasets created.<br>Test it out now by opening <a href="http://localhost:3000/">http://localhost:3000/</a> in the browser.</p><p><strong>Important:</strong> At times, you might notice the following status for the App Pod (notice the <em>RESTARTS</em> column) -</p><pre>NAME                                            READY   STATUS    RESTARTS   AGE<br>pod/url-shortener-deployment-77bd88587d-r7xbp   1/1     Running   2          35s</pre><p>What this means is that there was some error and the Pod crashed and then restarted twice before being ready. Give it some time to think over why it’s happening and then read further 🙂<br>The error lies in our code in <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/db.js"><strong>db.js</strong></a><strong><br></strong>We are trying to connect to the database only once and failing on error. What if, the mysql Pod isn’t ready by then. Our code will continue to crash till then. Kubernetes comes to the rescue by starting the Pod again and in this case, after 2 re-tries, it connects as the mysql Pod is up. That said, we should fix this in our code to keep trying for sometime to connect to the mysql database instead of trying once and failing.</p><p>To bring the Cluster down -</p><pre>$ <strong>kubectl delete -f kube-config</strong><br>deployment.apps &quot;url-shortener-deployment&quot; deleted<br>service &quot;url-shortener-app&quot; deleted<br>deployment.apps &quot;mysql-url-shortener&quot; deleted<br>service &quot;mysql-url-shortener&quot; deleted<br>deployment.apps &quot;redis-url-shortener&quot; deleted<br>service &quot;redis-url-shortener&quot; deleted<br>secret &quot;mysecrets&quot; deleted</pre><p>Also, you might have noticed that there is no changes required in our original code-base. Everything just works. We were able to refer to the mysql and redis instances by the same names because we used same names in the mysql.yaml and redis.yaml files.</p><p><strong>Here are some useful Kubernetes commands —</strong></p><pre>// minikube status<br>minikube status</pre><pre>// status<br>kubectl get all/ nodes/ pod/ service/ replicaset</pre><pre>// Edit a deployment, you don&#39;t need to apply/ delete/ create anything. It&#39;s managed automatically by K8s<br>kubectl edit deployment NAME</pre><pre>// show logs for a Pod, add -f to stream the logs<br>kubectl logs POD_NAME</pre><pre>// delete deployment<br>kubectl delete deployment NAME</pre><pre>// open terminal/shell of the Pod. it = iterative terminal<br>kubectl exec -it POD_NAME -- bash</pre><pre>// more details for a pod including IP address<br>kubectl get pod -o wide</pre><pre>// scale up/down a deployment, change value of replicas<br>kubectl scale deployment url-shortener-deployment --replicas=1;</pre><pre>// details of all services<br>kubectl get svc</pre><pre>// details for a service<br>kubectl describe service NAME</pre><pre>// details of the stored secrets<br>kubectl get secret mysecrets -o yaml</pre><p>And finally — <a href="https://kubernetes.io/docs/reference/kubectl/cheatsheet/">https://kubernetes.io/docs/reference/kubectl/cheatsheet/</a></p><p>That’s all for now. I hope, this intro helped you get started with Kubernetes :-)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4d33f19f511" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting started with building apps using node and docker]]></title>
            <link>https://sameerbhatt.medium.com/getting-started-with-building-apps-using-node-and-docker-d41ac42b8774?source=rss-bebf1f6e74b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/d41ac42b8774</guid>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[apps]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Sameer Bhatt]]></dc:creator>
            <pubDate>Sun, 30 May 2021 05:41:00 GMT</pubDate>
            <atom:updated>2021-09-13T08:57:57.366Z</atom:updated>
            <content:encoded><![CDATA[<p>Let me first start with my motivation to create this. I thought of exploring a few technologies and what better to build a simple sample. So, I will talk through an example of building a simple URL Shortener Service.</p><p>Though, I had some knowledge of working with node, express and mysql, I was totally new to things such as redis and docker. So, with limited prior knowledge, I started last weekend and spent mostly about an hour or so every day to get it to the state that it’s in right now.</p><p><em>The intent here is an introduction of these technologies. Hence I will not be going into the details but just touching the surface and creating a simple app.<br></em>So, let’s get started.</p><p>Here is what we will be covering—</p><ol><li>Creating an express app with REST APIs and a minimalist UI.</li><li>Setting up database — MySql (through docker)</li><li>Introducing a cache — Redis (through docker)</li><li>Connecting the app to the database/ redis</li><li>Building a docker image for the app</li><li>Getting it all together using docker-compose</li></ol><p>Source code in Github: <a href="https://github.com/sameerbhatt/url-shortener-app">https://github.com/sameerbhatt/url-shortener-app</a></p><p><strong>Installing Node, Express<br></strong>Installing node and express is pretty easy, just follow these respective links</p><ol><li>Node — download and install from <a href="https://nodejs.org/">https://nodejs.org</a>/</li><li>Express — <a href="https://expressjs.com/en/starter/installing.html">https://expressjs.com/en/starter/installing.html</a></li></ol><pre><em>Either use —<br>npm install express --save</em></pre><pre><em>Or use the express generator -<br>npm install -g express-generator</em></pre><p><strong>Generating an App and setting up UI<br></strong>I will use express generator to generate the app -</p><pre><em>npx express-generator --no-view url-shortener-app</em></pre><pre><em>Note: Not generating any views to simplify things for now. We will anyways have a minimalistic UI</em></pre><p>Test it out now by running the following and opening <a href="http://localhost:3000/">http://localhost:3000/</a> in the browser —</p><pre><em>npm install</em></pre><pre><em>DEBUG=url-shortener-app:* npm start</em></pre><p>You can use <a href="https://code.visualstudio.com/">Visual Studio Code</a> as the IDE, pretty good it is.<br>And very easy to run and debug the app to see what’s going on —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/1*wghzaqbAYeYo-Q7p0OSt7Q.png" /><figcaption>Select “Start Debugging”</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*SODhhoedkz_LC6bC85_KWw.png" /><figcaption>Select “Node.js” environment</figcaption></figure><p><strong>Creating UI</strong></p><p>For UI, we will build a very basic one, using —</p><ol><li>Bootstrap — <a href="https://getbootstrap.com/">https://getbootstrap.com/</a></li><li>axios for calling our APIs — <a href="https://axios-http.com/">https://axios-http.com/</a></li></ol><p>Please refer to <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/public/index.html"><strong><em>index.html</em></strong></a> for the details. I know, we can go into a lot of details/handling here and create awesome looking pages. However, that’s not the intent now 🙂</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SEcxrcCZCRd6rfco5g-kjQ.png" /><figcaption>Sample UI</figcaption></figure><p>For calling the APIs using axios, here is a snippet —</p><pre>axios.post(&#39;http://localhost:3000/api/link&#39;, {<br>url: inputURL // body<br>})<br>.then(function (response) {<br>    alert(response.data.shortenedURL);<br>})<br>.catch(function (error) {<br>    alert(error.data);<br>});</pre><p><strong>Let’s get the database up now</strong></p><p>For database, we will use MySql and instead of setting up/installing on our machine, we will use it through a docker image.<br>Refer — <a href="https://hub.docker.com/_/mysql">https://hub.docker.com/_/mysql</a></p><pre><em>// pull the mysql 8 image<br>docker pull mysql:8</em></pre><pre><em>// run - create a container out of the mysql image and run it<br>docker run -p 3306:3306 - name mysql-url-shortener -v ~/personal/mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root123 -d mysql:8</em></pre><p>There are few very important options above, let’s go over them —</p><blockquote><em>-p bind machine port to container port — your server will be exposed on port 3306</em><br><em>-v host_dir:container_dir — Host volumes to mount directories.<br>The -v ~/personal/mysql-data:/var/lib/mysql part of the command mounts the ~/personal/mysql-data directory from the underlying host system as /var/lib/mysql inside the container, where MySQL by default will write its data files. </em><strong><em>If you don’t use this option, all changes to your mysql databases will be lost, when the container is deleted.</em></strong><em><br>-e MYSQL_ROOT_PASSWORD=root123 — this is setting the root password for mysql.</em></blockquote><p><strong>Creating tables in the database</strong></p><p>With MySql up, let’s connect to it and explore/ create some tables.<br>You can use <a href="https://hub.docker.com/_/phpmyadmin">phpMyAdmin</a> as well and connect to MySql but the below demonstrates much more.</p><pre><em>// list all the running containers<br>docker ps</em></pre><pre><em>// access container&#39;s shell. replace CONTAINER_ID with the MySQL container_id from above<br>docker exec -it CONTAINER_ID bash</em></pre><pre><em>// connect to mysql<br>mysql - user=root - password</em></pre><pre><em>// create sample database and use it and create a Link table create database mydb;<br>use mydb;</em></pre><pre><em>create table if not exists Link(code varchar(8) primary key, url varchar(512) not null, INDEX(url));</em></pre><pre><em>// insert some dummy values to test<br>INSERT INTO Link VALUES(&#39;abcd123&#39;, &#39;</em><a href="https://www.abcd.com&/#39;"><em>https://www.abcd.com&#39;</em></a><em>);</em></pre><p><strong>Connect to MySql from the node app</strong></p><p>Goto the app directory and run the following to install mysql client on node —</p><pre><em>npm install mysql2</em></pre><p><strong>Note:</strong> See why we use <em>mysql2</em> and not <em>mysql</em> (took me quite a while to figure out) — <a href="https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server/56509065#56509065">https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server/56509065#56509065</a></p><p>The connection code is pretty self explanatory here. You can refer to — <a href="https://expressjs.com/en/guide/database-integration.html#mysql">https://expressjs.com/en/guide/database-integration.html#mysql</a><br>Here is a basic way to connect (refer <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/db.js"><strong><em>db.js</em></strong></a> and <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/routes/index.js"><strong><em>index.js</em></strong></a>) —</p><pre><em>var mysql = require(&#39;mysql2&#39;)<br>var pool = mysql.createPool({<br>    host: &#39;127.0.0.1&#39;,<br>    user: &#39;root&#39;,<br>    password: &#39;root123&#39;,<br>    database: &#39;mydb&#39;<br>});<br>pool.getConnection(function(err, connection){<br>    connection.query(SQL_STATEMENT, function (err, rows, fields) {<br>        ......<br>    }<br>});</em></pre><p><strong>Plugin-in the cache<br></strong>For a cache, we will use Redis. Start Redis docker container —</p><pre><em>docker run -p 6379:6379 - name redis-url-shortener -d redis:alpine</em></pre><p>Install Redis client on node -</p><pre><em>npm install - save redis</em></pre><p>Things are pretty straightforward now.</p><ul><li>We just create a redis client (refer <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/routes/index.js"><strong><em>index.js</em></strong></a><em>)</em></li></ul><pre><em>const redis = require(&#39;redis&#39;);<br>const client = redis.createClient({<br>  host: &#39;127.0.0.1&#39;,<br>  port: 6379<br>});</em></pre><ul><li>Use get and set methods for managing the code:url mapping</li></ul><pre><em>client.get(code, function(err, response) {.....});<br>client.set(code, url);</em></pre><p>Refer here for more details — <a href="https://docs.redislabs.com/latest/rs/references/client_references/client_nodejs/">https://docs.redislabs.com/latest/rs/references/client_references/client_nodejs/</a></p><p><strong>Creating APIs</strong></p><p>We would primarily need two APIs —</p><ol><li>Take an input URL and generate/return a short link. Method: POST.<br>Check the URL in the database, if present, return the short link.<br>Else, generate a random 7 character string, insert into database, update cache and return.</li><li>Take a short link and redirect to the full URL. Method: GET<br>Check in cache or database, if present 302 redirect.<br>Else, return an 404 Not Found.<br>Refer — <a href="https://expressjs.com/en/api.html#res.redirect">https://expressjs.com/en/api.html#res.redirect</a></li></ol><p>content-type for input/output: used json for now as it’s pretty much standard and can be extended if we need to add more fields.<br>Refer <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/routes/index.js"><strong><em>index.js</em></strong></a> for details.</p><p><strong>Test out the App</strong></p><p>So, the MySql and Redis container are running now. Test it out now by running the following and opening <a href="http://localhost:3000/">http://localhost:3000/</a> in the browser —</p><pre><em>npm install</em></pre><pre><em>DEBUG=url-shortener-app:* npm start</em></pre><p><strong>Building a Docker image</strong></p><p>Okay, so, now we have the entire system working. However, for a dev to setup on a machine, there are too many steps. Let’s try to create a docker image from your node app. <br>Note: You can also setup Jenkins and configure it to create a docker image automatically on commits to your code. We will try to do it manually here to get a better idea. This image can then be stored in any repository.<br>Create a <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/Dockerfile"><strong><em>Dockerfile</em></strong></a> in your app folder with the following content —</p><pre>FROM node:14-alpine</pre><pre># Create app directory<br>WORKDIR /usr/src/app</pre><pre># Install app dependencies<br># A wildcard is used to ensure both package.json AND package-lock.json are copied<br>COPY package*.json ./</pre><pre>RUN npm install</pre><pre># Bundle app source<br>COPY . .</pre><pre>EXPOSE 3000<br>CMD [&quot;npm&quot;, &quot;start&quot;]</pre><p>The above should be quite self-explanatory and the comments would help. Though, more details can be found here — <a href="https://nodejs.org/en/docs/guides/nodejs-docker-webapp/">https://nodejs.org/en/docs/guides/nodejs-docker-webapp/</a></p><p>You can also use Visual Studio Code to generate it through a wizard — <a href="https://code.visualstudio.com/docs/containers/quickstart-node">https://code.visualstudio.com/docs/containers/quickstart-node</a></p><p>Before building the image, make sure to change the MySql and Redis endpoint in your node app to MySql and Redis container name. That is, change the <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/db.js"><strong><em>db.js</em></strong></a> and <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/routes/index.js"><strong><em>index.js</em></strong></a> files —</p><pre><strong><em>db.js</em></strong><em><br>// from a docker container, use name of mysql container<br>var pool = mysql.createPool({<br>    host: &#39;</em><strong><em>mysql-url-shortener</em></strong><em>&#39;,<br>    user: &#39;root&#39;,<br>    password: &#39;root123&#39;,<br>    database: &#39;mydb&#39;<br>});</em></pre><pre><strong><em>index.js</em></strong><em><br>// from a docker container, use name of redis container<br>const client = redis.createClient({<br>    host: &#39;</em><strong><em>redis-url-shortener</em></strong><em>&#39;,<br>    port: 6379<br>});</em></pre><p>Let’s build the image and run it now —</p><pre><em>// build image<br>docker build . -t url-shortener-app:1.0</em></pre><pre><em>// create a network<br>docker network create my-network</em></pre><pre><em>// run - create a container with the network created above<br>docker run -p 3000:3000 — network my-network -d url-shortener-app:1.0</em></pre><pre><em>// connect redis and mysql to the same network<br>docker network connect my-network REDIS_CONTAINER_ID<br>docker network connect my-network MYSQL_CONTAINER_ID</em></pre><p>Creating a network and making all containers connect to it is very important. Remember, from node app we were connecting to MySql and Redis on localhost. However, with the node app running in a container, localhost points to the local container’s localhost. We have to make sure to have all containers connect to the same network and that ways, refer to MySql and Redis by their respective container names.</p><p><strong>Getting everything together<br></strong>Now that we have our mysql, redis and node app server images ready, to bring them together, we create a <a href="https://github.com/sameerbhatt/url-shortener-app/blob/master/docker-compose.yml"><strong><em>docker-compose.yml</em></strong></a> file and add the run configurations for each of our images as follows — <br><em>(yaml file alert: watch your indents/spaces)</em></p><pre>version: &#39;3.4&#39;</pre><pre>services:<br>  urlshortenerapp:<br>    image: url-shortener-app:1.0<br>    ports:<br>      - 3000:3000<br>  mysql-url-shortener:<br>    image: mysql:8<br>    ports:<br>      - 3306:3306<br>    environment:<br>      - MYSQL_ROOT_PASSWORD=root123<br>    volumes:<br>      - ~/personal/mysql-data:/var/lib/mysql<br>    redis-url-shortener:<br>      image: redis:alpine<br>      ports:<br>        - 6379:6379</pre><p>You will see that the above file has the same parameters/values as we defined when we were running using the <em>docker run</em> command.<br>Also, note that we aren’t creating a network explicitly here because <br>docker-compose would create a common network when bringing up the containers.<br>That’s it, you can bring all containers up or down using the following —</p><pre>// get all containers up<br>docker-compose up</pre><p>Test it out now by opening <a href="http://localhost:3000/">http://localhost:3000/</a> in the browser.</p><pre>// bring down all containers<br>docker-compose down</pre><p><strong>Here are some useful docker commands —</strong></p><pre><em>// show all running containers<br>docker ps </em></pre><pre><em>// show a history of containers - whether running or not<br>docker ps -a</em></pre><pre><em>// stop/start a container<br>docker stop CONTAINER_ID<br>docker start CONTAINER_ID</em></pre><pre><em>// show all local docker images<br>docker images</em></pre><pre><em>// show logs for a container, add -f to stream the logs<br>docker logs CONTAINER_ID</em></pre><pre><em>// open terminal/shell of the container. it = iterative terminal<br>docker exec -it CONTAINER_ID /bin/bash OR /bin/sh</em></pre><pre><em>// show/ create a network<br>docker network ls<br>docker network create name</em></pre><pre><em>// start all containers in the yaml<br>docker-compose -f FILE.yaml up</em></pre><pre><em>// remove/delete a container/ image<br>docker rm CONTAINER_ID<br>docker rmi IMAGE_ID</em></pre><p>Thanks for getting through till here. I hope, it’s useful and you learned something new! :-)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d41ac42b8774" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>