{"id":86,"date":"2021-05-05T13:48:05","date_gmt":"2021-05-05T13:48:05","guid":{"rendered":"https:\/\/blog.sbaranidharan.online\/?p=86"},"modified":"2021-05-10T06:16:44","modified_gmt":"2021-05-10T06:16:44","slug":"docker-macos-expose-container-ports-to-host-machine","status":"publish","type":"post","link":"https:\/\/blog.sbaranidharan.online\/index.php\/2021\/05\/05\/docker-macos-expose-container-ports-to-host-machine\/","title":{"rendered":"Docker: MacOS expose container ports to host machine"},"content":{"rendered":"\n<p>As per the documentation from <a href=\"https:\/\/docs.docker.com\/docker-for-mac\/networking\/\" data-type=\"URL\" data-id=\"https:\/\/docs.docker.com\/docker-for-mac\/networking\/\">docker<\/a>, there are certain limitation in Mac OS environment where we cannot expose ports from docker container to host machine, as it is running on a virtual machine.<\/p>\n\n\n\n<p>If you do not want to communicate outside docker but if you want to connect to different images inside a same docker container or different docker container you can use network-mode to host.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;yaml&quot;,&quot;mime&quot;:&quot;text\/x-yaml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;YAML&quot;,&quot;modeName&quot;:&quot;yaml&quot;}\">network_mode: &quot;host&quot;<\/pre><\/div>\n\n\n\n<h2>But what if we need to expose outside docker to the host machine where the docker is running?<\/h2>\n\n\n\n<p>As a workaround we may need to create a loopback IP address in Mac OS host machine. To do this,<\/p>\n\n\n\n<ol><li>create a file in your current directory named <em><span class=\"has-inline-color has-vivid-cyan-blue-color\">loopback-alias.plist<\/span><\/em> and place below content. Here I&#8217;ve used <em><span class=\"has-inline-color has-vivid-cyan-blue-color\">10.200.10.1<\/span><\/em> as a loopback IP address. But you can modify it if you want.<\/li><\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;xml&quot;,&quot;mime&quot;:&quot;application\/xml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;XML&quot;,&quot;modeName&quot;:&quot;xml&quot;}\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;!DOCTYPE plist&gt;\n&lt;plist version=&quot;1.0&quot;&gt;\n&lt;dict&gt;\n        &lt;key&gt;Label&lt;\/key&gt;\n        &lt;string&gt;loopback-alias&lt;\/string&gt;\n        &lt;key&gt;ProgramArguments&lt;\/key&gt;\n        &lt;array&gt;\n                &lt;string&gt;\/sbin\/ifconfig&lt;\/string&gt;\n                &lt;string&gt;lo0&lt;\/string&gt;\n                &lt;string&gt;alias&lt;\/string&gt;\n                &lt;string&gt;10.200.10.1&lt;\/string&gt;\n                &lt;string&gt;netmask&lt;\/string&gt;\n                &lt;string&gt;255.255.255.0&lt;\/string&gt;\n        &lt;\/array&gt;\n        &lt;key&gt;RunAtLoad&lt;\/key&gt;\n        &lt;true\/&gt;\n&lt;\/dict&gt;\n&lt;\/plist&gt;<\/pre><\/div>\n\n\n\n<p>2. And then run below commands in order to make it available when startup<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text\/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Shell&quot;,&quot;modeName&quot;:&quot;shell&quot;}\">sudo cp loopback-alias.plist \/Library\/LaunchDaemons\/loopback-alias.plist\nsudo launchctl load -w \/Library\/LaunchDaemons\/loopback-alias.plist<\/pre><\/div>\n\n\n\n<p>3. In order to take this effect either you can restart your system or simply restart the network as follows<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text\/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Shell&quot;,&quot;modeName&quot;:&quot;shell&quot;}\">networksetup -setairportpower en0 off &amp;&amp; networksetup -setairportpower en0 on<\/pre><\/div>\n\n\n\n<h2>Changes at docker-compose.yml<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;yaml&quot;,&quot;mime&quot;:&quot;text\/x-yaml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;YAML&quot;,&quot;modeName&quot;:&quot;yaml&quot;}\">redis:\n\timage: redis\n\texpose: \n\t\t- '6379'\n\tports: \n\t\t- '10.200.10.1:6379:6379'\n\textra_hosts: \n\t\t- 'localhost:10.200.10.1'<\/pre><\/div>\n\n\n\n<p>This means, we are using hosts loopback IP where we are exposing the port. And you can access it from your host machine as follows,<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;http&quot;,&quot;mime&quot;:&quot;message\/http&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;HTTP&quot;,&quot;modeName&quot;:&quot;http&quot;}\">http:\/\/10.200.10.1:6379<\/pre><\/div>\n\n\n\n<p>Similarly we can add same configuration to any images to access it from host machine.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>You may need to add the following to your bashrc\/zshrc for MacOS<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text\/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Shell&quot;,&quot;modeName&quot;:&quot;shell&quot;}\">export DOCKER_HOST=unix:\/\/\/var\/run\/docker.sock<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>As per the documentation from docker, there are certain limitation in Mac OS environment where we cannot expose ports from docker container to host machine, as it is running on a virtual machine. If you do not want to communicate outside docker but if you want to connect to different images inside a same docker [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[1],"tags":[23,24,16],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/posts\/86"}],"collection":[{"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":4,"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions\/91"}],"wp:attachment":[{"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}