Install Redis image in Docker container in Linux (CentOS) environment

Install Redis image in Docker container in Linux (CentOS) environment

1. Download the Redis image

docker pull redis     #如果使用docker pull redis命令,就会下载最新的redis镜像

docker pull redis:5.0.5  #下载Redis 5.0.5版本镜像

2. Create an instance and start Redis

mkdir -p /mydata/redis/conf
touch /mydata/redis/conf/redis.conf  

docker run -p 6378: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 explanation:

mkdir -p /mydata/redis/conf          #创建目录
touch /mydata/redis/conf/redis.conf  #创建文件(为什么要创建这个文件呢?因为我们在把容器内部的文件挂载到主机的时候
会把他当做目录去挂载,所以提前创建好这个文件就不会挂载成目录了)

docker run -p 6378:6379 --name redis \  #把Docker中的redis的6379端口映射到主机的6378端口
(因为我主机已经安装了redis,6379端口被占用了) --name redis 是把redis容器起名叫redis
-v /mydata/redis/data:/data \           #将redis容器内部的data文件夹挂载到主机
-v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf \  #将redis容器内部的redis.conf文件挂载到主机
-d redis redis-server /etc/redis/redis.conf  #开启Redis的后台运行

The instance is successfully created:

[root@Silence /]# mkdir -p /mydata/redis/conf
[root@Silence /]# touch /mydata/redis/conf/redis.conf
[root@Silence /]# docker run -p 6378: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
32142759198fa843b8c98915d48c19acd4f1d1d4e4c179b375b5ac4432d73d3f
[root@Silence /]# 
#将容器的端口映射到主机端口
docker run  -p  ip:hostPort:containerPort  redis  

View the running container in Docker:

[root@Silence /]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
81eb02763fc4        redis               "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        0.0.0.0:6378->6379/tcp              nostalgic_johnson
743df4fba2e9        mysql:5.7           "docker-entrypoint.s…"   3 hours ago         Up 34 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

You can see that the redis we just installed is up and running.

3. Open the redis client to connect to redis

docker exec -it redis redis-cli

Test redis:

[root@Silence /]# docker exec -it redis redis-cli
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> set name docker
OK
127.0.0.1:6379> get name
"docker"
127.0.0.1:6379> exit
[root@Silence /]# docker restart redis
redis
[root@Silence /]# docker exec -it redis redis-cli
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> 

After testing, we found that the data we inserted cannot be queried after redis restarts, which means that redis does not do persistence operations, so we need to configure redis for persistence operations.

vi /mydata/redis/conf/redis.conf  #编辑redis.conf文件

#在文件中添加appendonly yes      #打开AOF持久化模式
保存并退出

docker restart redis  #重启redis

After changing the configuration file, let's test it again:

[root@Silence conf]# docker exec -it redis redis-cli
127.0.0.1:6379> set name docker
OK
127.0.0.1:6379> get name
"docker"
127.0.0.1:6379> exit
[root@Silence conf]# docker restart redis
redis
[root@Silence conf]# docker exec -it redis redis-cli
127.0.0.1:6379> get name
"docker"
127.0.0.1:6379> 

After restarting again, we found that Redis already has a persistent operation, and a sudden redis crash will not cause data loss.

4. Use a visualization tool to connect to Redis remotely

If you are using Alibaba Cloud server, you need to open port 6379 in the security group. For details, please refer to the problem solving part of the article " Installing Mysql Mirror in a Docker Container in Linux (CentOS) Environment ".

I use the redis-desktop-manager visualization tool to connect to Redis remotely
Insert picture description here

connection succeeded! At this point, Redis can be used normally.

Guess you like

Origin blog.csdn.net/nxw_tsp/article/details/108042758