foreword
This tutorial demonstrates how to install a Redis image, create a Redis container and map ports to mount data volumes and configuration data.
environment
- CentOS 7
- Docker 20.10.10
Install
pull image
docker pull redis
View mirror
docker images
Create and start a Redis container
Create data directory and configuration file
Create configuration folder
mkdir -p /mydata/redis/conf
Create configuration file
touch /mydata/redis/conf/redis.conf
Reminder to avoid pits
The configuration file is created in advance redis.conf
, because when the machine is /mydata/redis/conf/redis.conf
mounted /etc/redis/redis.conf
, the last of the path will not be redis.conf
regarded as a file, but as a directory, so we want to redis.conf
mount the configuration file on the machine into the Docker container, A configuration file needs to be created in advance.
#######################################
Complete the above steps to create a data directory and configuration file~
#######################################
Create and start MySQL container command
sudo docker run -p 6379:6379 --name redis \
-v /mydata/redis/data:/data \
-v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf
Parameter Description
-p 3306:3306
: map port 3306 of the container to port 3306 of the host--name redis
: Define the container name as redis-v /mydata/redis/data:/data
: Mount the data folder of Redis to the host-v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf
: Mount the Redis configuration folder to the host-d redis redis-server /etc/redis/redis.conf
: run in the background, start with the redis image according to the configuration file /etc/redis/redis.conf
View running containers
docker ps
Redis connected to Docker
docker exec -it redis redis-cli
Stored value
set name zhangsan
value
get name
Set up Redis persistent storage
By default, the data of redis is stored in memory, and the data will be lost after restarting. After setting persistent storage, the restarted data will still be stored in the memory.
echo "appendonly yes" >> /mydata/redis/conf/redis.conf