<?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: Elanchezhiyan Ravichandran</title>
    <description>The latest articles on DEV Community by Elanchezhiyan Ravichandran (@elanchezhiyan).</description>
    <link>https://dev.to/elanchezhiyan</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%2F61724%2F3ae2680a-db01-4882-a401-0569946923e4.jpeg</url>
      <title>DEV Community: Elanchezhiyan Ravichandran</title>
      <link>https://dev.to/elanchezhiyan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elanchezhiyan"/>
    <language>en</language>
    <item>
      <title>Connect Docker Containers</title>
      <dc:creator>Elanchezhiyan Ravichandran</dc:creator>
      <pubDate>Thu, 24 Mar 2022 04:11:59 +0000</pubDate>
      <link>https://dev.to/elanchezhiyan/connect-docker-containers-2i02</link>
      <guid>https://dev.to/elanchezhiyan/connect-docker-containers-2i02</guid>
      <description>&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;I needed multiple docker containers to communicate with each other, through network calls.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Using "BRIDGE &amp;amp; OVERLAY network modes"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Connect Containers in Same Docker Host&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Bridge Network Mode - allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network.&lt;/p&gt;

&lt;p&gt;A default bridge called "bridge" is always created between all the containers in the host.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Connect Containers in Different Docker Host&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Overlays Network Mode - this network sits on top of the host-specific networks, allowing containers connected to it to communicate securely when encryption is enabled.&lt;/p&gt;

&lt;p&gt;For each container you want to add to the network, add the following in &lt;em&gt;docker-compose&lt;/em&gt; file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;networks:
    - local //{network_name}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;and at the end of the file, add the following&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;networks:
local: // {network_name}
    driver: bridge
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Finally, &lt;em&gt;docker-compose&lt;/em&gt; looks like this&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;local-redis:
    image: redis
    ports:
        - "6379:6379"
    volumes:
        - redis-data:/data
    networks:
        - local

local-mongo:
    image: mongo:3.2
    command: ["mongod", "--smallfiles"]
    ports:
        - "27017:27017"
    volumes:
        - mongo-data:/data/db
    networks:
        - local
networks:
local:
    driver: bridge
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And, this is how you make network calls to call other docker containers&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{container_name}:{container_port}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;"mongo_url": "mongodb://local-mongo:27017/analytics"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"redis_host": "local-redis"&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Side Note
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What are Container Port &amp;amp; Host Port?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Container port is the port to be used within a container, when using BRIDGE or USER networking mode.&lt;/p&gt;

&lt;p&gt;A Host port specifies a port on the host machine to bind to.&lt;/p&gt;

&lt;p&gt;In a &lt;em&gt;docker-compose&lt;/em&gt; file, it looks like this,&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;local-application:
    image: ...
    ports:
        - "7602:7600" //{hostPort}:{containerPort}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Inside docker code, you can call like this&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"app_url": "http://local-application:7600"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Outside docker(from browser), you can call like this&lt;/p&gt;

&lt;p&gt;&lt;code&gt;http://localhost:7602/&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://docs.docker.com/network/" rel="noopener noreferrer"&gt;Networking Types&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.docker.com/compose/compose-file/compose-file-v3/#ports" rel="noopener noreferrer"&gt;Docker Compose Ports&lt;/a&gt;&lt;/p&gt;

</description>
      <category>docker</category>
    </item>
  </channel>
</rss>
