Docker Port Forwarding


Docker Port Forwarding

Port forwarding is a process to redirect the communication of one address to other. It is also known as Port Binding. We can use -p command to use port forwarding in our local Docker environment.

docker run -p 8000:8000 django-app

The first port number is the local machine port and followed by a : is the container port number. SO, the request from the container port are forwarded to the local/outside world in the docker environment.

Additionally, we need to expose the container port first. We can do that in the Dockerfile or by adding a -e argument followed by the port to expose. This will open the port on container to forward the requests to the specified port in the -p option.

In the Dockerfile, we can expose the port by adding the command EXPOSE 8000 , or any other port number.