<?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: Nitish Kafle</title>
    <description>The latest articles on DEV Community by Nitish Kafle (@kaflenitish).</description>
    <link>https://dev.to/kaflenitish</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%2F483931%2F57bb71c0-750e-48a9-9b1f-4b45392c6884.jpg</url>
      <title>DEV Community: Nitish Kafle</title>
      <link>https://dev.to/kaflenitish</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kaflenitish"/>
    <language>en</language>
    <item>
      <title>Solving Find Pivot Index from LeetCode + Explanation</title>
      <dc:creator>Nitish Kafle</dc:creator>
      <pubDate>Thu, 31 Mar 2022 20:57:53 +0000</pubDate>
      <link>https://dev.to/kaflenitish/solving-find-pivot-index-from-leetcode-explanation-m6o</link>
      <guid>https://dev.to/kaflenitish/solving-find-pivot-index-from-leetcode-explanation-m6o</guid>
      <description>&lt;p&gt;Question Link: &lt;a href="https://leetcode.com/problems/find-pivot-index/" rel="noopener noreferrer"&gt;https://leetcode.com/problems/find-pivot-index/&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Question:&lt;/p&gt;

&lt;p&gt;Given an array of integers &lt;code&gt;nums&lt;/code&gt;, calculate the pivot index of this array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;p&gt;First lets look at the example and try to understand the logic behind it.&lt;/p&gt;

&lt;p&gt;According to the example, we have an Array of numbers.&lt;/p&gt;

&lt;p&gt;When we visualize it, along with index we get:&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%2F3f0hzi2irjssg177t8dd.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%2F3f0hzi2irjssg177t8dd.png" alt="Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, Looking at the image, if we add 3 elements on the left, and 2 elements on the right, we get &lt;code&gt;11&lt;/code&gt;. So, the element in between has an index which will be &lt;strong&gt;pivot index&lt;/strong&gt;. &lt;br&gt;
In our case, &lt;br&gt;
&lt;code&gt;Array[3] = 6&lt;/code&gt;, therefore, 3 is our pivot index.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logic:
&lt;/h3&gt;

&lt;p&gt;Now, lets walk through the problem on how we can check for the pivot index.&lt;/p&gt;

&lt;p&gt;First, we need to know the sum of all elements in the array and denote it as &lt;code&gt;totalSum&lt;/code&gt;, which in our case is 28.&lt;/p&gt;

&lt;p&gt;Then lets have sum of all elements in the left index and denote it as &lt;code&gt;leftSum&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now lets create a table with these element and also lets track the element on the index tht we have and denote it as &lt;code&gt;Index[e]&lt;/code&gt; and check what it is equal to.&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%2F79omtvh0dob1kyc6o3be.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%2F79omtvh0dob1kyc6o3be.png" alt="Logic behind the problem"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's approach the problem from the right to the left.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;leftSum&lt;/code&gt; currently is 0 because we did not add anything. And the &lt;code&gt;Index[e]&lt;/code&gt; is the element of the array on that index.&lt;/p&gt;

&lt;p&gt;Doing this for each item in the array, we end up in one condition where &lt;code&gt;leftSum&lt;/code&gt; equals to the same number that is in &lt;code&gt;leftSum&lt;/code&gt;. The next index after that condition is the &lt;code&gt;pivot Index&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And our problem is to find that pivot Index.&lt;/p&gt;

&lt;h3&gt;
  
  
  Congrats! You have successfully diagnosed the problem!
&lt;/h3&gt;

&lt;p&gt;Now, let's create the logic from the result we had:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;totalSum&lt;/code&gt; - &lt;code&gt;leftSum&lt;/code&gt; - &lt;code&gt;Index[e]&lt;/code&gt; = &lt;code&gt;leftSum&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let's implement it in terms of code:&lt;br&gt;
&lt;em&gt;I'm using JavaScript&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

  &lt;span class="c1"&gt;// initialize the sums, 0 because we don't know the sums yet.&lt;/span&gt;

   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;totalSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;leftSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

   &lt;span class="c1"&gt;// now let's calculate the total sum&lt;/span&gt;

   &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;totalSum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/* 
now we have the sum, so we want to check for the condition.
but before that, we need to loop through each element in the array.
*/&lt;/span&gt;

   &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;totalSum&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;leftSum&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;leftSum&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;leftSum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;We checked for that condition, and returned the index if it satisfied the condition since we were looking for that &lt;code&gt;pivot index&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In case, the exact condition did not pass, we had to add another element of the array to the leftSum as we did in the exercise above.&lt;/p&gt;

&lt;p&gt;And, if there was no pivot index, it would return -1 as per the question requirement.&lt;/p&gt;

&lt;h3&gt;
  
  
  Congrats! You did it!
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The solution might not be optimal in terms of runtime and memory distribution, but hey, you figured out the concept behind it and implemented it!&lt;/p&gt;
&lt;/blockquote&gt;




&lt;br&gt;&lt;br&gt;
Cover Photo by &lt;a href="https://unsplash.com/@clark_fransa?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Arnold Francisca&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/code?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;br&gt;&lt;br&gt;

&lt;br&gt;&lt;br&gt;
Thanks for reading. If you have any questions, feel free to send them my way on Twitter &lt;a href="https://twitter.com/developernit" rel="noopener noreferrer"&gt;@developernit&lt;/a&gt; 

</description>
      <category>leetcode</category>
      <category>javascript</category>
      <category>programming</category>
      <category>array</category>
    </item>
    <item>
      <title>Dockerizing your Next.js/React Application!</title>
      <dc:creator>Nitish Kafle</dc:creator>
      <pubDate>Mon, 28 Mar 2022 17:01:57 +0000</pubDate>
      <link>https://dev.to/kaflenitish/dockerizing-your-nextjsreact-application-42ob</link>
      <guid>https://dev.to/kaflenitish/dockerizing-your-nextjsreact-application-42ob</guid>
      <description>&lt;h2&gt;
  
  
  What is Docker?
&lt;/h2&gt;

&lt;p&gt;Docker is a software framework for building, running, and managing containers on servers and the cloud. Think of Docker as a CLI but for the cloud.&lt;/p&gt;

&lt;p&gt;For this tutorial, we will be using &lt;code&gt;Next.js&lt;/code&gt; sample application and create a &lt;code&gt;Dockerfile&lt;/code&gt; in order to Dockerize it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements:
&lt;/h2&gt;

&lt;p&gt;In order to complete Dockerizing your &lt;code&gt;Next.js&lt;/code&gt; app, you will need the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Docker&lt;/code&gt; installed on your computer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Node.js&lt;/code&gt; and &lt;code&gt;npm/yarn&lt;/code&gt; installed on your system in order to create Next app.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating a sample &lt;code&gt;Next.js&lt;/code&gt; app
&lt;/h2&gt;

&lt;p&gt;If you already have an application that you want to dockerize then, you can proceed with further steps else let’s create a &lt;code&gt;next.js&lt;/code&gt; app.&lt;/p&gt;

&lt;p&gt;Run the following command on your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn create next-app 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will initialize files and configurations that are required to create and run your &lt;code&gt;next.js&lt;/code&gt; application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a &lt;code&gt;Dockerfile&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;First, let’s open our app in VS Code or any Code Editor of your choice.&lt;/p&gt;

&lt;p&gt;Run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd &amp;lt;your app name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;code . 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(assuming you have vscode configured)&lt;/p&gt;

&lt;p&gt;Here, you will see the directories of your application. That would look something similar like this.&lt;/p&gt;

&lt;p&gt;[ Note: I am using TypeScript that’s why you are seeing &lt;code&gt;tsconfig.json&lt;/code&gt; and files that ends with &lt;code&gt;.ts&lt;/code&gt; ]&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%2Fpt1050neh8694gdzl9on.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%2Fpt1050neh8694gdzl9on.png" alt="file contents"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go ahead and create a new file and name it &lt;code&gt;Dockerfile&lt;/code&gt;. By default, this file is recognized by &lt;code&gt;docker&lt;/code&gt; and it will executes bunch of commands and instructions that we will provide.&lt;/p&gt;

&lt;p&gt;Remember: The commands will be executed in order of how they are written.&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;Dockerfile&lt;/code&gt; write these codes. I will go through each one and explain how it works at the end of the tutorial.&lt;/p&gt;

&lt;p&gt;[Note: I am using &lt;code&gt;yarn&lt;/code&gt; for this tutorial, you can use &lt;code&gt;npm&lt;/code&gt; but you will have to swap those &lt;code&gt;yarn&lt;/code&gt; executable code with &lt;code&gt;npm&lt;/code&gt;]&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;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:lts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;dependencies&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /&amp;lt;your-app-name&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json yarn.lock ./&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--frozen-lockfile&lt;/span&gt;

&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:lts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;builder&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /&amp;lt;your-app-name&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=dependencies /&amp;lt;your-app-name&amp;gt;/node_modules ./node_modules&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn build

&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:lts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;runner&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /&amp;lt;your-app-name&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; NODE_ENV production&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /&amp;lt;your-app-name&amp;gt;/public ./public&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /&amp;lt;your-app-name&amp;gt;/package.json ./package.json&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /&amp;lt;your-app-name&amp;gt;/.next ./.next&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /&amp;lt;your-app-name&amp;gt;/node_modules ./node_modules&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 3000&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["yarn", "start"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Building Docker Image
&lt;/h2&gt;

&lt;p&gt;Execute the following command to build the &lt;code&gt;Docker&lt;/code&gt; image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build . -t &amp;lt;project-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will build the &lt;code&gt;Docker&lt;/code&gt; image with the name &lt;code&gt;&amp;lt;project-name&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Running the &lt;code&gt;Docker&lt;/code&gt; image once the build is over with the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -p 3000:3000 &amp;lt;project-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, open the browser and navigate to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:3000 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to view your project.&lt;/p&gt;

&lt;h2&gt;
  
  
  CONGRATULATIONS! You have successfully dockerized your application!
&lt;/h2&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%2F65c3jebnnj1f9aggp5wt.gif" 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%2F65c3jebnnj1f9aggp5wt.gif" alt="Congrats"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Visiting contents of the &lt;code&gt;Dockerfile&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Now, let’s go through the code contents of the &lt;code&gt;Dockerfile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Remember, the code are executed based on how they are written, &lt;code&gt;top-down&lt;/code&gt; approach.&lt;/p&gt;

&lt;p&gt;Lets go through the code in three different stages in top-down approach:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Installing dependencies&lt;/li&gt;
&lt;li&gt;Building our &lt;code&gt;Next.js&lt;/code&gt; application&lt;/li&gt;
&lt;li&gt;Configuring the runtime environment for our application&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  1. Installing Dependencies
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:lts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;dependencies&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /&amp;lt;your-app-name&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json yarn.lock ./&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--frozen-lockfile&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s talk about what is happening on this code.&lt;/p&gt;

&lt;p&gt;First, we want to define what image we want to build from for which we are using the latest &lt;code&gt;node&lt;/code&gt; version with &lt;code&gt;node:lts&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;You could use any specific version of node. For example: &lt;code&gt;FROM node:16&lt;/code&gt; would build your image with Node version 16. We are using &lt;code&gt;as dependencies&lt;/code&gt; so that we can export this code and reuse it later when building our application in &lt;code&gt;docker&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Second, we want to create an application directory which holds our application code with &lt;code&gt;WORKDIR&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Third, we want to copy our &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;yarn.lock&lt;/code&gt; files which allows us to take advantage of cached Docker layers. A good explanation of Docker Caching is &lt;a href="https://medium.com/swlh/docker-caching-introduction-to-docker-layers-84f20c48060a" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, we want to be able to run our &lt;code&gt;yarn install&lt;/code&gt; to install these dependencies. We are using &lt;code&gt;--frozen-lockfile&lt;/code&gt; because our &lt;code&gt;yarn.lock&lt;/code&gt; or &lt;code&gt;package-lock.json&lt;/code&gt; gets updated when running &lt;code&gt;yarn install&lt;/code&gt; ( or &lt;code&gt;npm install&lt;/code&gt;). We do not want to check for these changes.&lt;/p&gt;

&lt;p&gt;If you are using &lt;code&gt;npm&lt;/code&gt; you can use &lt;code&gt;npm ci&lt;/code&gt; (ci means clean install / use it for production or else just use &lt;code&gt;RUN npm install&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;yarn&lt;/code&gt; its &lt;code&gt;--frozen-lockfile&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Building our &lt;code&gt;Next.js&lt;/code&gt; application
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:lts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;builder&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /&amp;lt;your-app-name&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=dependencies /&amp;lt;your-app-name&amp;gt;/node_modules ./node_modules&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s look at the build. &lt;/p&gt;

&lt;p&gt;Here, we build our application copying dependencies from node_modules.&lt;/p&gt;

&lt;p&gt;If you are using &lt;code&gt;npm&lt;/code&gt; then use &lt;code&gt;RUN npm build&lt;/code&gt;.&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;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:lts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;runner&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /&amp;lt;your-app-name&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; NODE_ENV production&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After building our project, we want to be able to run it. &lt;/p&gt;

&lt;h3&gt;
  
  
  3. Configuring the runtime environment for our application
&lt;/h3&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; --from=builder /&amp;lt;your-app-name&amp;gt;/public ./public&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /&amp;lt;your-app-name&amp;gt;/package.json ./package.json&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /&amp;lt;your-app-name&amp;gt;/.next ./.next&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /&amp;lt;your-app-name&amp;gt;/node_modules ./node_modules&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 3000&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["yarn", "start"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we want to be able to bundle the app source code inside our Docker image that’s why we are using &lt;code&gt;COPY&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, we want to define a command to run our app using &lt;code&gt;CMD&lt;/code&gt; which defines the runtime.&lt;/p&gt;

&lt;p&gt;For our runtime, we are using &lt;code&gt;yarn&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;If you have &lt;code&gt;Docker&lt;/code&gt; Application installed, you can view your container on the dashboard and run it from there which would look something like the images below.&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%2Fsgn0i3xa4v26bodc1lhq.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%2Fsgn0i3xa4v26bodc1lhq.png" alt="Docker Dashboard"&gt;&lt;/a&gt;&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%2Fm6pndsjc57mq4qfu7wqp.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%2Fm6pndsjc57mq4qfu7wqp.png" alt="Docker Dashboard"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;br&gt;&lt;br&gt;
You did it!&lt;br&gt;&lt;br&gt;
Thanks for reading. If you have any questions, feel free to send them my way on Twitter &lt;a href="https://twitter.com/developernit" rel="noopener noreferrer"&gt;@developernit&lt;/a&gt; 

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>docker</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
