docker port forwarding
PUREVPNPort ForwardingDocker Port Forwarding 

Docker is a popular container-based framework to develop, package, and deploy applications. Containers are portable, lightweight environments that can run applications reliably across several settings. 

Docker offers a simple yet efficient approach for packaging an app and all of its dependencies into a container image that can be readily deployed on any Docker-compatible platform. 

Port forwarding allows traffic to be sent from one network port to another. This approach is critical in network communication and plays an important role when using Docker.

docker port forwarding

What is CGNAT?

For most users, getting access to ports behind CGNAT is a common problem. Internet service providers use Carrier-Grade Network Address Translation (CGNAT) as a technique to retain IP addresses. 

As CGNAT restricts inbound connections, accessing devices or services behind it could be challenging.

Port forwarding in Docker

Port forwarding in Docker allows you to expose container ports to the outside world. Docker containers run in isolation by default and are only able to communicate with other containers on the same network. 

For making a container accessible from the outside world, or for mapping a container port to a host port, port forwarding will work.

Anniversary Special! Enjoy Our Best Deal Ever!

On this year's PureVPN anniversary, get an Exclusive PureVPN plan at a 83% discount - starting from $1.99/month

How can PureVPN help get around CGNAT?

PureVPN’s port forwarding add-on offers you a safe and secure way to unblock ports behind CGNAT, allowing you to remotely access any devices and services. PureVPN assigns you a unique public IP address, making it easy to access ports behind CGNAT and ensuring maximum efficiency and security.

  1. Download and install PureVPN on your device.
  2. Connect to a port-forwarding-supporting server.
  3. Enable port forwarding in the app by going to the port forwarding tab.
  4. Specify the port number to be forwarded, along with the IP address of the device to which it has to be forwarded.
  5. Click Apply to save the changes

CGNAT port forwarding

How to forward ports in Docker

Step 1: Identify the Container Port

For port forwarding, the first step is determining which container port you wish to expose. This may be accomplished by viewing the Docker container through the following command:

docker inspect <container_name_or_id>

This command will return a full JSON representation of the container setup. Check for the “ExposedPorts” section.

Step 2: Choose a host port

Once the container port has been identified, you must select a host port to which it will be mapped. A host port on the Docker host is a port that receives traffic from the container port. On the Docker host, you can select any accessible port. Port 8080 will be used in this example.

Step 3: Map the ports

The “docker run” command is then used for mapping the container port to the host port. 

Following is the syntax of the command:

docker run -p <host_port>:<container_port> <image_name>

Now the host port 8080 will be mapped to container port 80 for the “nginx” image:

docker run -p 8080:80 nginx

This command will launch a new container with the “nginx” image and map host port 8080 to container port 80.

Step 4: Test the connection

After mapping the ports, you can test the connection in a web browser by going to the Docker host’s IP address and port 8080. If all is in order, you will see the standard Nginx welcome page.

Step 5: Persist the port mapping

Docker containers are ephemeral by default, i.e., modifications made to them aren’t saved after the container is removed or stopped. 

For keeping the port mapping, you can use the “-P” flag rather than the “-p” flag in the “docker run” command. 

The “-P” flag causes all exposed ports to be mapped to random host ports.

docker run -P nginx

You can use the “docker port” command for viewing the port mapping:

docker port <container_name_or_id>

This command displays the host port mapping for all exposed container ports.

Step 6: Use Docker compose

Docker Compose is a tool used for creating and operating multi-container Docker applications. It enables you to declare the port mapping in a YAML file, which makes it easy to maintain and deploy apps. 

To utilize Docker Compose for port forwarding, you can create the ports section in your Docker Compose file:



    image: nginx


      – “8080:80”

In this example, the host port 8080 is mapped to container port 80 for the “web” service.

Can you bypass CGNAT and open ports?

Carrier Grade Network Address Translation (CGNAT) is a technology used by ISPs to preserve IP address space. CGNAT assigns a single public IP address to several devices, which may cause issues for users that need to access resources on their network from outside their network.

Still, you may simply overcome CGNAT and open ports on a router by using PureVPN’s port forwarding add-on. Install PureVPN to gain access to its outstanding port forwarding add-on, which can be used to open ports behind CGNAT. 

This enables you to mask IP addresses rapidly, avoid CGNAT-related issues, and port forward routers.

Host a server with a dedicated IP and port forwarding add on

If you are planning to host a game server, have your IP whitelisted, or have your friends connect to your devices at home, you’ll require a dedicated IP with a Port Forwarding add-on. 

A dedicated IP address enables you to set up servers and permit external IP addresses to connect to them without worrying about IP mapping.

Experience seamless surfing with the PureVPN port forwarding add-on

PureVPN offers an efficient Port Forwarding add-on in addition to its standard VPN services, which can improve your online browsing experience. 

Users can use the Port Forwarding add-on to unlock ports on their router or firewall, allowing direct connections between their device and internet resources such as streaming services or gaming websites.

This can help to reduce buffering, improve streaming quality, and create a more seamless surfing experience.

PureVPN’s Port Forwarding add-on works with a wide range of protocols and apps, including BitTorrent and FTP, etc. 

PureVPN provides a comprehensive solution for anyone looking to improve their online experience by combining the security benefits of a VPN with the advantages of Port Forwarding.

  1. Login to PureVPN in the member section.
  2. Open the subscription tab.
  3. Select Configure.
  4. Adjust the port settings as required.
  5. Click Apply settings to save the changes.

Final Words

Port forwarding is a crucial method for Docker that allows you to expose container ports to the outside world. It enables you to run apps in a separate environment while keeping them accessible. 

By following the steps mentioned in this guide, you can easily forward ports in Docker and enjoy the performance benefits.

Frequently Asked Questions

Can I use a specific IP address for port forwarding in Docker?

Yes, the IP address of the Docker host can be specified for port forwarding. You can use the “-p” flag with the IP address and port mapping separated by a colon to accomplish this.

Is port forwarding safe for Docker?

When you use Docker port forwarding, it exposes the container ports to the host, making them available from outside the container by default. Since anybody with an IP address and port number can access any service listening to the public, this can pose a security risk. 

You can limit this issue by using Docker’s built-in security capabilities, like blocking access to specified IP addresses or installing a firewall for blocking incoming traffic to undesired ports.



Marrium Akhtar


May 15, 2024


1 week ago

Marrium is a dedicated digital Marketer and an SEO enthusiast who is skilled in cracking SEO codes. Other than work, she loves to stream, eat, and repeat.

Have Your Say!!

Join 3 million+ users to embrace internet freedom

Signup for PureVPN to get complete online security and privacy with a hidden IP address and encrypted internet traffic.