← Back to Articles         
Docker Port Mapping – Access Containers from Windows

Docker Port Mapping – Access Containers from Windows

Published on 04 Nov 2026 11 min read Docker
Docker Fundamentals

Docker Port Mapping is one of the most important concepts in Docker Networking.

Let's understand it from the basics and then perform practical exercises using your Windows CMD and Docker Desktop.

We will use Nginx and ASP.NET Core Web API examples.

1. What is Docker Port Mapping?

Docker Port Mapping allows applications running inside a Docker container to be accessed from outside the container, such as your Windows machine or another computer.

Consider this example:

You have an ASP.NET Core Web API running inside a Docker container.

Your API listens on port 8080 inside the container.

You want to access it from your Windows browser.

How can you do that?

Docker provides Port Mapping.

        Windows Host Machine
                 |
                 |
          Browser / Postman
                 |
                 V
        http://localhost:8080
                 |
                 |
          Docker Port Mapping
                 |
                 V
          Container Port 8080
                 |
                 V
          ASP.NET Core API

The Windows machine communicates through a host port, and Docker forwards the traffic to the container port.

2. Why do we need Port Mapping?

Imagine you have created a Docker container running an Nginx web server.

Inside the container, Nginx listens on port 80.

You try to access it from your Windows browser:

http://localhost:80

Will it work?

Not necessarily.

Why?

Because port 80 belongs to the container's network environment. It is not automatically exposed on your Windows host.

You need to map the container port to a Windows host port.

For example:

docker run -d -p 8080:80 nginx

Now open:

http://localhost:8080

It works!

Important: Exposing a port in a Dockerfile does not automatically publish it to your Windows machine. You need port publishing using -p or --publish.

3. Understand Host Port and Container Port

This is the most important concept.

Consider:

docker run -d -p 8080:80 nginx

The syntax is:

-p HOST_PORT:CONTAINER_PORT

In our example:

Port

Meaning

8080

Windows Host Port

80

Docker Container Port

Let's understand the communication:

Windows Browser
      |
      V
localhost:8080
      |
      V
Docker Engine
      |
      V
Container Port: 80
      |
      V
Nginx Web Server

Here:

  • Windows sends a request to port 8080.

  • Docker forwards the request to port 80 inside the container.

  • Nginx receives the request.

  • Nginx returns the response.

Important point

Host port and container port do not need to be the same.

For example:

docker run -d -p 5000:80 nginx

Now:

  • Windows port = 5000.

  • Container port = 80.

Access:

http://localhost:5000
 

4. Practical Example 1 – Access Nginx from Windows

Let's perform the first practical exercise.

Step 1: Download the Nginx image

docker pull nginx

This downloads the Nginx image from Docker Hub.

Step 2: Create a container without Port Mapping

docker run -d --name nginx-demo nginx

Check the running container:

docker ps

Example:

CONTAINER ID   IMAGE   PORTS
abc123         nginx   80/tcp

Notice:

80/tcp

This means the container has port 80 exposed in its image metadata.

But it is not published to the Windows host.

Step 3: Try accessing Nginx from Windows

Open your browser:

http://localhost:80

You should not expect to reach this Nginx container through that address.

Why?

Because we didn't configure port publishing.

Step 4: Remove the existing container

docker rm -f nginx-demo

Step 5: Create a container with Port Mapping

docker run -d --name nginx-demo -p 8080:80 nginx

Now check:

docker ps

Example:

CONTAINER ID   IMAGE   PORTS
abc123         nginx   0.0.0.0:8080->80/tcp

Understand this output:

0.0.0.0:8080 -> 80/tcp
  • 0.0.0.0 = Published on all IPv4 host interfaces.

  • 8080 = Host port.

  • 80 = Container port.

Step 6: Access Nginx from Windows

Open:

http://localhost:8080
Success

You should see:

Welcome to nginx!

Congratulations! You have successfully accessed a Docker container from Windows using Port Mapping.

5. Different Port Mapping Scenarios

Docker supports different port mapping configurations.

Scenario 1: Same Host and Container Port

docker run -d -p 8080:8080 nginx
Host Port:      8080
Container Port: 8080

Access:

http://localhost:8080

Note: This example only works if the application inside the container actually listens on port 8080. Standard Nginx listens on port 80, so use a different image configuration or application for this example.

Scenario 2: Different Host and Container Ports

docker run -d -p 5000:80 nginx
Host Port:      5000
Container Port: 80

Access:

http://localhost:5000

Scenario 3: Multiple Port Mappings

Suppose your application exposes two ports:

  • HTTP: 80

  • Management endpoint: 8080

You can map both:

docker run -d -p 5000:80 -p 9000:8080 myapp:v1

Communication:

Windows                Container
  |                        |
  |--- Port 5000 --------> Port 80
  |                        |
  |--- Port 9000 --------> Port 8080

Access:

http://localhost:5000
http://localhost:9000

Both ports must have applications listening inside the container.

Scenario 4: Publish on a Specific Host IP

You can restrict the published port to a particular host interface.

Example:

docker run -d -p 127.0.0.1:8080:80 nginx

Now the service is published on the host loopback interface.

Access:

http://localhost:8080

Other machines on your LAN cannot access this service through your Windows LAN IP using this mapping.

Security recommendation: Use 127.0.0.1 when a service should only be accessible from your local machine.

Scenario 5: Automatically Assign Host Port

You can ask Docker to select an available host port.

docker run -d -p 80 nginx

Here:

  • Container port = 80.

  • Host port = Automatically assigned.

Check the assigned port:

docker port <container-name>

For example:

docker port my-nginx

Example output:

80/tcp -> 0.0.0.0:32768

Access:

http://localhost:32768

6. What is the Difference Between EXPOSE and -p?

This is a very common interview question.

Both are related to container ports, but their purposes are different.

EXPOSE

EXPOSE is a Dockerfile instruction.

It documents the port on which the application is expected to listen inside the container.

Example Dockerfile:

FROM nginx

EXPOSE 80

This indicates that Nginx uses port 80.

However, it does not publish port 80 to Windows.

-p (Publish)

The -p option publishes a container port to a host port.

Example:

docker run -d -p 8080:80 nginx

Now Windows can access the container through port 8080.

EXPOSE vs -p comparison

Feature

EXPOSE

-p

Where used

Dockerfile

docker run command

Purpose

Documents container port

Publishes container port

Makes port accessible from Windows

No

Yes

Required for -p

No

No

Network publishing

No

Yes

Important: You can publish a port using -p even if the Dockerfile does not contain EXPOSE.

7. What is -P (Capital P)?

Docker supports both lowercase -p and uppercase -P.

They are different.

Lowercase -p

You specify the host and container ports.

docker run -d -p 8080:80 nginx

Uppercase -P

Docker publishes all ports declared through EXPOSE to automatically assigned host ports.

docker run -d -P nginx

Check:

docker ps

Example:

PORTS
0.0.0.0:32768->80/tcp

Difference

Command

Purpose

-p 8080:80

Manually map ports

-P

Automatically publish exposed ports

Remember:

  • -p = Manual port mapping.

  • -P = Automatic port publishing for exposed ports.

8. Can Two Containers Use the Same Host Port?

No, not for the same host IP address and protocol combination.

For example:

First container:

docker run -d --name nginx1 -p 8080:80 nginx

Second container:

docker run -d --name nginx2 -p 8080:80 nginx

The second command will fail because host port 8080 is already occupied by the first container's published port.

Solution

Use different host ports.

docker run -d --name nginx2 -p 8081:80 nginx

Now:

Container

Host Port

Container Port

nginx1

8080

80

nginx2

8081

80

Access:

http://localhost:8080
http://localhost:8081

Both containers can run simultaneously.

9. Docker Port Mapping vs Container-to-Container Communication

This is a very important distinction.

Suppose you have two containers:

  • ASP.NET Core API

  • SQL Server

Both are connected to the same user-defined Bridge Network.

           Docker Network
                 |
        +--------+--------+
        |                 |
        V                 V
    API Container    SQL Container
        |                 |
        +-----------------+
           Communication

Your API can connect to SQL Server using:

Server=sqlserver,1433;

You do not need to publish SQL Server port 1433 to Windows just for this communication.

When do you need port mapping?

Requirement

Port Mapping Required?

Access API from Windows browser

Yes

Access SQL Server from SSMS on Windows

Yes

API container connects to SQL Server container

No

Container accesses another container on same user-defined network

No

Container accesses the internet

Usually no inbound port mapping required

Key takeaway: Port Mapping is mainly used to expose container services outside the container's network environment.

10. Practical Example 2 – ASP.NET Core Web API

Now let's apply port mapping to your ASP.NET Core Web API.

Suppose you have built an API and created a Docker image:

myapi:v1

Your ASP.NET Core application listens on port 8080 inside the container.

Step 1: Run the container

docker run -d --name myapi-container -p 5000:8080 myapi:v1

Step 2: Check the container

docker ps

Expected port information:

0.0.0.0:5000->8080/tcp

Step 3: Access the API from Windows

Open your browser or Postman:

http://localhost:5000/api/products

Communication:

Windows Browser / Postman
            |
            V
     localhost:5000
            |
            V
      Docker Engine
            |
            V
     Container Port 8080
            |
            V
     ASP.NET Core Web API
            |
            V
        Products API

Important ASP.NET Core configuration

Your application must listen on an address accessible through the container network interface, not only on localhost inside the container.

For example:

ENV ASPNETCORE_HTTP_PORTS=8080
EXPOSE 8080

Then publish:

docker run -d -p 5000:8080 myapi:v1

The host port is 5000, and the container port is 8080.

Common mistake: If ASP.NET Core listens only on localhost inside the container, publishing the port may not make the API accessible. Ensure it listens on 0.0.0.0 or the appropriate container network interface.

11. Docker Port Mapping Commands

Command

Purpose

docker run -p 8080:80 nginx

Publish container port 80 on host port 8080

docker run -P nginx

Automatically publish exposed ports

docker ps

View published ports

docker port container-name

Display container port mappings

docker inspect container-name

Inspect container network and port configuration

docker stop container-name

Stop container

docker rm container-name

Remove container

Check port mapping

docker port nginx-demo

Example:

80/tcp -> 0.0.0.0:8080

Inspect port mapping

docker inspect nginx-demo

Look for:

"PortBindings": {
    "80/tcp": [
        {
            "HostPort": "8080"
        }
    ]
}
 

12. Troubleshooting Common Port Mapping Issues

These are common problems developers face.

Problem

Possible Reason

Solution

Cannot access localhost

Port not published

Use -p

Connection refused

Application not listening

Check application port

Port already allocated

Host port already occupied

Choose another host port

API not accessible

Application listening only on localhost inside container

Bind to 0.0.0.0

Wrong URL

Incorrect host port

Check docker ps

API works inside container but not from Windows

Incorrect port mapping or application binding

Verify both

Useful troubleshooting commands

Check running containers:

docker ps

Check logs:

docker logs myapi-container

Check port mapping:

docker port myapi-container

Check Windows port usage:

netstat -ano | findstr :8080

13. Important Interview Questions

No.

Interview Question

Severity

1

What is Docker Port Mapping?

High

2

Why do we need port mapping?

High

3

What is the difference between Host Port and Container Port?

High

4

What is the difference between -p and -P?

High

5

What is the difference between EXPOSE and -p?

High

6

Can multiple containers use the same host port?

High

7

Can we map multiple ports to one container?

Medium

8

How do you automatically assign a host port?

Medium

9

How do you restrict a published port to localhost?

Medium

10

Do containers need port mapping to communicate with each other?

High

11

How do you check published ports?

Medium

12

Why might a published ASP.NET Core port still be inaccessible?

High

 

14. Practical Exercises – Windows CMD

Let's verify your understanding with these exercises.

Port Mapping Practice Checklist

0/10 completed
 
Reset checklist

15. Final Summary – Remember These Concepts

Concept

Explanation

Port Mapping

Connects host port to container port

-p

Manually publish a port

-P

Automatically publish exposed ports

EXPOSE

Documents the container port

Host Port

Port on Windows

Container Port

Port inside Docker container

localhost

Refers to the machine or network namespace where the client runs

docker port

Displays published port mappings

One important command to remember:

docker run -d --name myapi -p 5000:8080 myapi:v1

Meaning:

Windows
   |
   V
localhost:5000
   |
   V
Docker Port Mapping
   |
   V
Container:8080
   |
   V
ASP.NET Core Web API

My recommendation: Practise the Nginx examples first, then repeat the same process with your DockerDemo.API project. Once you understand port mapping, Docker Compose port configuration (ports:) will be much easier to learn.

 
← Back to Articles