NCache Docker Port Forwarding
Docker port forwarding or port mapping redirects a communication request from one IP address and port number combination to another. Port forwarding exposes services to applications outside 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).
Docker Port Forwarding Configuration
There are some scenarios where you cannot access the NCache servers running inside Docker containers directly. You'll have to map NCache ports from the containers to the host machine network for accessibility. Additionally, you'll have to configure these ports for the client application. The configuration for both is explained below:
Configure Port Forwarding in Containers
Before you proceed with docker 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. Therefore, if 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 creation occurs, you may 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. Two containers are assigned static IP addresses using the custom Docker network nbrg creat in the last step. The NCache communication ports map 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:latest-java
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:latest-java
Configure Docker 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 uses 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 map 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