Mac M series chip (M1/M2) Docker installs Redis and configures Redis persistence

Docker install Redis

Install

Pull the image (take Redis6.2.12 as an example)

docker pull redis:6.2.12

View the image that has been pulled to the local

docker image ls

 Run the image (start the container)

docker run --name redis_container_name -d redis:6.2.12

test

into the container

docker exec -ti redis_container_name bash

 

Configure Redis persistence

Scenes

Use the Redis pulled by Docker to start the redis container from docker. By default, there is no configuration file

Persistence

  • RDB: It is saved in the form of snapshots according to a certain time interval, and can also be triggered actively
  • AOF: save by logging
  • The Redis persistence method currently has RDB and AOF, and the default is RDB. The specific storage/implementation/triggering mechanism, as well as the advantages and disadvantages of each, can be found on Baidu.

Prepare

  1. Go to the official website to download the redis.conf configuration file in Reids. In short, find a way to find a redis.conf configuration file
  2. Create folders /Users/leo/docker/redis and /Users/leo/docker/redis/data to store data (can be other locations, /Users/leo is the user directory on the author's Mac, which is equivalent to the ~ directory, reader Adjust the directory yourself)
  3. Put redis.conf in the /Users/leo/docker/redis directory

Configuration file modification

Comment out bind, otherwise you can only install Redis for local access

#bind 127.0.0.1

Enable AOF persistence

appendonly yes

write mechanism

#每次有修改就立刻写入
# appendfsync always
#每秒钟写入一次(推荐该方式)
appendfsync everysec
#让Redis自动判断
# appendfsync no

Save the file name, the default is fine

appendfilename "appendonly.aof"

where to save the data

#当前目录,可选择其他目录
dir ./

Configure connection password

requirepass pwd123456

run container

docker run --restart=always -p 6379:6379 --name redis \
> -v /Users/leo/docker/redis/redis.conf:/etc/redis/redis.conf \
> -v /Users/leo/docker/redis/data:/data \  
> -d redis:6.2.12 \
> redis-server /etc/redis/redis.conf --appendonly yes

Parameter Description:

  • docker run: run the Docker image
  • --restart=always: When docker restarts, the container will also restart
  • -p: mapping port number, the host port is mapped to the internal port of the container
  • --appendonly yes: persistence
  • –name redis: set the container name to redis
  • -v /Users/leo/docker/redis/redis.conf:/etc/redis/redis.conf: Map the redis.conf file in the host to docker
  • -v /Users/leo/docker/redis/data:/data: Mount the container's /data directory to the host's /Users/leo/docker/redis/data directory (data persistence)
  • -d redis:6.2.12: Select the running Docker image and specify the Tag (if not specified, the default is latest)
  • redis-server /etc/redis/redis.conf: start by loading the configuration file

test

After running, restart the Redis service, and the saved files will be generated in the /Users/leo/docker/redis/data folder

 reference article

1.  Docker configures Redis persistence

2. DockerHub-Redis

Guess you like

Origin blog.csdn.net/Tan_LC/article/details/130637670