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.
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.
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:
-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,
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: