Skip to content

How to deploy or setup multiple redis servers in one machine using docker ?

Purpose

This post will show you how to deploy and setup multiple redis servers in one machine. Just as the following picture shows, I will install two redis servers on my server.

diagram shows how to deploy multiple redis servers in one machine

Environment

  • Linux or MacOS

Solution

Step 1: Install docker

You can follow this guide to install docker on your server.

Step 2: Start redis server1

Now let’s start redis server 1

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

Let’s check the command’s options:

Option 1: use -p to map docker port to host port

-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

So for redis1 server, I am listening port 6379 on my host and it is mapped to docker port 6379.

Option 2: use --restart to make it always running

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.

alwaysAlways 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: use --detach or -d to run in background

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:

Terminal window
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 redis1 server in docker is running, it’s listenning on port 6379.

Step 3: start redis server 2

Now let’s deploy redis server 2 named my-redis2, I will change the listenning port to 26379, but the docker port is still 6379.

Terminal window
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!

Final Words + More Resources

My intention with this article was to help others who might be considering deploying their own redis. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: [email protected].

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

⭐️ Thanks For Your Support 🙏