Docker quickly install and configure Redis

Preface

The advantage of docker containerized installation is that it can be installed quickly with a few commands, and multiple Redis can be run on the same linux at the same time without affecting each other. If the configuration is broken, easily delete the container and run the container again.

Detailed explanation of the basic operations of installing Docker and Docker: https://blog.csdn.net/qq_43290318/article/details/107743188

1. Pull the latest image

Central warehouse: https://hub.docker.com/

The version number of the latest mirror I pulled is: 6.0.9

# 拉取最新的镜像
docker pull redis

# 查看所有镜像
docker images

# 删除某个镜像。其中 -f 表示强制删除
docker rmi -f 镜像名/id

4. Go to the official website to download the corresponding version of the configuration file

Redis official website: https://redis.io/

Since the configuration file redis.conf was not found in the Redis container run by Docker, we need to download the corresponding version of the Redis compressed package from the official website, and then upload the redis.conf to our Linux.

3. Run Redis container

Full command:

docker run --privileged=true -v /data/redis/redis.conf:/usr/local/bin/redis.conf -v /data/redis/data:/data -p 6379:6379 --name redis01 -d redis redis-server /usr/local/bin/redis.conf

Parameter analysis:

1. --privileged=true: With this parameter added, the root in the container has real root privileges. Plus here, the following needs to be tested. Generally I also recommend adding

2. -p 6379:6379: port mapping between the host and the container, here are the default listening ports of Redis directly

3. --name redis01: Name the container redis01

4. -d: Run the container in the background and return the full id of the container (ie start the daemon container)

Since it starts a log process after running the Redis container, the -d here does not need to be used with -it. If you want to understand clearly, please refer to the blog in the preface.

5、-v /data/redis/redis.conf:/usr/local/bin/redis.conf -v /data/redis/data:/data

Mount the Redis configuration file and data directory. The left side of the colon is the host directory, and the right side of the colon is the internal path of the container!

(1) If we do not mount the configuration file, we need to modify the configuration of the Redis container. We need to copy the configuration file from the host to the Redis container, and then enter the container to modify the configuration file. There is no text editor like vim in the Redis container. , Although it can be installed but a bit troublesome.

(2) How do we know where the configuration files and core files in the Redis container are located? So, we can docker run a Redis container at random, and then enter it to have a look and understand the approximate directory structure. Under /usr/local/bin inside the container, we saw the familiar redis-cli and redis-server, but we didn't find the configuration file redis.conf (the picture below was only available after I mounted it, not originally).

To verify that there is indeed no redis.conf in the container. We can perform a full search by command:

# 进入redis03容器内部,并返回一个终端
# 它与 docker attach 有什么区别,请参看 前言 中的博客
docker exec -it redis03 /bin/bash

# 全盘搜索redis.conf
find / -name 'redis.conf'

 Note that if the --privileged=true parameter is not added when running the container at the beginning, some directories will be denied access when searching because of insufficient permissions.

View the logs of the container redis01: docker logs redis01

4. Modify the redis.conf in the host

Detailed configuration file: https://www.cnblogs.com/metu/p/9609604.html

Here we only need to modify the following parameters:

bind 127.0.0.1 # Comment out this part, this is to restrict redis to only local access

protected-mode no # Default yes, opening protected mode will restrict local access

port 6379 # The port number that redis listens to

daemonize no # Default no, changing to yes means to start as a daemon process, which can run in the background, unless the process is killed, changing to yes will make the configuration file mode start redis fail

databases 16 # The number of databases (optional), you can change it to see if it takes effect

appendonly yes # redis persistence (optional)

requirepass *** # The password that needs to be entered when connecting

5. Restart the container for the configuration to take effect

Modify and save the redis.conf in the host. Let's verify that the redis.conf inside the running container has also been modified at this time?

# 进入容器
docker exec -it redis01 /bin/bash

# 查看配置文件的密码是否已修改
cat /usr/lcoal/bin/redis.conf | grep 'requirepass'

You should see that the password we changed has not taken effect. At this point we need to exit the container and restart the container

docker restart redis01

At this point, we went to check the configuration file in the container and found that it took effect.

6. Use RedisDesktopManager to connect remotely

RedisDesktopManager is now charged. I found a modified version of RedisDesktopManager on the Internet, and the experience is better. https://github.com/qishibo/AnotherRedisDesktopManager/releases

Note that if you are connecting to Redis on the cloud server, don't forget to open the Redis listening port in the security group.

 

Guess you like

Origin blog.csdn.net/qq_43290318/article/details/109478032