1. Preparation
1.1 Install Docker
Make sure Docker is installed. If not installed, please refer to the following command:
Ubuntu/Debian:
sudo apt-get update sudo apt-get install
CentOS/Fedora:
sudo yum install docker
macOS/Windows:
Download and installDocker Desktop。
After the installation is complete, start the Docker service:
sudo systemctl start docker sudo systemctl enable docker
2. Create Redis Directory and Configuration Files
2.1 Create a directory
exist/home/middleware/redis
Created belowdata
Directory is used to store Redis data and createConfiguration file:
mkdir -p /home/middleware/redis/data touch /home/middleware/redis/
2.2 Edit configuration files
editFile, add the following content:
bind 0.0.0.0 port 6379 dir /data dbfilename # Set Redis Passwordrequirepass yourpassword # RDB persistent configuration # Save triggers if at least 1 key is modified within 900 seconds (15 minutes)save 900 1 # Save triggers when at least 10 keys are modified within 300 seconds (5 minutes)save 300 10 # Save triggers when at least 10,000 keys are modified within 60 seconds (1 minute)save 60 10000
-
bind 0.0.0.0
: Allow all IP connections. -
port 6379
: Redis service port. -
dir /data
: Data storage directory. -
dbfilename
: Data file name. -
requirepass yourpassword
: Set Redis password (replaceyourpassword
Replace with your password). -
save
: Configure RDB persistence rules (see explanation below for details).
2.3 Save instruction detailed explanation
save
Directives are used to configure Redis's RDB persistence policy. Its syntax is as follows:
save <seconds> <changes>
-
<seconds>
: Time interval (seconds). -
<changes>
: If the data occurs at least<changes>
After modification, save will be triggered.
Example explanation
In the configuration file:
save 900 1 save 300 10 save 60 10000
-
save 900 1
- if900 seconds (15 minutes)Inside, at least1 keyIf modified, save will be triggered.
-
save 300 10
- if300 seconds (5 minutes)Inside, at least10 keysIf modified, save will be triggered.
-
save 60 10000
- if60 seconds (1 minute)Inside, at least10000 keysIf modified, save will be triggered.
Why do you need multiplesave
rule?
Multiplesave
Rules can be balancedData securityandperformance:
-
Save frequently(like
save 60 10000
): Ensure that the risk of data loss is minimized, but may affect performance, as frequent disk writes increase I/O load. -
Less saving(like
save 900 1
): Reduce disk I/O, but may increase the risk of data loss.
By combining multiple rules, Redis can flexibly trigger data storage in different scenarios.
3. Run Redis with Docker
3.1 Pull Redis Image
Pull the official Redis image from Docker Hub:
docker pull redis
3.2 Start the Redis container
Run the following command to start the Redis container and mount the configuration file and data directory:
docker run --name redis -d \ -p 6379:6379 \ -v /home/middleware/redis/data:/data \ -v /home/middleware/redis/:/usr/local/etc/redis/ \ redis redis-server /usr/local/etc/redis/
-
--name redis
: The container is namedredis
。 -
-d
: Run containers in the background. -
-p 6379:6379
: Map the host's port 6379 to the container's port 6379. -
-v /home/middleware/redis/data:/data
: Mount the data directory. -
-v /home/middleware/redis/:/usr/local/etc/redis/
: Mount the configuration file. -
redis redis-server /usr/local/etc/redis/
: Start Redis with a custom configuration file.
4. Verify Redis deployment
4.1 Check container status
Run the following command to see if the container is running normally:
docker ps
4.2 Connecting Redis
useredis-cli
Connect to Redis and verify password:
docker exec -it redis redis-cli
Execute the following command in the Redis CLI to authenticate:
AUTH yourpassword
After the authentication is successful, you can execute Redis commands, such as:
SET mykey "Hello, Redis!" GET mykey
4.3 Verify data persistence
Stop and restart the Redis container:
docker stop redis docker start redis
Connect Redis again to check if the data still exists:
docker exec -it redis redis-cli AUTH yourpassword GET mykey
5. Use Docker Compose (optional)
If you prefer to use Docker Compose to manage Redis containers, you can createdocument:
version: '3' services: redis: image: redis container_name: redis ports: - "6379:6379" volumes: - /home/middleware/redis/data:/data - /home/middleware/redis/:/usr/local/etc/redis/ command: redis-server /usr/local/etc/redis/
Then run the following command to start the service:
docker-compose up -d
Stop service:
docker-compose down
6. Directory structure
The final/home/middleware/redis
The directory structure is as follows:
/home/middleware/redis/ ├── data/ # Redis Persistence Data Directory│ └── # Redis Data File└── # Redis configuration file
7. Summary
Through the above steps, you have successfully deployed Redis using Docker and passedThe configuration file implements data persistence and password protection. This article also explains in detail
save
The role and configuration methods of instructions help you flexibly adjust your persistence strategy according to business needs.
The above is the detailed steps for using Docker to deploy Redis and configure persistence and password protection. For more information about Docker deploying Redis persistence and password protection, please follow my other related articles!