Port Forwarding in Docker
Port forwarding or port mapping redirects a communication request from one IP address and port number combination to another. Through port forwarding, services are exposed to the applications residing outside of the host's internal network.
Here, port forwarding allows remote client applications to connect to the NCache servers running inside Docker containers (internal Docker network).
Configuration
There are some scenarios where you will not be able to access the NCache servers running inside Docker containers directly. Here you'll have to map NCache ports from the containers to the host machine network for accessibility. You'll also have to configure these ports for the client application. Configuration for both is explained below:
Configure Port Forwarding in Containers
Before you proceed with port forwarding, you need to assign NCache containers static IP addresses. NCache servers need to have static IP addresses to communicate with each other and with NCache clients. Also, NCache servers are not accessible to the NCache clients if both don't exist on the same network or subnet.
Step 1: Create a Custom Docker Network
By default, Docker assigns containers dynamic IP addresses. Meaning, if for some reason, the containers have to restart or the host machine restarts, the IP addresses of the containers might change. To assign containers static IP addresses, a custom Docker network is required.
The following command creates a custom Docker network named nbrg with a subnet and gateway defined:
docker network create --subnet=172.19.0.0/16 --gateway=172.19.0.1 nbrg
Step 2: Create NCache Containers and Map NCache Ports
Once the custom Docker network has been created, you can use it to assign NCache containers static IP addresses and map NCache ports from the containers to the host for accessibility.
The following commands create two NCache Docker containers named ncache-ent-server-01 and ncache-ent-server-02. The two containers are assigned static IP addresses using the custom Docker network nbrg created in the previous step. NCache communication ports are mapped from the containers to the host using the Docker -p switch:
docker run --name ncache-ent-server-01 --net nbrg --ip 172.19.0.11 -p 1250-1260:8250-8260 -p 1300-1400:8300-8400 -p 9801:9800 -itd alachisoft/ncache:enterprise-server-linux-latest
docker run --name ncache-ent-server-02 --net nbrg --ip 172.19.0.12 -p 2250-2260:8250-8260 -p 2300-2400:8300-8400 -p 9802:9800 -itd alachisoft/ncache:enterprise-server-linux-latest
Configure Port Forwarding for Client Application
Once you have mapped the ports from the containers to the host, you need to reflect that change for the client application as well. The client.ncconf file is used by the client application to establish a connection with the NCache servers. You need to map the ports and the IP addresses in this file.
The following is a client.ncconf file configuration where the ports are mapped with the containers (NCache servers) ncache-ent-server-01 and ncache-ent-server-02 which were created previously and are hosted on the public IP address 20.200.20.212
:
<configuration>
<ncache-server connection-retries="1" retry-connection-delay="0" retry-interval="1" command-retries="3" command-retry-interval="0.1" client-request-timeout="90" connection-timeout="5" port="9800"/>
<cache id="demoCache" client-cache-id="" client-cache-syncmode="optimistic" skip-client-cache-if-unavailable="True" reconnect-client-cache-interval="10" default-readthru-provider="" default-writethru-provider="" load-balance="True" enable-client-logs="False" log-level="error">
<server name="172.19.0.11"/>
<server name="172.19.0.12"/>
</cache>
<server-end-point>
<end-point public-ip="20.200.20.212" public-ports="9801" private-ip="172.19.0.11" private-ports="9800"/>
<end-point public-ip="20.200.20.212" public-ports="1250-1260" private-ip="172.19.0.11" private-ports="8250-8260"/>
<end-point public-ip="20.200.20.212" public-ports="1300-1400" private-ip="172.19.0.11" private-ports="8300-8400"/>
<end-point public-ip="20.200.20.212" public-ports="9802" private-ip="172.19.0.12" private-ports="9800"/>
<end-point public-ip="20.200.20.212" public-ports="2250-2260" private-ip="172.19.0.12" private-ports="8250-8260"/>
<end-point public-ip="20.200.20.212" public-ports="2300-2400" private-ip="172.19.0.12" private-ports="8300-8400"/>
</server-end-point>
</configuration>
See Also
NCache Docker Deployment Scenarios
NCache Docker on Windows
NCache Docker on Linux