others-how to start multiple redis servers in one machine using docker ?

Purpose

How to start multiple redis server in one machine ? This article would demo how to start two redis server in a Macbook pro.

Environment

  • MacOS

Solution

Step 1: Install docker

You can follow this document to install docker on MacBook.

Step 2: Start redis server1

Now let’s start redis server 1

docker run --name my-redis -p 6379:6379 --restart always --detach redis

Let’s check the command’s options:

Option 1: -p
-P         : Publish all exposed ports to the host interfaces
-p=[]      : Publish a container's port or a range of ports to the host
               format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
               Both hostPort and containerPort can be specified as a
               range of ports. When specifying ranges for both, the
               number of container ports in the range must match the
               number of host ports in the range, for example:
                   -p 1234-1236:1234-1236/tcp
Option 2: –restart

Using the --restart flag on Docker run you can specify a restart policy for how a container should or should not be restarted on exit.

always Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.
   
Option 3: –detach

When starting a Docker container, you must first decide if you want to run the container in the background in a “detached” mode or in the default foreground mode:

-d=false: Detached mode: Run container in the background, print new container id

To start a container in detached mode, you use -d=true or just -d option. By design, containers started in detached mode exit when the root process used to run the container exits,

Now let’s check the running redis:

➜  main git:(master) docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
61c10b3a6470        redis               "docker-entrypoint.s…"   13 seconds ago      Up 11 seconds       0.0.0.0:6379->6379/tcp   my-redis
➜  main git:(master) lsof -i:6379
COMMAND     PID       USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
com.docke 26624 bswen   22u  IPv4 0x444a1d7b8d308949      0t0  TCP *:6379 (LISTEN)
com.docke 26624 bswen   23u  IPv6 0x444a1d7b79382209      0t0  TCP localhost:6379 (LISTEN)
➜  main git:(master)

We can see that the redis server in docker is running.

Step 3: start redis server 2

docker run --name my-redis2 -p 26379:6379 --restart always --detach redis

You can see that we only changed the hostport to 26379 and the name of the docker instance to my-redis2.

Let’s verify it:

➜  main git:(master) docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
cb1e517e0a0f        redis               "docker-entrypoint.s…"   3 seconds ago       Up 2 seconds        0.0.0.0:26379->6379/tcp   my-redis2
61c10b3a6470        redis               "docker-entrypoint.s…"   43 seconds ago      Up 41 seconds       0.0.0.0:6379->6379/tcp    my-redis

It works!