{"id":5074,"date":"2022-10-03T14:27:52","date_gmt":"2022-10-03T14:27:52","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=5074"},"modified":"2025-07-12T05:40:14","modified_gmt":"2025-07-12T05:40:14","slug":"working-with-ports-in-docker-containers","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/working-with-ports-in-docker-containers\/","title":{"rendered":"Docker Tutorials: Working with Ports in Docker Containers"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5106\" src=\"http:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2018\/07\/docker-containers1.png\" alt=\"\" width=\"600\" height=\"315\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/07\/docker-containers1.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/07\/docker-containers1-300x158.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Port expose and publish has to happen when a container is created. Just stop the existing container and create a new one in its place with the added expose and\/or publish options.<\/p>\n<p>By default Docker containers can make connections to the outside world, but the outside world cannot connect to containers. Each outgoing connection will appear to originate from one of the host machine\u2019s own IP addresses thanks to an iptables masquerading rule on the host machine that the Docker server creates when it starts:<\/p>\n<p>[code]$ sudo iptables -t nat -L -n<br>\n&#8230;<br>\nChain POSTROUTING (policy ACCEPT)<br>\ntarget prot opt source destination<br>\nMASQUERADE all &#8212; 172.17.0.0\/16 0.0.0.0\/0[\/code]<\/p>\n<p>The Docker server creates a masquerade rule that lets containers connect to IP addresses in the outside world. If you want containers to accept incoming connections, you will need to provide special options when invoking docker run. There are two approaches.<\/p>\n<p><strong>How to map ports to containers?<\/strong><br>\n<strong>Approach 1<\/strong><br>\nFirst, you can supply -P or &#8211;publish-all=true|false to docker run<br>\nor<br>\nEXPOSE line in the image\u2019s Dockerfile<br>\nor<br>\n&#8211;expose &lt;port&gt; commandline flag and maps it to a host port somewhere within an ephemeral port range.<\/p>\n<p><strong>Approach 2<\/strong><br>\nMapping can be specified explicitly using -p SPEC or &#8211;publish=SPEC option. It allows you to particularize which port on docker server &#8211; which can be any port at all, not just one within the ephemeral port range \u2013 you want mapped to which port in the container.<\/p>\n<p><strong>How to EXPOSE Port on running container?<\/strong><\/p>\n<p><strong>Mehtod 1 &#8211; Using docker commit<\/strong><br>\nCommit your current container to a new image and then do a docker run specifying the new port range and the new image name.<\/p>\n<p>[code]$ docker stop containerID<br>\n$ docker commit containerID newImageName:tag<br>\n$ docker run -d &#8211;name db -p 8091-8094:8091-8094 -p 11210:11210 newImageName:tag[\/code]<\/p>\n<p><strong>Method 2 &#8211; using iptables<\/strong><\/p>\n<p>[code]HOST&gt; iptables -t nat -A DOCKER -p tcp &#8211;dport 443 -j DNAT &#8211;to-destination 172.17.0.2:443<br>\nHOST&gt; iptables -t nat -A POSTROUTING -j MASQUERADE -p tcp &#8211;source 172.17.0.2 &#8211;destination 172.17.0.2 &#8211;dport https<br>\nHOST&gt; iptables -A DOCKER -j ACCEPT -p tcp &#8211;destination 172.17.0.2 &#8211;dport https[\/code]<\/p>\n<p>Docker Tutorials Fundamental To Advanced-2021 Crash Course:- <a href=\"https:\/\/bit.ly\/3hOIbTB\" target=\"_blank\" rel=\"noopener\">https:\/\/bit.ly\/3hOIbTB<\/a><\/p>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_97260\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_59105\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_97260\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Port expose and publish has to happen when a container is created. Just stop the existing container and create a new one in its place with the added expose and\/or publish options. By default Docker containers can make connections to the outside world, but the outside world cannot connect to containers. Each outgoing connection will&#8230;<\/p>\n","protected":false},"author":1,"featured_media":5106,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[4862],"tags":[567,4959,4960,456],"class_list":["post-5074","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker","tag-docker","tag-docker-port","tag-expose","tag-port"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=5074"}],"version-history":[{"count":6,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5074\/revisions"}],"predecessor-version":[{"id":31450,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5074\/revisions\/31450"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/5106"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=5074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=5074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=5074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}