<?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 Supun Dunusingha on Medium]]></title>
        <description><![CDATA[Stories by Supun Dunusingha on Medium]]></description>
        <link>https://medium.com/@supunsd?source=rss-1f1e416601db------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wRIyS7ICQLoHAno4bnU8VA.png</url>
            <title>Stories by Supun Dunusingha on Medium</title>
            <link>https://medium.com/@supunsd?source=rss-1f1e416601db------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 00:07:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@supunsd/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[Docker Essentials for Web Developers: How to Containerize Your Projects]]></title>
            <link>https://medium.com/@supunsd/docker-essentials-for-web-developers-how-to-containerize-your-projects-0623b6e9a71f?source=rss-1f1e416601db------2</link>
            <guid isPermaLink="false">https://medium.com/p/0623b6e9a71f</guid>
            <dc:creator><![CDATA[Supun Dunusingha]]></dc:creator>
            <pubDate>Tue, 17 Sep 2024 05:06:50 GMT</pubDate>
            <atom:updated>2024-09-17T05:06:50.746Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*39MXdCaC1Q-dSmlbHfI3Qw.jpeg" /></figure><blockquote><strong>“It works on my machine”</strong></blockquote><h3><strong>Introduction</strong></h3><p>Docker streamlines web development by enabling the execution of applications in lightweight, isolated containers, providing consistency across many settings. Bundling the application’s code, libraries, and dependencies inside Docker containers makes them faster and more efficient than traditional virtual machines. The typical problem of apps functioning properly on a developer’s PC but not working in other situations is thus resolved.</p><p>In order to streamline workflow and facilitate deployment across a range of platforms from basic web apps to complex microservices you will discover how to build up a web development environment using Docker in this article.</p><h3>What is Docker ?</h3><p>Docker is a software platform that facilitates the rapid development, testing, and deployment of programs. Docker software bundles programs into isolated units known as containers, each of which contains the libraries, runtime, code, and system tools required for the program to function. Docker allows you to deploy apps fast into any environment while ensuring that your code will execute.</p><h3>Installing Docker</h3><p>Getting Docker up and running on your system is a straightforward process. Let’s go over how to install Docker on Windows and macOS.</p><p>Docker Desktop is the easiest way to get started with Docker on Windows and MacOS. It includes everything you need to run Docker containers on your machine.</p><p>You can download docker desktop <a href="https://www.docker.com/products/docker-desktop/"><em>here</em></a>.</p><p>Once the download is complete, run the installer and follow the instructions.</p><p>After installation, open Docker Desktop, and you should see Docker running in the system tray.</p><p>Test the installation by running the following command.</p><pre>docker --version</pre><h3>Docker Components</h3><p><strong>Containers — </strong>the key component of Docker. Code, runtime, system tools, libraries, and settings are all included in containers, which are lightweight, standalone, and executable software packages. Consistency across many environments is ensured by containers.</p><p><strong>Images — </strong>The blueprints for containers are called Docker images. A read-only template containing instructions for setting up a Docker container is called an image. Images may be created using Dockerfiles and distributed using Docker Hub.</p><p><strong>Dockerfile — </strong>An instruction text file for creating a Docker image is called a Dockerfile. It provides instructions on how to launch the program, as well as the base image, required dependencies, and environment parameters.</p><p><strong>Volumes — </strong>Docker volumes are used to store data that containers create and utilize. Volumes enable data sharing and persistence between containers for an extended period of time.</p><p><strong>Docker compose — </strong>Multi-container Docker applications may be defined and executed using Docker Compose. With a YAML file defining services, networks, and volumes, it makes the task of maintaining and coordinating several containers that are a component of a single application easier.</p><p><strong>Docker Hub — </strong>Developers are able to locate and exchange Docker images using this cloud-hosted repository. Easily get started with standard application stacks, it contains official images from software providers as well as community-contributed ones.</p><h3><strong>Setting Up a Web Development Environment with Docker</strong></h3><h4>Step 1 — Choose a simple web app</h4><p>For demonstration purposes, let’s build a simple web app using Node.js and Express. This app will respond with a simple message when accessed via a web browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XoR1O7zPS2kChPazwGK4Ag.png" /></figure><h4>Step 2 — Create a Dockerfile</h4><p>Now, let’s containerize this app using Docker. To do this, we need a Dockerfile. This file acts as a set of instructions that tells Docker how to build an image for our app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RuA98j1wTRsgInQDZJJQIw.png" /></figure><ol><li><strong>FROM node:14:</strong> This line specifies the base image for our container. In this case, we’re using an official Node.js image from Docker Hub, version 20.</li><li><strong>WORKDIR /app</strong>: This sets the working directory inside the container where all commands will be run. Here, we set it to /app.</li><li><strong><em>COPY package*</em>.json /app:</strong> This command copies the package.json and package-lock.json files from your local machine to the container’s working directory. These files contain information about the app’s dependencies.</li><li><strong>RUN npm install</strong>: This installs the dependencies listed in the package.json file inside the container.</li><li><strong>COPY . /app</strong>: This copies the remaining application files (like app.js) from your local machine into the working directory inside the container.</li><li><strong>EXPOSE 3000</strong>: This tells Docker that the app will be available on port 3000. It doesn’t actually expose the port to the host machine, but it’s good practice to document which port the container is using.</li><li><strong>CMD [“node”, “app.js”]</strong>: Finally, this tells Docker to run the Node.js application (app.js) when the container starts.</li></ol><h4>Step 3 — Build and Run the Docker Container</h4><p>Once we have the Dockerfile, we can build a Docker image for our app. To do this, navigate to the directory containing the Dockerfile and run the following</p><pre>docker build -t my-node-app .</pre><p>This will build a Docker image named my-node-app. The -t flag allows us to tag the image with a name.</p><p>Next, we’ll run the container:</p><pre>docker run -p 3000:3000 my-node-app</pre><p>The <strong>-p 3000:3000</strong> flag maps port 3000 inside the container to port 3000 on the host machine, making the app accessible at <a href="http://localhost:3000/">http://localhost:3000</a></p><p>And that’s it! You’ve now containerized a simple Node.js app and set up a basic web development environment using Docker. Your app should be live on <a href="http://localhost:3000/">http://localhost:3000</a>, and you’ll see the message &quot;Hello from Docker!&quot; when you visit it in your browser.</p><h3><strong>Using Docker Compose for Multi-Container Applications</strong></h3><p>When working with more complex applications, you’ll often need to run multiple services together. For example, a Node.js app might need a MongoDB database running alongside it. Docker Compose is a powerful tool designed to simplify this by allowing you to define and manage multi-container Docker applications.</p><h4>What is Docker Compose?</h4><p>Docker Compose uses a YAML file (docker-compose.yml) to define services, networks, and volumes for your application. Instead of manually running multiple containers, you can use Docker Compose to manage them with a single command, making it ideal for multi-container environments.</p><h4>Example: Node.js App with MongoDB</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*buSzQTFsy67UT0FCodLI2w.png" /></figure><ol><li><strong>version: ‘3’</strong>: This defines the version of the Compose file format. Version 3 is the most widely used and works well with newer versions of Docker.</li><li><strong>services</strong>: This is where we define the containers (services) that our app will use. In this case, we have two services: app(the Node.js app) and db(MongoDB).</li><li><strong>app</strong>:</li></ol><ul><li><strong>build: .</strong>: This tells Docker Compose to build the app’s Docker image using the Dockerfile located in the current directory (denoted by the .).</li><li><strong>ports: “3000:3000”</strong>: This maps port 3000 on the host machine to port 3000 inside the app container, making the app accessible at <a href="http://localhost:3000/">http://localhost:3000</a></li><li><strong>volumes: .:/app</strong>: This maps the current directory on your local machine to /app inside the container, allowing real-time changes to be reflected without rebuilding the image.</li><li><strong>depends_on</strong>: This ensures that the db service (MongoDB) starts before the app service.</li></ul><p>4. <strong>db:</strong></p><ul><li><strong>image: mongo</strong>: This specifies that we want to use the official MongoDB image from Docker Hub.</li><li><strong>ports: “27017:27017”</strong>: This maps port 27017 (the default MongoDB port) from the container to the host machine, allowing access to MongoDB.</li></ul><p>With the docker-compose.yml file in place, you can start the entire application stack (both the Node.js app and MongoDB) with a single command:</p><pre>docker-compose up</pre><p>This command runs both containers and links them together, making it easy to manage multi-container applications. Your app will be available at <a href="http://localhost:3000/">http://localhost:3000</a>, and MongoDB will be running on <a href="http://localhost:27017/">http://localhost:27017</a></p><p>If you want to stop the services, you can simply run:</p><pre>docker-compose down</pre><h3><strong>Conclusion</strong></h3><p>By incorporating Docker into your development workflow, you can ensure that your applications run consistently across different environments, streamlining both development and deployment. From simple web apps to complex microservices architectures, Docker helps simplify the process of managing dependencies, setting up environments, and running services. Whether you’re containerizing a single application or orchestrating multiple services with Docker Compose, adopting this tool will save you time and make your projects more scalable and robust. Start small, experiment, and soon you’ll see the immense value Docker brings to web development! For more detailed information, check out the <a href="https://docs.docker.com/">official Docker documentation</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0623b6e9a71f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering State Management in React: Context API vs. Redux]]></title>
            <link>https://medium.com/@supunsd/mastering-state-management-in-react-context-api-vs-redux-8d32270ea529?source=rss-1f1e416601db------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d32270ea529</guid>
            <dc:creator><![CDATA[Supun Dunusingha]]></dc:creator>
            <pubDate>Wed, 17 Jul 2024 18:43:09 GMT</pubDate>
            <atom:updated>2024-07-17T18:49:12.404Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*8N0B6DIDBRFGei98MjDI9g.jpeg" /></figure><h3><strong>Introduction</strong></h3><p>Creating React apps involves a lot of state management. It might become challenging to maintain state as your app grows. Two popular tools to assist with this are Redux and the Context API. This blog will explain these tools, describe their functions, and provide guidance on when to use them.</p><p>First, we’ll go over the fundamentals of the React Context API and Redux. After that, we’ll compare them to determine what makes each different. We’ll conclude off by offering some advice on selecting the best tool for your task. You’ll know more about how these state management solutions can simplify your React programming by the end of this blog.</p><h3><strong>Context API</strong></h3><p>The React Context API is a tool built into React that lets you share state across different parts of your app without having to pass props down manually at every level.</p><blockquote><strong>When and Why to Use It</strong></blockquote><p>You should use the Context API when you have data or functions that need to be accessible by many components in your app, especially when these components are nested deeply. It helps avoid the problem of “prop drilling,” where you must pass data through many levels of components.</p><p>Here is an example to manage user state across the app using the React Context API.</p><ol><li><strong>Create a Context</strong></li></ol><p>The first step is to create a Context using createContext hook provided by the React library.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0OC_YMniF1YmZODD5XLkVA.png" /></figure><p><strong>2. Initial state</strong></p><p>Next step is to create an object to store the initial state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*x6KwFh6OzAu8mlegcqTWLw.png" /></figure><p><strong>3. Create a provider component</strong></p><p>Next, create a provider component that will hold the user state and provide it to other components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1004/1*H5_S73ZwxS5adkKvBcMZVw.png" /></figure><p><strong>4. Create a custom hook</strong></p><p>To make working with the user state even easier, you can create a custom hook. This can also be done inside the component where the context is used. But this simplifies the process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UAD8yfLdRiRNoHgAY4KWHQ.png" /></figure><p><strong>5. Wrap Your App with the Provider</strong></p><p>wrap your main app component with the UserProvider to make the user state available to all components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*yvvu0oVUEExso07SDnryLQ.png" /></figure><p><strong>6. Use the Context in a Component</strong></p><p>Now, let’s create a component that consumes the user state and allows updating it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o_4PPRbaCbkBC424CoJG-A.png" /></figure><p><strong>UserContext:</strong> This is the context we created to manage the user state.</p><p><strong>UserProvider:</strong> This component holds the user state and provides it to any component wrapped inside it.</p><p><strong>UserProfile:</strong> This component consumes the user state and allows updating it through an input form.</p><p><strong>Main:</strong> Your main application is wrapped with UserProvider to make the user state accessible throughout the app.</p><h3>Redux</h3><p>Redux is a state management tool for JavaScript applications, commonly used with React. It helps you manage the state of your app in a more structured way. Redux Toolkit simplifies the setup and usage of Redux, providing a set of tools to work with Redux more efficiently.</p><blockquote><strong>When and Why to Use It</strong></blockquote><p>When you have a huge application that requires a lot of state to be shared across multiple components, Redux is the way to go. Redux simplifies the management of complex state logic and the tracking of state changes. Applications where the state must be consistent across several app sections and is updated often will find it very helpful.</p><p>Here’s a basic example of how to use Redux Toolkit. To effectively use Redux Toolkit, it’s beneficial to understand the core concepts of pure Redux. However, in this blog, I’ll focus on demonstrating how to manage state using Redux Toolkit to simplify the process and enhance productivity.</p><ol><li><strong>Install redux to your React app</strong></li></ol><pre>npm install @reduxjs/toolkit react-redux</pre><p><strong>2. Create a Redux Slice</strong></p><p>A slice contains the reducer logic and actions for a specific part of your state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CfE_WUbuE8boRL6-gKJmEQ.png" /></figure><p>3. <strong>Configure the Store</strong></p><p>Create a store and add the user reducer to it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*LxO6a0eEs8ocXDM72YbEFQ.png" /></figure><p><strong>4. Provide the Store to Your App</strong></p><p>Use the Provider component to make the Redux store available to your React components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zoCgNxDgd1fGuEuKFw7FLQ.png" /></figure><p><strong>5. Connect Components to the Redux Store:</strong></p><p>Use the <strong>useSelector</strong> and <strong>useDispatch</strong> hooks to connect your components to the Redux store.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lYNL1I0w1vn5nCS569vsnA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*4gRIfj_KVf_dVcdbfiCN_g.png" /></figure><h3>Comparison of React Context API and Redux</h3><blockquote>Key differences</blockquote><p><strong>Usage</strong></p><p>Context — Simple and straightforward. You create a context, a provider, and use the context in your components.</p><p>Redux — Requires more setup, including creating a store, actions, and reducers. Using Redux Toolkit simplifies this process but still involves more steps than Context API.</p><p><strong>State management</strong></p><p>Context —Suitable for less complex state management needs. Works well for state that doesn’t change often or isn’t used in many places.</p><p>Redux — Ideal for complex state management. It offers a structured way to manage state changes with actions and reducers.</p><p><strong>Async operations</strong></p><p>Context —No mechanism for async operations</p><p>Redux — supports middleware for async operations (Redux thunks)</p><p><strong>Performance</strong></p><p>Context — Can lead to performance issues if not used carefully. When the state updates, all components that consume the context re-render.</p><p>Redux — Manages performance better in large applications by using a single store and minimizing re-renders with selectors.</p><p><strong>Debugging</strong></p><p>Context — No built-in debugging tools.</p><p>Redux — Comes with powerful developer tools that help track state changes, actions.</p><blockquote>When to use Context API or Redux</blockquote><p><strong>Context API</strong></p><ol><li>Use the Context API for global state management in small apps.</li><li>When you just need to share a value that doesn’t change often. [Color theme, preferred language, authenticated user, …]</li><li>When you need to solve a simple prop drilling problem.</li><li>If you prefer to use a built-in React solution without adding extra dependencies, the Context API is a good choice.</li></ol><p><strong>Redux</strong></p><ol><li>Use Redux for global state management in large apps.</li><li>When you have lots of global UI state that needs to be updated frequently (Redux is optimized for frequently changed state) [Shopping cart, current tabs, complex filters or search, …]</li><li>When you have complex state with nested objects and arrays (because you can mutate state with Redux Toolkit)</li></ol><h3>Conclusion</h3><p>Building reliable and scalable React apps requires selecting the appropriate state management solution. Redux and React Context API provide powerful state management solutions, but they serve different purposes.</p><p><strong>React Context API</strong> is perfect for smaller applications or simple state sharing between components. Since it’s integrated into React, using it is simple and doesn’t require any other dependencies.</p><p><strong>Redux</strong>, especially with Redux Toolkit, used in larger applications with complex state management requirements. It provides a structured, predictable way to handle state changes and offers advanced debugging tools.</p><p>The optimal option ultimately relies on the particular requirements and complexity of your application. Test out both approaches to determine which works best for your project. If required, don’t be afraid to combine them, making sure you use the appropriate tool for the job.</p><p>With this blog, I hope to clarify the differences between Redux and React Context API so you may choose wisely for your next React project. Have fun with coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d32270ea529" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>