{"id":173,"date":"2023-01-02T07:06:24","date_gmt":"2023-01-02T07:06:24","guid":{"rendered":"http:\/\/cloudkul.com\/blog\/?p=173"},"modified":"2024-12-24T11:07:33","modified_gmt":"2024-12-24T11:07:33","slug":"docker-containers-linking","status":"publish","type":"post","link":"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/","title":{"rendered":"Docker Containers Linking"},"content":{"rendered":"<p><span style=\"color: #000000;\">Before reading this blog, do read about the<a href=\"https:\/\/cloudkul.com\/blog\/docker-a-new-era-of-virtualization\/\"> docker containers<\/a>, otherwise, you may not understand how all commands are working in this blog.<\/span><\/p>\n<h2>How to link 2 containers?<\/h2>\n<p>To link containers, first, you need to<a href=\"https:\/\/cloudkul.com\/blog\/start-your-journey-with-docker\/\"> install Docker<\/a> and make sure you installed the 1.8.2 version.<\/p>\n<p>Let&#8217;s start now<\/p>\n<p>First, pull the fresh container by typing:-<\/p>\n<pre class=\"brush:shell\">docker pull ubuntu:14.04<\/pre>\n<p>after pulling it just run the container by typing:-<\/p>\n<pre class=\"brush:shell\">docker run -i -t -d ubuntu:14.04<\/pre>\n<p>now it will provide a terminal for you,<\/p>\n<p>now you can install apache2 in this container by typing:-<\/p>\n<pre class=\"brush:shell\">sudo apt-get update\n\nsudo apt-get install -y apache2<\/pre>\n<p>after that, just commit your container by some name, I would prefer the <strong>&#8220;apache_container&#8221;<\/strong> name.<\/p>\n<p>Let execute the command to commit it:-<\/p>\n<pre class=\"brush:shell\">docker commit container_id apache_container<\/pre>\n<p>now stop all the running containers by typing:-<\/p>\n<p>docker stop $(docker ps -aq)<\/p>\n<p>remove all running containers by typing:-<\/p>\n<pre class=\"brush:shell\">docker rm $(docker ps -aq)<\/pre>\n<p>now to see your container images, just hit the command:-<\/p>\n<pre class=\"brush:shell\">docker images<\/pre>\n<p>you will get two container names:<\/p>\n<p><span style=\"color: #000000;\"><strong>apache_container<\/strong><\/span><br \/>\n<span style=\"color: #000000;\"> <strong>ubuntu:14.04<\/strong><\/span><\/p>\n<p>Your first container is ready, now let&#8217;s proceed further with the second container.<\/p>\n<p>In the second container, we will install the MySQL database &amp; then we will link these two containers with each other.<\/p>\n<p>ok, let&#8217;s hit the command and start your default container ubuntu:14.04 by typing:-<\/p>\n<pre class=\"brush:shell\">docker run -i -t ubuntu:14.04<\/pre>\n<p>now install the database in it by typing:-<\/p>\n<pre class=\"brush:shell\">sudo apt-get update\n\nsudo apt-get install mysql-server<\/pre>\n<p>then restart MySQL service once by typing:-<\/p>\n<pre class=\"brush:shell\">service mysql restart<\/pre>\n<p>After this, check your MySQL user and password by typing:-<\/p>\n<pre class=\"brush:shell\">mysql -u root -p\"give_your_password\"\n<\/pre>\n<p><span style=\"line-height: 1.5;\">And grant permission to your database so it would be accessible from the remote side too, by typing:-<\/span><\/p>\n<pre class=\"brush:shell\">grant all privileges on *.* to root@\"%\" identified by \"your_password\";<\/pre>\n<p>here you are granting complete access to your database to everyone.<\/p>\n<p>First * star stands for all databases, another one stands for all tables and % stands for everyone&#8230;<\/p>\n<p>If you face any problem regarding this, feel free to ask by replying to this post.<\/p>\n<p>Further, Change the bind_address in my.cnf file with your editor (which is located in \/etc\/mysql\/ path) by typing:-<\/p>\n<pre class=\"brush:shell\">nano \/etc\/mysql\/my.cnf<\/pre>\n<p>now search the line where bind-address=127.0.0.1 &amp; change it to bind-address=0.0.0.0<\/p>\n<pre class=\"lang:default decode:true \">save &amp; exit<\/pre>\n<p>Let&#8217;s commit this container by name, I would prefer the &#8220;mysql_db&#8221; name, ok now let&#8217;s commit this container by typing:-<\/p>\n<pre class=\"brush:shell\">docker commit container_id mysql_db<\/pre>\n<p>now stop all the running containers by typing:-<\/p>\n<pre class=\"lang:default decode:true \">docker stop $(docker ps -aq)<\/pre>\n<p>remove all running containers by typing:-<\/p>\n<pre class=\"lang:default decode:true \">docker rm $(docker ps -aq)<\/pre>\n<p>After that, check your container images by typing the command:-<\/p>\n<pre class=\"brush:shell\">docker images<\/pre>\n<p>Now, you will get three container names:<\/p>\n<p><span style=\"color: #000000;\"><strong>apache_container<\/strong><\/span><br \/>\n<span style=\"color: #000000;\"> <strong>mysql_db<\/strong><\/span><br \/>\n<span style=\"color: #000000;\"> <strong>ubuntu:14.04<\/strong><\/span><\/p>\n<h2>Now, the time is to link these two containers&#8230;&#8230;<\/h2>\n<p>Let&#8217;s run these containers to link with each other by typing:-<\/p>\n<pre class=\"brush:shell\">docker run --name db -p 3306:3306 -id mysql_db<\/pre>\n<p>now you will have to restart MySQL service once by typing:-<\/p>\n<pre class=\"brush:shell\">docker exec -i -t db bash<\/pre>\n<p>after the previous command you will get the terminal of your docker container, now just <span style=\"color: #000000;\"><strong>restart MySQL service<\/strong><\/span> and exit from that container.<\/p>\n<p>Your MySQL container is running in daemonize mode, and we named it &#8220;db&#8221; for easy linking during connection.<\/p>\n<p>Let&#8217;s link your &#8220;db&#8221; named container with another container by typing:<\/p>\n<pre class=\"brush:shell\">docker run --name web --link db:db -p 80:80 -i -d apache_container\n\nIn daemonize mode your both container are running and linked as well, now access your web (apache_container) by typing:-<\/pre>\n<pre class=\"brush:shell\">docker exec -i -t web bash<\/pre>\n<p>Further, run this command to access MySQL:-<\/p>\n<pre class=\"brush:shell\">mysql -h db -u root -p\"your_password\"<\/pre>\n<p>Now you can access MySQL from another container.<\/p>\n<p><span style=\"color: #000000;\"><strong>So, Today we learned how to link two containers and share databases between them.<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>In the next blog, we will discuss <a href=\"https:\/\/cloudkul.com\/blog\/how-to-host-website-on-docker-container\/\">how to host a website on docker conatiner<\/a>.<\/strong><\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #000000;\"><strong><a href=\"http:\/\/cloudkul.com\/contact\/\" target=\"_blank\" rel=\"noopener\">FOR ANY TYPE OF QUERY OR HELP, KINDLY CONTACT US<\/a><\/strong><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before reading this blog, do read about the docker containers, otherwise, you may not understand <a class=\"text-primary\" title=\"read more\" href=\"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":4,"featured_media":323,"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":[1],"tags":[5],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Docker Containers Linking - Cloudkul<\/title>\n<meta name=\"description\" content=\"Docker Containers are lightweight and can share the kernel and file system of the same operating system if deployed on a single system.\" \/>\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\/docker-containers-linking\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Docker Containers Linking - Cloudkul\" \/>\n<meta property=\"og:description\" content=\"Docker Containers are lightweight and can share the kernel and file system of the same operating system if deployed on a single system.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudkul\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-02T07:06:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-24T11:07:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2015\/11\/Untitled-9.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=\"Prashant Arora\" \/>\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\/docker-containers-linking\/\",\"url\":\"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/\",\"name\":\"Docker Containers Linking - Cloudkul\",\"isPartOf\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\"},\"datePublished\":\"2023-01-02T07:06:24+00:00\",\"dateModified\":\"2024-12-24T11:07:33+00:00\",\"author\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/0f130d5c45f7a843f035c2b26dfd8822\"},\"description\":\"Docker Containers are lightweight and can share the kernel and file system of the same operating system if deployed on a single system.\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Docker Containers Linking\"}]},{\"@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\/0f130d5c45f7a843f035c2b26dfd8822\",\"name\":\"Prashant Arora\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ffd3d12ef1ac706011cfd6f835707402?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ffd3d12ef1ac706011cfd6f835707402?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"caption\":\"Prashant Arora\"},\"url\":\"https:\/\/cloudkul.com\/blog\/author\/prashant089\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Docker Containers Linking - Cloudkul","description":"Docker Containers are lightweight and can share the kernel and file system of the same operating system if deployed on a single system.","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\/docker-containers-linking\/","og_locale":"en_US","og_type":"article","og_title":"Docker Containers Linking - Cloudkul","og_description":"Docker Containers are lightweight and can share the kernel and file system of the same operating system if deployed on a single system.","og_url":"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/","og_site_name":"Cloudkul","article_published_time":"2023-01-02T07:06:24+00:00","article_modified_time":"2024-12-24T11:07:33+00:00","og_image":[{"width":848,"height":422,"url":"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2015\/11\/Untitled-9.png","type":"image\/png"}],"author":"Prashant Arora","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/","url":"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/","name":"Docker Containers Linking - Cloudkul","isPartOf":{"@id":"https:\/\/cloudkul.com\/blog\/#website"},"datePublished":"2023-01-02T07:06:24+00:00","dateModified":"2024-12-24T11:07:33+00:00","author":{"@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/0f130d5c45f7a843f035c2b26dfd8822"},"description":"Docker Containers are lightweight and can share the kernel and file system of the same operating system if deployed on a single system.","breadcrumb":{"@id":"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudkul.com\/blog\/docker-containers-linking\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cloudkul.com\/blog\/docker-containers-linking\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Docker Containers Linking"}]},{"@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\/0f130d5c45f7a843f035c2b26dfd8822","name":"Prashant Arora","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ffd3d12ef1ac706011cfd6f835707402?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ffd3d12ef1ac706011cfd6f835707402?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","caption":"Prashant Arora"},"url":"https:\/\/cloudkul.com\/blog\/author\/prashant089\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/173"}],"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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":25,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":19234,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/173\/revisions\/19234"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media\/323"}],"wp:attachment":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}