<?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/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									Docker Expose Port: How to get traffic into your container - Kubernetes and Containers				            </title>
            <link>https://www.virtualizationhowto.com/community/kubernetes-and-containers/docker-expose-port-how-to-get-traffic-into-your-container/</link>
            <description>Virtualization Howto Discussion Board</description>
            <language>en-US</language>
            <lastBuildDate>Sun, 16 Aug 2026 17:15:54 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>Docker Expose Port: How to get traffic into your container</title>
                        <link>https://www.virtualizationhowto.com/community/kubernetes-and-containers/docker-expose-port-how-to-get-traffic-into-your-container/#post-1157</link>
                        <pubDate>Fri, 31 Jan 2025 04:39:29 +0000</pubDate>
                        <description><![CDATA[When you spin up Docker containers, there are generally two types of communication that you want to configure. That is communication between containers and communication from the outside. Do...]]></description>
                        <content:encoded><![CDATA[<p>When you spin up Docker containers, there are generally two types of communication that you want to configure. That is communication<span> </span><strong>between<span> </span></strong>containers and communication<span> </span><strong>from the outside</strong>. Docker allows you to expose docker ports in a couple of different ways for each of these use cases. Let’s take a look at what you need to know to expose a port or multiple ports to your Docker containers.</p>
<h2 id="what-does-docker-expose-port-mean" class="wp-block-heading">What Does Docker Expose Port Mean?</h2>
<p>When you “expose” a port or multiple ports, it means that you are making that network port on a<span> </span>Docker container available<span> </span>to be connected to from either the Docker network or the outside world. By default, Docker containers are isolated in the way their networking is designed and are not accessible unless you configure this.</p>
<p>Isolation helps with security as if a port doesn’t need to be exposed to the outside, it shouldn’t be. And, if you can limit the number of network ports you expose, the better off you will be from an attack surface. However, the downside is that you will need to think about which ports need to be open for necessary or expected communication with your containers.</p>
<h2 id="how-to-expose-ports-in-docker" class="wp-block-heading">How to Expose Ports in Docker</h2>
<p>Let’s take a look at how to expose ports in Docker and see what commands are needed for opening a Docker port.</p>
<p>To expose ports in Docker, you use the docker run command with the<span> </span><strong>-p or –publish flag<span> </span></strong>for publish ports functionality. This flag specifies which ports are available for connection. Let’s look at a simple example of exposing port 80 on an Nginx Docker image web server using port 8080 on the outside.</p>
<pre class="wp-block-code" contenteditable="false"><code>docker run -d -p 8080:80 nginx</code></pre>
<p>In this command,<span> </span><strong>8080 is the host port</strong>, and<span> </span><strong>80 is the container port</strong>. The -d flag runs the container in<span> </span><strong>detached<span> </span></strong>mode which is how you want to run containers to keep them running without having to have a console connection to your Docker container host.</p>
<p>The port expose configuration via a port number means that you are exposing the container on the same network internally to the Docker network. By default when you spin up a new container and don’t specify the container network, they are connected to the default bridge network on the Docker host.</p>
<h2 id="configuring-container-ports-to-listen" class="wp-block-heading">Configuring container ports to listen</h2>
<p>When<span> </span>creating a Docker container<span> </span>from a docker container image, configuring container ports that you want the container to listen on is an important step. To do this, we use a special instruction called<span> </span><strong>EXPOSE<span> </span></strong>in the Dockerfile. For example let’s look at what expose instructions for ports 80 and 443 would look like. Pretty simple:</p>
<pre class="wp-block-code" contenteditable="false"><code>EXPOSE 80 EXPOSE 443</code></pre>
<p>This EXPOSE instruction tells Docker that the container should expose multiple ports, 80 and 443.</p>
<p>Do you have to do this necessarily? No, if the container image already is exposing the ports, then the configuration will have this by default and you won’t have to “re-expose” the port in your Docker directives.</p>
<p>Case in point, if you see the below, you can see the port<span> </span><strong>81/tcp</strong><span> </span>is being exposed on the container, but you don’t see a host side port mapping.</p>
<div class="wp-block-image">
<figure>
507
<br />
<figcaption class="wp-element-caption">viewing an exposed port in docker</figcaption>
</figure>
</div>
<p>In addition, the Docker Compose code for the container above (Nginx Proxy Manager) looks like the following, so you don’t see any directives on the ports side or an expose directive for port 81. So, it means the container image is already exposing this port and the container listens on this exposed port by default.</p>
<div class="wp-block-image">
<figure>
508
<br />
<figcaption class="wp-element-caption">published ports in a docker compose file</figcaption>
</figure>
</div>
<h2 id="docker-container-port-mapping" class="wp-block-heading">Docker Container Port Mapping</h2>
<p>Let’s look at the Docker container port mapping construct. This is the configuration that allows mapping internal container ports to external host ports. When you do this, it allows traffic that is destined for the host IP network interface to be forwarded inward to the container and vice versa.</p>
<pre class="wp-block-code" contenteditable="false"><code>docker run -d -p 5000:5000 testapp</code></pre>
<p>This command maps<span> </span><strong>port 5000 on the host to port 5000 on the container</strong>. This configures communication to the container’s ports via the host IP address and makes the container accessible by the host system. Otherwise, you wouldn’t be able to communicate with it.</p>
<h2 id="configuring-the-exposed-ports-with-docker-compose" class="wp-block-heading">Configuring the exposed Ports with Docker Compose</h2>
<p>Docker Compose is the means by which you can spin up Docker container “stacks” that allow multiple containers to be provisioned at once. This is a great way to work with Docker containers if you have an application that requires multiple containers as part of the overall application architecture.</p>
<p>Docker Compose uses YAML code to describe the container configuration. The<span> </span><strong>ports</strong><span> </span>directive configures published ports available for connection from the host into the container.</p>
<pre class="wp-block-code" contenteditable="false"><code>version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"</code></pre>
<p>In this configuration, the web service exposes port 80 of the container on port 8080 of the host machine.</p>
<h2 id="verifying-exposed-ports-with-docker-ps-and-docker-compose-ps" class="wp-block-heading">Verifying Exposed Ports with Docker PS and Docker-Compose PS</h2>
<p>When you provision a container, you can verify the exposed port mappings using the<span> </span><strong>docker ps</strong><span> </span>command.</p>
<pre class="wp-block-code" contenteditable="false"><code>docker ps</code></pre>
<p>Look for the PORTS column in the output to see the mappings</p>
<p><img src="https://www.virtualizationhowto.com/wp-content/uploads/wpforo/attachments/2/510-using-docker-ps-to-view-port-configurations.png" /></p>
<p>When you use<span> </span><strong>docker-compose ps</strong><span> </span>command, it shows the same information, just for the application stack configured via the Docker Compose file:</p>
<p><img src="https://www.virtualizationhowto.com/wp-content/uploads/wpforo/attachments/2/511-viewing-ports-using-docker-compose-ps.png" /></p>
<h2 id="troubleshooting-docker-expose-port-mappings" class="wp-block-heading">Troubleshooting Docker expose port mappings</h2>
<p>There are a few issues that could come up when you are trying to expose a specified port for a container. Note the following:</p>
<ul class="wp-block-list">
<li>Make sure the port is not in use by another container</li>
<li>Make sure the container is attached to the Docker network that you assume it is connected to</li>
<li>Is the port mapped to the host port you expect?</li>
<li>If you are just exposing a port to use with something like Nginx Proxy Manager, make sure the port proxy is configured corrected to forward traffic to the internally exposed ports of the Docker container.</li>
</ul>
<h2 id="best-practices-for-exposing-ports" class="wp-block-heading">Best Practices for Exposing Ports</h2>
<ol class="wp-block-list">
<li><strong>Use Non-Standard Ports:</strong><span> </span>Common ports like 80 and 443 are going to be widely used, so you can use non-standard ports to avoid conflicts</li>
<li><strong>Limit Exposed Ports:</strong><span> </span>Only expose Docker container ports as this will help to avoid security vulnerabilities by only exposing what is absolutely needed</li>
<li><strong>Document Exposed Ports:</strong><span> </span>Maintain clear documentation of which ports are exposed and their purposes.</li>
</ol>]]></content:encoded>
						                            <category domain="https://www.virtualizationhowto.com/community/kubernetes-and-containers/">Kubernetes and Containers</category>                        <dc:creator>Brandon Lee</dc:creator>
                        <guid isPermaLink="true">https://www.virtualizationhowto.com/community/kubernetes-and-containers/docker-expose-port-how-to-get-traffic-into-your-container/#post-1157</guid>
                    </item>
							        </channel>
        </rss>
		