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
.
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
Let’s check the command’s options:
Option 1: use -p
to map docker port to host port
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.
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: 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:
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:
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
.
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:
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!