CodeGym /Courses /Docker SELF /Port Forwarding

Port Forwarding

Docker SELF
Level 12 , Lesson 1
Available

7.1 Basics of Port Forwarding

Port forwarding is an important Docker feature that allows applications inside containers to be accessible from the outside. This feature is needed to connect to web servers, databases, or other services running in containers from the host machine or even other networks.

By default, containers operate in an isolated network. To make a service in a container accessible, Docker forwards the container's ports to the host machine's ports.

The -p or --publish parameter of the docker run command is used for port forwarding.

Syntax


docker run -p [HOST_PORT]:[CONTAINER_PORT] [OPTIONS] IMAGE [COMMAND] [ARG...]

Where:

  • HOST_PORT: the port on the host machine through which traffic will be routed.
  • CONTAINER_PORT: the port inside the container where the traffic will be forwarded.
  • OPTIONS: additional parameters for configuring the container.
  • IMAGE: the image from which the container is created.
  • COMMAND: the command executed inside the container.
  • ARG...: arguments for the command.

Example of basic usage

In this example, port 80 of a container running an Nginx web server is forwarded to port 8080 of the host machine. After this, you can open the web server in a browser at http://localhost:8080.


docker run -d -p 8080:80 nginx

7.2 Other Port Forwarding Options

1. Multiple Port Forwarding

Docker lets you forward multiple ports at the same time. You just need to use several -p options.

Example

In this example, port 80 of the container is forwarded to port 8080 of the host, and port 443 of the container is forwarded to port 8443 of the host.



docker run -d -p 8080:80 -p 8443:443 nginx

2. Port Forwarding with a Specific IP Address

You can specify a certain IP address to which the forwarded port will be bound. This is super handy if the host machine has multiple network interfaces and you need to restrict access to the container.

Example:

In this example, port 80 of the container is forwarded to port 8080 only on the 127.0.0.1 interface of the host machine. This means the service will only be accessible from the host machine itself.


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

3. Range of Ports Forwarding

If you need to forward a bunch of ports at once, Docker supports working with ranges.

Example:

In this example, ports from 7000 to 8000 of the container are forwarded to ports from 7000 to 8000 of the host machine.


docker run -d -p 7000-8000:7000-8000 someimage

7.3 Practical Use Scenarios

1. Access to a Web Server

Port forwarding is most often used to provide access to web servers running inside containers.


docker run -d -p 8080:80 nginx 

After running this command, the Nginx web server running on port 80 of the container will become available on port 8080 of the host machine.

2. Access to a Database

Port forwarding is also handy for accessing databases running in containers.


docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword postgres

In this example, PostgreSQL running on port 5432 of the container will be accessible on port 5432 of the host machine.

3. Testing and Development

Docker containers are often used to create isolated testing environments. Port forwarding allows developers to run and test applications as if they were operating in real-world conditions.


docker run -d -p 8080:80 -p 8443:443 myapp

In this example, the application is being tested on ports 8080 and 8443, which correspond to the standard ports used in a production environment.

7.4 Additional Recommendations

1. Port Protection

When forwarding ports, don't forget about security. Open ports can become a target for attacks. Forward only those ports that you really need, and use a firewall to limit access.

2. Firewalls and NAT

To enhance security and manage traffic, use firewalls and Network Address Translation (NAT). This will help restrict access to your services and control network connections.

3. Monitoring and Logs

Set up monitoring and log collection to track traffic on forwarded ports. This will help you quickly notice and respond to suspicious activity.

3
Task
Docker SELF, level 12, lesson 1
Locked
Redirecting a port
Redirecting a port
3
Task
Docker SELF, level 12, lesson 1
Locked
Multiple port forwarding
Multiple port forwarding
3
Task
Docker SELF, level 12, lesson 1
Locked
Port redirection with specified IP address
Port redirection with specified IP address
3
Task
Docker SELF, level 12, lesson 1
Locked
Port Range Forwarding
Port Range Forwarding
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION