{"id":3424,"date":"2024-12-13T11:37:28","date_gmt":"2024-12-13T11:37:28","guid":{"rendered":"https:\/\/cloudkul.com\/blog\/?p=3424"},"modified":"2024-12-13T11:37:31","modified_gmt":"2024-12-13T11:37:31","slug":"understanding-communication-docker-containers","status":"publish","type":"post","link":"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/","title":{"rendered":"Understanding Communication Between Docker Containers"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2024\/11\/understanding-communication-between-docker-containers.png\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"440\" src=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2024\/11\/understanding-communication-between-docker-containers.png\" alt=\"understanding-communication-between-docker-containers\" class=\"wp-image-18513\"\/><\/a><\/figure>\n\n\n<p>In our earlier blogs, we have seen various architectures of <a href=\"https:\/\/cloudkul.com\/docker\/\">docker containers<\/a> to deploy the <a href=\"https:\/\/cloudkul.com\/blog\/how-to-install-magento-2-in-aws\/\">Magento 2 server<\/a>.<\/p>\n<p>We have seen how to set up a single container architecture that included a web server and database server inside the single container using Dockerfile.<\/p>\n<p>We have also seen multi-container architectures having different services being run on different containers.<\/p>\n<p>These containers were interlinked with each other and were configured by a <a href=\"https:\/\/cloudkul.com\/blog\/docker-a-new-era-of-virtualization\/\">docker-compose<\/a> tool which uses a single configuration file docker-compose.yml file to deploy all the containers.<\/p>\n<p>In between these setups, we have used various terms like <a href=\"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/\"><strong>c<\/strong><\/a><strong><a href=\"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/\">ontainer linking,<\/a> port exposure, volume mapping, etc, <\/strong>but we had never discussed how containers communicate with each other.<\/p>\n<p>So let&#8217;s take a break from setting up docker architecture and understand communication between docker containers.<\/p>\n<h2><strong>Docker Network<\/strong><\/h2>\n<p>Docker utilizes three inbuilt networks that come with fresh <a href=\"https:\/\/cloudkul.com\/blog\/magento-2-docker-installation\/\">docker installation<\/a>. If you run,<\/p>\n<pre class=\"lang:default decode:true\">docker network ls<\/pre>\n<p>you might see three networks:<\/p>\n<ul>\n<li>Bridge<\/li>\n<li>None<\/li>\n<li>Host<\/li>\n<\/ul>\n<p><strong>Let&#8217;s take a brief overview of these networks<\/strong><\/p>\n<ul>\n<li>A bridge network is the default network that comes with all docker installations. It is also known as the docker0 network.<\/li>\n<li>If not mentioned otherwise, all docker containers are created within the docker0 network. No network is generally known as a container-specific network.<\/li>\n<li>A container can be attached to a network. This is utilized for internal communication between containers being isolated to the outside network.<\/li>\n<li>The host network adds a container to the host\u2019s network stack. As far as the network is concerned, there is no isolation between the host machine and the container.<\/li>\n<li>For instance, if you run a container that runs a web server on port 80 using host networking, the web server is available on port 80 of the host machine.<\/li>\n<\/ul>\n<p>Apart from these built-in networks, there are also user-defined networks.<\/p>\n<p>We can create our network and control which containers can communicate with each other, and also enable automatic <a href=\"https:\/\/cloudkul.com\/blog\/role-of-dns-in-website-performance\/\">DNS resolution<\/a> of container names to IP addresses.<\/p>\n<p>With the help of default network drivers being provided, we can create the following networks:<\/p>\n<ul>\n<li>Bridge network<\/li>\n<li>Overlay network<\/li>\n<li>MACVLAN network<\/li>\n<\/ul>\n<p>However, here we will keep our discussion limited to the default bridge network.<\/p>\n<h2>Bridge Network<\/h2>\n<p>As mentioned earlier, docker containers are attached to a bridge or docker0 network by default if no other network is mentioned.<\/p>\n<p>Take note that all containers within the same bridge network can communicate with each other via IP addresses.<\/p>\n<p>However, they cannot resolve container names so communication via container names is not possible.<\/p>\n<p>To ensure communication via container names, these containers are needed to link with each other in one way or another.<\/p>\n<p>Docker0 bridge allows port mapping and linking to allow communication among containers or communication between container and host.<\/p>\n<p>Communication between containers is managed at an operating system level and it depends on two factors:<\/p>\n<ul>\n<li>By default Docker daemon attaches all containers to the docker0 bridge, providing network address translation for their communication.<\/li>\n<li>So it is mandatory that network topology can identify containers&#8217; networks.<\/li>\n<li>Connection establishing permissions from iptables<\/li>\n<\/ul>\n<p>For communication between the host and containers, it is mandatory that iptables settings and port mapping are done correctly to transfer packets between the docker container and hosts.<\/p>\n<h2><strong>Understanding Docker Containers Linking By Example:<\/strong><\/h2>\n<p>Let us understand <a href=\"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/\">container linking<\/a> and port exposure by an example. We will link apache2 and MySQL-server containers using docker command line tools.<\/p>\n<p>First, we will need a mysql-server image,<\/p>\n<pre class=\"lang:default decode:true \">docker pull mysql\/mysql-server:5.6\n\n<\/pre>\n<p>Then we will launch a container from this image,<\/p>\n<pre class=\"lang:default decode:true \">docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=rootpassword123 -d mysql\/mysql-server:5.6<\/pre>\n<p>In the above command, the -p arguments map the host&#8217;s port with the container&#8217;s port, and the &#8211;name argument defines the container&#8217;s name.<\/p>\n<p>We are mapping the 3306 port of the host with the 3306 port inside the container. All traffic to and from the MySQL server will be routed from these ports.<\/p>\n<p>Make sure ports on the host are available to be used.<\/p>\n<p>After image and docker container creation, we can check their status as,<\/p>\n<pre class=\"lang:default decode:true \">docker images\n\ndocker ps<\/pre>\n<p>Now, lets pull apache2 server with php5 image as,<\/p>\n<pre class=\"lang:default decode:true \">docker pull nimmis\/apache-php5\n\n<\/pre>\n<p>Now that we have the MySQL container already running, we will launch the apache2 container being linked to the MySQL container as,<\/p>\n<pre class=\"lang:default decode:true \">docker run -tid -p 80:80 --name apache2 --link mysql nimmis\/apache-php5<\/pre>\n<p>In the above command, <strong>we have launched a container named apache2 whose port 80 is mapped with the 80 port of the host, and this container is linked with our database container named mysql by &#8211;link<\/strong><\/p>\n<p>Check the networks of both containers as:<\/p>\n<pre class=\"lang:default decode:true \">docker inspect mysql\n\ndocker inspect apache2<\/pre>\n<p>You will see the default bridge network for both containers.<\/p>\n<p>We can check this connectivity by,<\/p>\n<pre class=\"lang:default decode:true \">docker exec -ti apache2 bash\n\nping mysql<\/pre>\n<p>and,<\/p>\n<pre class=\"lang:default decode:true\">docker exec -ti mysql bash\n\nping apache2<\/pre>\n<p>If we get a response for both the pings, then the docker containers have been linked properly.<\/p>\n<h2>Conclusion<\/h2>\n<p>So far we have seen how to link to docker containers within the default bridge network from simple command-line docker commands.<\/p>\n<p>In our later blogs, we will discuss more user-defined networks and container communication.<\/p>\n\n\n<h2 class=\"wp-block-heading\">Need Support?<\/h2>\n\n\n\n<p>Thank You for reading this Blog!<\/p>\n\n\n\n<p>For further more interesting blogs, keep in touch with us. If you need any kind of support, simply raise a ticket at&nbsp;<strong><a href=\"https:\/\/webkul.uvdesk.com\/en\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/webkul.uvdesk.com\/en\/<\/a><em>.<\/em><\/strong><\/p>\n\n\n\n<p><strong>For further help or queries, please&nbsp;<a href=\"https:\/\/cloudkul.com\/contact\/\">contact<\/a>&nbsp;us or raise a&nbsp;<a href=\"https:\/\/webkul.uvdesk.com\/en\/customer\/create-ticket\/\">ticket<\/a>.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our earlier blogs, we have seen various architectures of docker containers to deploy the <a class=\"text-primary\" title=\"read more\" href=\"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":7,"featured_media":3425,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,35,86,1,36],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Understanding Communication Between Docker Containers - Cloudkul<\/title>\n<meta name=\"description\" content=\"Docker containers are attached to a docker network. All containers within the same bridge network can communicate with each other via IP addresses.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understanding Communication Between Docker Containers - Cloudkul\" \/>\n<meta property=\"og:description\" content=\"Docker containers are attached to a docker network. All containers within the same bridge network can communicate with each other via IP addresses.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudkul\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-13T11:37:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-13T11:37:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2017\/09\/download-3.png\" \/>\n\t<meta property=\"og:image:width\" content=\"848\" \/>\n\t<meta property=\"og:image:height\" content=\"422\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Alankrit Srivastava\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/\",\"url\":\"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/\",\"name\":\"Understanding Communication Between Docker Containers - Cloudkul\",\"isPartOf\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\"},\"datePublished\":\"2024-12-13T11:37:28+00:00\",\"dateModified\":\"2024-12-13T11:37:31+00:00\",\"author\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/fc06bfd7f18d9a606dd94062d205af16\"},\"description\":\"Docker containers are attached to a docker network. All containers within the same bridge network can communicate with each other via IP addresses.\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Understanding Communication Between Docker Containers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\",\"url\":\"https:\/\/cloudkul.com\/blog\/\",\"name\":\"Cloudkul\",\"description\":\"Host your eCommerce Store on AWS with Optimized Performance\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudkul.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/fc06bfd7f18d9a606dd94062d205af16\",\"name\":\"Alankrit Srivastava\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/955c3dda2678272c436c5153832e401f?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/955c3dda2678272c436c5153832e401f?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"caption\":\"Alankrit Srivastava\"},\"description\":\"DevOps Manager at Webkul Software Privated Limited\",\"sameAs\":[\"http:\/\/cloudkul.com\"],\"url\":\"https:\/\/cloudkul.com\/blog\/author\/alankrit-srivastava256\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Understanding Communication Between Docker Containers - Cloudkul","description":"Docker containers are attached to a docker network. All containers within the same bridge network can communicate with each other via IP addresses.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/","og_locale":"en_US","og_type":"article","og_title":"Understanding Communication Between Docker Containers - Cloudkul","og_description":"Docker containers are attached to a docker network. All containers within the same bridge network can communicate with each other via IP addresses.","og_url":"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/","og_site_name":"Cloudkul","article_published_time":"2024-12-13T11:37:28+00:00","article_modified_time":"2024-12-13T11:37:31+00:00","og_image":[{"width":848,"height":422,"url":"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2017\/09\/download-3.png","type":"image\/png"}],"author":"Alankrit Srivastava","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/","url":"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/","name":"Understanding Communication Between Docker Containers - Cloudkul","isPartOf":{"@id":"https:\/\/cloudkul.com\/blog\/#website"},"datePublished":"2024-12-13T11:37:28+00:00","dateModified":"2024-12-13T11:37:31+00:00","author":{"@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/fc06bfd7f18d9a606dd94062d205af16"},"description":"Docker containers are attached to a docker network. All containers within the same bridge network can communicate with each other via IP addresses.","breadcrumb":{"@id":"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cloudkul.com\/blog\/understanding-communication-docker-containers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Understanding Communication Between Docker Containers"}]},{"@type":"WebSite","@id":"https:\/\/cloudkul.com\/blog\/#website","url":"https:\/\/cloudkul.com\/blog\/","name":"Cloudkul","description":"Host your eCommerce Store on AWS with Optimized Performance","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudkul.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/fc06bfd7f18d9a606dd94062d205af16","name":"Alankrit Srivastava","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/955c3dda2678272c436c5153832e401f?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/955c3dda2678272c436c5153832e401f?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","caption":"Alankrit Srivastava"},"description":"DevOps Manager at Webkul Software Privated Limited","sameAs":["http:\/\/cloudkul.com"],"url":"https:\/\/cloudkul.com\/blog\/author\/alankrit-srivastava256\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/3424"}],"collection":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/comments?post=3424"}],"version-history":[{"count":26,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/3424\/revisions"}],"predecessor-version":[{"id":18520,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/3424\/revisions\/18520"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media\/3425"}],"wp:attachment":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media?parent=3424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/categories?post=3424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/tags?post=3424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}