Docker Port Forwarding

Edit

Author: Meet Rajesh Gor

#docker

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.

<a class='prev' href='/dockerize-django-prj'>

    <svg width="50px" height="50px" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M13.5 8.25L9.75 12L13.5 15.75" stroke="var(--prevnext-color-angle)" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"> </path>
    </svg>
    <div class='prevnext-text'>
        <p class='prevnext-subtitle'>prev</p>
        <p class='prevnext-title'>Dockerize a Django project</p>
    </div>
</a>

<a class='next' href='/libsql-query-remote-db'>

    <div class='prevnext-text'>
        <p class='prevnext-subtitle'>next</p>
        <p class='prevnext-title'>LibSQL: Query a remote Turso database with cURL</p>
    </div>
    <svg width="50px" height="50px" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M10.5 15.75L14.25 12L10.5 8.25" stroke="var(--prevnext-color-angle)" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
    </svg>
</a>