<?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: Anirudh Mathad</title>
    <description>The latest articles on DEV Community by Anirudh Mathad (@asthinasthi).</description>
    <link>https://dev.to/asthinasthi</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%2F813937%2F3edd2fc0-aa9e-4f74-ac21-fe89ef6b16b3.jpeg</url>
      <title>DEV Community: Anirudh Mathad</title>
      <link>https://dev.to/asthinasthi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asthinasthi"/>
    <language>en</language>
    <item>
      <title>Service to Service Communication between containers on your localhost
</title>
      <dc:creator>Anirudh Mathad</dc:creator>
      <pubDate>Sun, 13 Feb 2022 22:33:09 +0000</pubDate>
      <link>https://dev.to/asthinasthi/service-to-service-communication-between-containers-on-your-localhost-371p</link>
      <guid>https://dev.to/asthinasthi/service-to-service-communication-between-containers-on-your-localhost-371p</guid>
      <description>&lt;p&gt;tldr;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inside the containers other containers can be called by their container names&lt;/li&gt;
&lt;li&gt;Outside the containers (your laptop) they need to be called by localhost:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have a ☕️ Java Tomcat Service A that talks to 🐍 Python Flask Service B.&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%2Fzwtu0qlvujzf35a1exam.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%2Fzwtu0qlvujzf35a1exam.png" alt="Tomcat Service A hosted at 8080 talks to Flask Service B hosted at 9090"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Service A runs on 8080 &amp;amp; Service B on 9090. See I am smart💡 because I chose different ports to avoid port conflicts.&lt;/p&gt;

&lt;p&gt;Everything works great in the non-container world. From my Java Service A, service discovery is easy peasy. You call &lt;a href="http://localhost:9090/serviceB" rel="noopener noreferrer"&gt;http://localhost:9090/serviceB&lt;/a&gt; and we are good.&lt;/p&gt;

&lt;h2&gt;
  
  
  When ServiceB get “pip”ed
&lt;/h2&gt;

&lt;p&gt;As I add Python libs in the Service B it becomes nasty. I hate your GCC compiler (Mac vs Linux) or some dependency of dependency, gives you an error, which doesn’t tell exactly what is wrong and holds your weekend ransom 🔫 . Shit Happens! 💩&lt;/p&gt;

&lt;p&gt;So my in my infinite wisdom I containerize 🐳 them. It’s a little slow but better than managing the chaos of dependencies. If you have a million Python projects on your laptop this is appreciated even more.&lt;/p&gt;

&lt;p&gt;Alright I containerized them. Because I containerized, I don’t have to worry about port conflicts.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I will use 8080 because Java has institutionalized me to use 8080.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Cool Kids at React use 3000! The lazy just me wants to ping &lt;a href="http://localhost:8080/serviceB" rel="noopener noreferrer"&gt;http://localhost:8080/serviceB&lt;/a&gt; and voila nothing happens! Aaargh! Why? Meanwhile, I have switched between Reddit, Social Media, Twitter and WSJ by now. Because I hate being stuck and my ADHD starts distracting me.&lt;/p&gt;

&lt;p&gt;I gather myself, let’s google it. Can’t access containerized App from localhost. Within 2 secs I learn about the port mapping. The restless me has already updated by the port mapping to -p 8080:9090 Doesn’t work. Dang! Why does this happen to me? I should have been an MBA, wouldn’t have to deal with this shit. Another ADHD trip and Googling reveals, now I learnt the port mapping must be reversed. &lt;code&gt;-p 9090:8080&lt;/code&gt; Phew! I ping the endpoint &lt;a href="http://localhost:9090" rel="noopener noreferrer"&gt;http://localhost:9090&lt;/a&gt; and feel like Mark Zuckerberg. Billion dollar Unicorn here I come.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time for Service to Docker Communication
&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%2Fj4m943qet1flesdhztad.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%2Fj4m943qet1flesdhztad.png" alt="Tomcat Service talks to Containerized Flask Service B which has port exposed at 9090"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I haven’t containerized Service A. I mean it’s a Java Bulldozer who wants to containerize and make it painfully slower? I update my configs to call Containerized Service B &lt;a href="http://localhost:9090/serviceB" rel="noopener noreferrer"&gt;http://localhost:9090/serviceB&lt;/a&gt; and of course I feel like a million dollars.&lt;/p&gt;

&lt;p&gt;I have solved the notorious Python dependency problem with a container and that’s as much I am willing to go with containers. I mean I have a life, I can’t get sucked into the containers. I think outside the Box, I am a visionary!&lt;/p&gt;

&lt;h2&gt;
  
  
  When Contributors burst your bubble! 💥
&lt;/h2&gt;

&lt;p&gt;After I feel like I have conquered the world, I write a detailed doc on how to contribute to this awesomeness. Add pictures, gotchas, snippets and the whole shebang! (are we still allowed to use this?, I wish there was a linter that could check those, may be there is, too lazy to research)&lt;/p&gt;

&lt;p&gt;An enthusiast forks my service and asks me what is JDK, JRE, what is maven?🤦🏽‍♂️ but he is supposed to be a Python Guru.&lt;br&gt;
I forgot there is a world beyond Java, where people have fun with programming! They don’t want to learn the whole Java stack (or anything remotely close), they just want to contribute to Python stuff.&lt;/p&gt;

&lt;p&gt;Back to the box again! I containerized Java. Yay!&lt;/p&gt;
&lt;h2&gt;
  
  
  localhost:9090/serviceB doesn’t work from my Java container!
&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%2F24ax96wcesoxmyceozom.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%2F24ax96wcesoxmyceozom.png" alt="Containerized Tomcat talks to Containerized Flask"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ok, I get it! localhost for Java container means its own container and there is nothing on 9090 there. You gotta use the “hostname” and may be use docker-compose.yml to make it elegant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3.8'
services:
  serviceA:
    build:
      context: .
    ports:
      - "8080:8080"
  serviceB:
    image: serviceB-image
    hostname: hostB
    ports:
      - "9090:8080"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, look at me, how fluent and disciplined. Before, I update my Java calls to &lt;a href="http://hostB:9090/serviceB" rel="noopener noreferrer"&gt;http://hostB:9090/serviceB&lt;/a&gt; I want to test it! Because I am a smart Engineer and I can tell you Battle stories 🏰 of how I went in without testing and got bruised etc.&lt;/p&gt;

&lt;p&gt;So I ping &lt;a href="http://hostB:9090/serviceB" rel="noopener noreferrer"&gt;http://hostB:9090/serviceB&lt;/a&gt; from my laptop. Unknown Host! Wow! I have been taken for a ride. This whole docker compose is a scam right? I mean you say something but you won’t mention how to validate it and get away with it. Just like Politicians!&lt;/p&gt;

&lt;p&gt;Googling as always gives more wisdom than I can digest. I just want to finish this and doom scroll my Reddit feed man!&lt;/p&gt;

&lt;p&gt;One dude or dudette says you gotta update &lt;code&gt;/etc/hosts&lt;/code&gt; the other use docker network Someone wrote a Python script to update &lt;code&gt;/etc/hosts&lt;/code&gt; I mean people have a lot of time No? Don’t people have to take ADHD trips or doom scroll their Twitter feed? Seriously people get a life!&lt;/p&gt;

&lt;p&gt;All the solutions were too nerdy for me. I just want to do it with a few lines of change. I am the first mouse of Who Moved My Cheese? 🧀 I refuse to goto another Cheese station and will wait here for the Cheese to comeback.&lt;/p&gt;

&lt;h2&gt;
  
  
  Occam’s Razor
&lt;/h2&gt;

&lt;p&gt;An entire weekend passes, turns out hostB is accessible from inside Java Container and not my local machine. You don’t even need to add the hostname field. FML!&lt;/p&gt;

&lt;p&gt;And if I had not decided to validate, it would have worked and I would saved my weekend and be much younger when I write this! So much for learning from your mistakes.&lt;br&gt;
The good news is we get to do all over again next Friday!&lt;/p&gt;

</description>
      <category>docker</category>
      <category>microservices</category>
      <category>java</category>
      <category>network</category>
    </item>
  </channel>
</rss>
