DEV Community

Docker container with fixed IP address

When starting a YugabyteDB cluster, you provide network and host names, but the nodes identify themselves by their IP addresses. In a Docker lab, if you stop and restart them, the assigned IP address may change.

It is recommended to create a Docker network with an IP range using the --subnet flag and start containers with a static IP address using the --ip flag.

Here's an example of how to start a 3-node cluster:



docker network create --subnet=172.16.1.0/16 yb_lab &&
docker run -d --network yb_lab --ip 172.16.1.1 --hostname yb1 \
 --name yb1 -p 15433:15433 yugabytedb/yugabyte \
 yugabyted start --background=false &&
docker exec yb1 bash -c '
 until postgres/bin/pg_isready -h $(hostname) ; do sleep 1 ; done | 
 uniq && yugabyted status
' &&
docker run -d --network yb_lab --ip 172.16.1.2 --hostname yb2 \
 --name yb2 yugabytedb/yugabyte \
 yugabyted start --background=false --join=yb1 &&
docker exec yb2 bash -c '
 until postgres/bin/pg_isready -h $(hostname) ; do sleep 1 ; done | 
 uniq && yugabyted status
' &&
docker run -d --network yb_lab --ip 172.16.1.3 --hostname yb3 \
 --name yb3 yugabytedb/yugabyte \
 yugabyted start --background=false --join=yb1 &&
docker exec yb3 bash -c '
 until postgres/bin/pg_isready -h $(hostname) ; do sleep 1 ; done | 
 uniq && yugabyted status
'


Enter fullscreen mode Exit fullscreen mode

Output:
Image description

The example above has exported the port 15433 to show the console on http://localhost:15433
Image description

More nodes can be added with a similar yugabyted start --join= command.

If you want to delete this network along with all its containers, follow the steps below:




docker rm -f $(docker ps --filter "network=yb_lab" -qa)
docker network rm yb_lab



Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)