Docker install and configure Redis

foreword

This tutorial demonstrates how to install a Redis image, create a Redis container and map ports to mount data volumes and configuration data.
insert image description here

environment

  • CentOS 7
  • Docker 20.10.10

Install

pull image

docker pull redis

insert image description here
View mirror

docker images

insert image description here

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.confmounted /etc/redis/redis.conf, the last of the path will not be redis.confregarded as a file, but as a directory, so we want to redis.confmount 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
    insert image description here

View running containers

docker ps

insert image description here

Redis connected to Docker

docker exec -it redis redis-cli

insert image description here
Stored value

set name zhangsan

insert image description here
value

get name

insert image description here

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

Guess you like

Origin blog.csdn.net/qq_31762741/article/details/121468731