docker 安装并配置redis

1. 下载redis的镜像

[root@host-10-23-110-128 ~]# docker pull redis:latest
latest: Pulling from library/redis
a2abf6c4d29d: Pull complete
c7a4e4382001: Pull complete
4044b9ba67c9: Pull complete
c8388a79482f: Pull complete
413c8bb60be2: Pull complete
1abfd3011519: Pull complete
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
[root@host-10-23-110-128 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
redis        latest    7614ae9453d1   11 months ago   113MB
2.  配置

[root@host-10-23-110-128 docker]# cd data/
[root@host-10-23-110-128 data]# ls
[root@host-10-23-110-128 data]# mkdir redis
[root@host-10-23-110-128 data]# cd redis/
[root@host-10-23-110-128 redis]# ls
[root@host-10-23-110-128 redis]# wget http://download.redis.io/redis-stable/redis.conf
--2022-11-24 14:28:03--  http://download.redis.io/redis-stable/redis.conf
正在解析主机 download.redis.io (download.redis.io)... 45.60.125.1
正在连接 download.redis.io (download.redis.io)|45.60.125.1|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:106545 (104K) [application/octet-stream]
正在保存至: “redis.conf”

redis.conf             100%[=========================>] 104.05K  --.-KB/s  用时 0.1s    

2022-11-24 14:28:03 (831 KB/s) - 已保存 “redis.conf” [106545/106545])

[root@host-10-23-110-128 redis]# chmod 777 redis.conf
[root@host-10-23-110-128 redis]# nano redis.conf
bind 127.0.0.1 # 这行要注释掉,解除本地连接限制 protected-mode no # 默认yes,如果设置为yes,则只允许在本机的回环连接,其他机器无法连接。 daemonize no # 默认no 为不守护进程模式,docker部署不需要改为yes,docker run -d本身就是后台启动,不然会冲突 requirepass 123456 # 设置密码 appendonly yes # 持久化

3. 启动

[root@host-10-23-110-128 redis]#sudo docker run -p 6379:6379 --name redis -v /home/docker/data/redis.conf:/etc/redis/redis.conf  -v /home/docker/data/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes
6427f941cece1f3b93d575ac7845975c4c0c5c15f1b4f6e9cd12aa924981f64a
[root@host-10-23-110-128 redis]#

  • -p 6379:6379:端口映射,前面是宿主机,后面是容器。
  • –name redis:指定该容器名称。
  • -v 挂载文件或目录:前面是宿主机,后面是容器。
  • -d redis redis-server /etc/redis/redis.conf:表示后台启动redis,以配置文件启动redis,加载容器内的conf文件。
  • appendonly yes:开启redis 持久化。

[root@host-10-23-110-128 redis]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                    NAMES
a7aafa90cd99   redis     "docker-entrypoint.s…"   5 seconds ago   Up 4 seconds   0.0.0.0:6379->6379/tcp   redis
[root@host-10-23-110-128 redis]#

[root@host-10-23-110-128 docker]# netstat -antp | grep 6379
tcp6       0      0 :::6379                 :::*                    LISTEN      8539/docker-proxy   
[root@host-10-23-110-128 docker]# docker exec -it a7aafa90cd99 redis-cle
OCI runtime exec failed: exec failed: container_linux.go:370: starting container process caused: exec: "redis-cle": executable file not found in $PATH: unknown
[root@host-10-23-110-128 docker]# docker exec -it a7aafa90cd99 redis-cli
127.0.0.1:6379> keys
(error) ERR wrong number of arguments for 'keys' command
127.0.0.1:6379> set test "this is an test"
OK
127.0.0.1:6379> get test
"this is an test"
127.0.0.1:6379> exit

posted @ 2022-11-24 15:05  李悠然  阅读(484)  评论(0编辑  收藏  举报