<?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: Zhalok Rahman</title>
    <description>The latest articles on DEV Community by Zhalok Rahman (@zhalok).</description>
    <link>https://dev.to/zhalok</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%2F872813%2F4b04f483-9836-43d4-b8b0-6b75c5e89cd5.jpeg</url>
      <title>DEV Community: Zhalok Rahman</title>
      <link>https://dev.to/zhalok</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zhalok"/>
    <language>en</language>
    <item>
      <title>Essential docker commands</title>
      <dc:creator>Zhalok Rahman</dc:creator>
      <pubDate>Fri, 05 May 2023 04:25:47 +0000</pubDate>
      <link>https://dev.to/zhalok/important-docker-commands-1bip</link>
      <guid>https://dev.to/zhalok/important-docker-commands-1bip</guid>
      <description>&lt;h2&gt;
  
  
  1. Showing all containers
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;docker ps -a&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Showing running containers
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;docker ps&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Showing all images
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;docker images&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Showing all conatiner ids
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;docker ps -a -q&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Showing all image ids
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;docker images -q&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Stopping all containers
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;docker stop $(docker ps -q)&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Deleting all containers
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;docker rm $(docker ps -a -q)&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Building image from docker file
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;docker build &amp;lt;directory in which the dockerfile exists&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Building image from docker file with a tag name
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;docker build -t &amp;lt;tag name&amp;gt; &amp;lt;directory in which the dockerfile exists&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Running an image in a container
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;docker run &amp;lt;image_id&amp;gt; &lt;br&gt;
&lt;/code&gt;or &lt;br&gt;
&lt;code&gt;docker run &amp;lt;tag name (if provided while building)&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  11. Running an image in a container with interactive shell terminal
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;docker run -i -t &amp;lt;image_id or tag_name&amp;gt; bash&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>containers</category>
      <category>devops</category>
    </item>
    <item>
      <title>Things I wish I knew as a hackathon participant</title>
      <dc:creator>Zhalok Rahman</dc:creator>
      <pubDate>Thu, 04 May 2023 04:56:47 +0000</pubDate>
      <link>https://dev.to/zhalok/things-i-wish-i-knew-as-a-hackathon-participant-4f6e</link>
      <guid>https://dev.to/zhalok/things-i-wish-i-knew-as-a-hackathon-participant-4f6e</guid>
      <description>&lt;p&gt;Hackathon is one of the most popular type of competition in the field of programming. It's basically a project development and showcasing competition where you get to implement a project idea on the spot and solve some particular real life problems, also add new features based on judge recommendations. &lt;/p&gt;

&lt;p&gt;Typically in a hackathon the team consists of 3-5 members. But of course if it is based upon the scale of the project that is required. One of the key points on winning a hackathon is the proper co operation and proper co-ordination as the workload is being distributed among the team members.&lt;/p&gt;

&lt;p&gt;Based on my experiences of participating on hackathons I would like to share some of the tricks that I wish I knew before.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Don't focus on scalability of your architecture
&lt;/h2&gt;

&lt;p&gt;You are not here to build a production level application that will have 1 million users. You just need to convince the judges that you do have the proper knowledge and potential to turn your idea into reality. &lt;/p&gt;

&lt;h2&gt;
  
  
  2. Use pre-built components from UI libraries as much as possible
&lt;/h2&gt;

&lt;p&gt;You don't need to build a cutting edge User Interface by applying a lot of styling. You just need to present that the features are working properly. If you put a lot of time in designing a cutting edge UI but don't get the time to make them functional, the judges will not be happy. &lt;/p&gt;

&lt;p&gt;(&lt;em&gt;However, there are some project showcasing competition where people get a prize by just creating a figma design. But I don't consider them as hackathons, they are startup business idea competitions for me. Well actually they are business idea competitions but sometimes they name it as hackathon.&lt;/em&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Use cloud services as back-end and database instead of developing own server-side application.
&lt;/h2&gt;

&lt;p&gt;Don't spend time on developing APIs for basic operations such as authentication and CRUD operations. Instead use cloud services like &lt;strong&gt;Firebase&lt;/strong&gt;, &lt;/p&gt;

&lt;h2&gt;
  
  
  4. Integrate modern techs such as Speech Processing, Computer Vision, Maps, etc. with cloud services  which are not free for everyone.
&lt;/h2&gt;

&lt;p&gt;Cloud services provide a lot of useful services that are easy to integrate and function. But the problem is they are not free. They require card information even if you want o create a free trial account. &lt;br&gt;
Well here's the hack, you can use your student email account to apply for github student developer pack. Once you have that you have a superpower as student dev. You can have 6 month free access to Microsoft Azure. Azure comes up with almost all the services you will possibly need to make your project an unique one from others'.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Use docker if you really need to migrate applications to a single machine.
&lt;/h2&gt;

&lt;p&gt;You may have developed front-end application and back-end application or maybe also a mobile application on different machines. But while presenting you need to demonstrate all the applications in a single machine. Therefore it's really a good and safe choice to dockerize the applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Create project in GitHub and keep track of the tasks.
&lt;/h2&gt;

&lt;p&gt;As you will be working on team, It's really important to keep track of the features that are implemented and that are in progress and assign task of implementing features to team members, it can be very useful to have a GitHub project. Or you can use other project management tools like Jira and Trello.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Learn how to work on branches and resolve merge conflicts in GitHub and VScode.
&lt;/h2&gt;

&lt;p&gt;If you are working in parallel, you definitely need to work on different branches. But in order to sync you also need to merge your codes into the default branch _(which is &lt;strong&gt;main&lt;/strong&gt;&lt;br&gt;
branch in most of the cases) _. Therefore you will have conflicts. Learn how to pull and resolve conflicts in local machine with VSCode or any IDE that you are using. They provide a GUI tool for you to work around gits or any other version control systems. &lt;/p&gt;

&lt;h2&gt;
  
  
  8. While testing use the IP addresses of your machine in local network for communication between apps running on different machines. No need to merge them on single machine.
&lt;/h2&gt;

&lt;p&gt;If you need to connect your front-end application with your back-end application which are running on separate devices you can use their IP addresses on the local network and port numbers. But make sure all the devices are connected to the same local network (e.g same WiFi). You can find the local IPs of the machine using some commands on your terminal which vary from machine to machine, therefore you need to do some googling based on your machine environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Finally the most important one: Present recorded workflow.
&lt;/h2&gt;

&lt;p&gt;Make small recording of your work flow of your project using &lt;br&gt;
&lt;a href="https://www.loom.com/"&gt;loom&lt;/a&gt; and present that to the judges instead of demonstrating the project on your machine. Because if you do that there may occur some errors due to unknown reasons which is not possible to fix at that time.&lt;/p&gt;

&lt;p&gt;That's all from me. If you think I missed something, share your experience with me ...&lt;/p&gt;

</description>
      <category>hackathon</category>
      <category>development</category>
      <category>agile</category>
    </item>
    <item>
      <title>Optimizing complex relational query in MongoDB using aggregation pipeline</title>
      <dc:creator>Zhalok Rahman</dc:creator>
      <pubDate>Fri, 28 Apr 2023 10:39:25 +0000</pubDate>
      <link>https://dev.to/zhalok/mongodb-aggregation-undercover-sql-agent-56f1</link>
      <guid>https://dev.to/zhalok/mongodb-aggregation-undercover-sql-agent-56f1</guid>
      <description>&lt;p&gt;we all know that MongoDB is a nosql database and is mostly used for non relational database. &lt;br&gt;
But you can also design a database with strong relationship between tables (or models for mongo) using MongoDB as well. But what will be the problem then? The problem will be if we use the basic APIs of MongoDB like FindOne(), FindById() etc, then we may end up writing a lot of database calls on a single API endpoint.&lt;/p&gt;

&lt;p&gt;For example, let's assume that we have a simple classroom management system where there will be a classroom and under one classroom there can be multiple teachers and students.&lt;br&gt;
So, now if we want to design a database schema for this design what we need to do? Let's first understand the relation between classroom teachers and students. Hence the relation between classroom-teacher is many to many and so as the relation between classroom-student.&lt;/p&gt;

&lt;p&gt;Now let's design a classroomSchema using mongoose. How we will do that?&lt;br&gt;
A naive approach can be like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2QrS4zaj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3nhbctdvdroiko2v5b82.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2QrS4zaj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3nhbctdvdroiko2v5b82.png" alt="Image description" width="800" height="714"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;but whats the problem? this database is not normalized and may lead to performance issue while scaling up.&lt;br&gt;
So what can we do?&lt;/p&gt;

&lt;p&gt;We can come up with a bit efficient solution by storing the object id of the teachers and students documents instead of keeping the whole documents.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LkI-JmkJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfabudhy4fm38wymu7p9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LkI-JmkJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfabudhy4fm38wymu7p9.png" alt="Image description" width="800" height="629"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Great, but now if we need to build a rest api endpoint which will return all the classrooms along with all the teachers and students associated with them &lt;/p&gt;

&lt;p&gt;we can write the following code for that:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qGmTP6Hc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/911hcrq5ssckxykjdlwx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qGmTP6Hc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/911hcrq5ssckxykjdlwx.png" alt="Image description" width="800" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;but what's the problem with this solution? this solution will lead to a larger response time because there are 3 database calls here and the complexity of the logic being applied is O(n^2) (quadratic) which both are going to make latency in the response. To get out of this problem, we need to make only one database call and get all the necessary information that we need directly from the database query instead of writing any additional logic.  &lt;/p&gt;

&lt;p&gt;But how can we do that? we are not storing the full document in our array of the classroom document, then how can we do combine classroom, teacher and student document and then make query? &lt;/p&gt;

&lt;p&gt;We can use aggregation pipeline. Aggregation pipeline allows you to make all kinds of query that you can perform in SQL with just some different syntax. I don't know why the mongoDB tutorials I found online did not include the concept of aggregation pipeline yet it is necessary to understand.&lt;/p&gt;

&lt;p&gt;MongoDB aggregation pipeline allows you to write multiple queries on a single database call. &lt;/p&gt;

&lt;p&gt;Right now our goal is to get all the classrooms along with their enrolled students and teachers. Which means we need to combine classroom, teacher and student schemas together and then run the find() query.&lt;/p&gt;

&lt;p&gt;we can write the following code for that&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0y1v_UMg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbzvi7r7j759foofshs0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0y1v_UMg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbzvi7r7j759foofshs0.png" alt="Image description" width="800" height="713"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we can see that the model.aggregate() function receives an array of objects. In each object we can write individual query. We used first $lookup query to join classroom with teachers schema and second $lookup query to join classroom with students schema. &lt;/p&gt;

&lt;p&gt;Let's see these in action and observe the difference.&lt;/p&gt;

&lt;p&gt;Let's first create three endpoints &lt;br&gt;
one for creating teachers&lt;br&gt;
one for creating students&lt;br&gt;
one for creating classrooms with 3 teachers and 3 students&lt;/p&gt;

&lt;p&gt;Each of these endpoints will create 1000 teacher and students and 100 classroom entries in the database&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--is4QkctP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c98vfp1qgxp4ck0dk46a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--is4QkctP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c98vfp1qgxp4ck0dk46a.png" alt="Image description" width="427" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now we will build two endpoints to fetch the classrooms. &lt;/p&gt;

&lt;p&gt;GET /classrooms/unoptimized&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WtnU1n82--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9yxa7w65aosie3ovfhyc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WtnU1n82--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9yxa7w65aosie3ovfhyc.png" alt="Image description" width="800" height="622"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GET /classrooms/optimized&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9jiINVZp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8b3j38xzebp82vor754.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9jiINVZp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8b3j38xzebp82vor754.png" alt="Image description" width="800" height="763"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now lets make the unoptimized query&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FkdeVwcv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhs0caxxcu4kpdws3itx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FkdeVwcv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhs0caxxcu4kpdws3itx.png" alt="Image description" width="427" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;see the response time. its around 850 ms&lt;/p&gt;

&lt;p&gt;Now lets see the optimized endpoint response time&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--665ilAZ7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ycob3h0av35x8ifnsev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--665ilAZ7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ycob3h0av35x8ifnsev.png" alt="Image description" width="420" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just see the magic&lt;br&gt;
we have managed to reduce the response time from 850 ms to just 15 ms. &lt;br&gt;
HOW COOL IS THAT&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/zhalok/test-project"&gt;Full Code --&amp;gt; &lt;/a&gt; &lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>node</category>
      <category>database</category>
      <category>api</category>
    </item>
    <item>
      <title>Hackathon Winning Experience…</title>
      <dc:creator>Zhalok Rahman</dc:creator>
      <pubDate>Fri, 26 Aug 2022 05:01:00 +0000</pubDate>
      <link>https://dev.to/zhalok/hackathon-winning-experience-phd</link>
      <guid>https://dev.to/zhalok/hackathon-winning-experience-phd</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eCpncilu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ceps4otk5azzp5ho1i4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eCpncilu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ceps4otk5azzp5ho1i4.jpg" alt="Image description" width="717" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recently, our team has won a hackathon competition HackNSU4 organized by the ACM student chapter, North South University, Bangladesh. It was really an amazing achievement for the whole team as we had to manage a lot of academic activities and also had to travel 6 hours to participate in this competition.&lt;br&gt;
However now I would like to share our experience in the competition and some tips for anyone who is eager to participate in any hackathon in the future.&lt;br&gt;
In this hackathon, at first, we were asked to submit an idea on some specific scopes. There were mainly 4 scopes on which we could submit our idea: transport efficiency, Reducing energy crisis, and reducing cyber harassment.&lt;br&gt;
we submitted our idea in the first scope where we had the idea of an advanced ticketing system. our idea was basically to reduce the number of unused seats on the Bangladesh railway and prevent tickets from being sold on the black market.&lt;br&gt;
However, our idea was selected and we were invited to participate in the final round. In the final round, there was no pre-code allowed. We had to start building our application from scratch. This was an 8-hour hackathon and within this time we had to complete the prototype and also the presentation and the speech.&lt;br&gt;
The most focused part of the competition was the presentation because it is definitely not possible to implement all the features in the prototype and show a demonstration. However, we still worked really hard and created a prototype for web and also mobile applications.&lt;br&gt;
With the grace of Almighty God, we have managed to become the champion with all our struggles but also I think there were some parts where we could have worked a bit more strategically.&lt;/p&gt;

&lt;p&gt;Prototype: While making the prototype only focus on the features that are the highlighters of our idea instead of implementing general stuff like login, and signup page. Because this is a prototype, you need to only show that you know how to make this happen and you can code to make this happen.&lt;/p&gt;

&lt;p&gt;Dummy Data: You don't need any database. So don't waste your time configuring your database. As we were working with transactions we were using a MySQL database and I had to spend a lot of time just connecting my application with my database. But you really don't need this because this is a prototype.&lt;/p&gt;

&lt;p&gt;Front-end: Again just make a dummy front-end. It doesn't need to function properly and fetch any data. Just set some internal logic and data with hard coding and show that you can do this with the application, that's it&lt;/p&gt;

</description>
      <category>hackathon</category>
      <category>dev</category>
      <category>coding</category>
    </item>
    <item>
      <title>My Docker Experience as a Beginner.</title>
      <dc:creator>Zhalok Rahman</dc:creator>
      <pubDate>Thu, 14 Jul 2022 04:46:29 +0000</pubDate>
      <link>https://dev.to/zhalok/my-docker-experience-as-a-beginner-3dj9</link>
      <guid>https://dev.to/zhalok/my-docker-experience-as-a-beginner-3dj9</guid>
      <description>&lt;p&gt;Recently I have started to get my hands dirty with docker. Here I would like to share some of my silly mistakes as a beginner and how I solved them.&lt;br&gt;
Basically, I was trying to dockerize my MySql database instance as me and my partner are working on completely different systems.&lt;br&gt;
Starting from installing docker in my system to creating the run the MySQL in a container was fine. But the problem arose when I wanted to connect my node js application with my dockerized database.&lt;br&gt;
Now before I explain the problem that I was facing I would like to share a bit of knowledge on how to connect a node js application with a MySQL database in short. For connecting node js application to any MySQL database server, we typically use a MySQL client using an npm package called "MySQL". &lt;a href="https://www.npmjs.com/package/mysql"&gt;https://www.npmjs.com/package/mysql&lt;/a&gt;. Now for establishing the connection between my node js app and MySQL server, there is an API provided by the "MySQL" npm package called "createConnection()". In the function, there are some options that need to be passed. A typical createConnection function looks something like this:&lt;/p&gt;

&lt;p&gt;`const mysql = require("mysql");&lt;br&gt;
const connection = mysql.createConnection({&lt;br&gt;
    host:"localhost",&lt;br&gt;
    port:"3306",&lt;br&gt;
    user:"root",&lt;br&gt;
    password:"03041959",&lt;br&gt;
    insecureAuth:true,&lt;br&gt;
    database:"BookLook",&lt;/p&gt;

&lt;p&gt;});`&lt;/p&gt;

&lt;p&gt;The problem was that I did not understand what the port number and the host needed to be used to connect with my dockerized database. Before jumping into the actual solution that worked, I would first like to go through the failed solutions that I initially tried and why they failed.&lt;br&gt;
Solution1: I first ran the MySQL server in the docker container, then found the hostname using the command "select @@hostnames" and I got the docker container ID as the result, so I thought probably a MySQL server running in a docker container should have the hostname as the docker container ID. Then I changed the host value from "localhost" to "". But it did not work out.&lt;br&gt;
The reason that this solution failed is that even if the MySQL instance is running inside a docker container, they are still on the same machine. Therefore, the dockerized MySQL instance is also running on localhost. So the host was basically not the problem. Then what was it? Why I was not able to connect and make queries to my dockerized MySQL instance using the NodeJS app. Well, the problem was with the port.&lt;br&gt;
Here I would like to note something that may help in future similar problems. If you are experiencing communication issues between two processes running on the same server, you should be 90% certain that the issue is related to the port. Ports allow processes to communicate with one another. However, there are other ways for inter-process communication, such as "shared memory". However, that's an OS topic and I am not diving into it.&lt;br&gt;
Now that I understood that the problem was with the port, I did some digging with the Docker communication via the port. Then I figured out that Docker has a concept called "port mapping". To learn about port mapping, you can read this article:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.docker.com/config/containers/container-networking/"&gt;https://docs.docker.com/config/containers/container-networking/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In short, the docker mapping creates two ports. One for the Docker host and another one in the Docker container for running the application. The Docker host post is mapped to the Docker container port. So whenever you are trying to communicate with any dockerized application, you need to communicate via a docker host port and that will be redirected to the docker container port (where the application is running.&lt;br&gt;
) that corresponds to the Docker host port. How can you specify the port mapping that is also in the link mentioned above?&lt;br&gt;
Ok, so let's explain the final successful solution, I simply mapped the port 3306:3306. But while mapping the port I was facing some errors as this port was already in use by localhost as my MySQL-server was already running at 3306 port. Therefore, I closed the MySQL server and then ran my Docker container with the above mentioned port mapping. Boom, it worked.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
