docker运行redis
查询镜像:
[root@server02 ~]# docker search redis NAME DESCRIPTION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that… 8940 [OK] bitnami/redis Bitnami Redis Docker Image 169 [OK] sameersbn/redis 82 [OK] grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0 74 rediscommander/redis-commander Alpine image for redis-commander - Redis man… 49 [OK] kubeguide/redis-master redis-master with "Hello World!" 33 redislabs/redisearch Redis With the RedisSearch module pre-loaded… 30 redislabs/redis Clustered in-memory database engine compatib… 27 redislabs/rejson RedisJSON - Enhanced JSON data type processi… 23 oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Supp… 22 arm32v7/redis Redis is an open source key-value store that… 22 bitnami/redis-sentinel Bitnami Docker Image for Redis Sentinel 18 [OK] redislabs/redisinsight RedisInsight - The GUI for Redis 16 redislabs/redisgraph A graph database module for Redis 13 [OK] webhippie/redis Docker images for Redis 11 [OK] arm64v8/redis Redis is an open source key-value store that… 10 s7anley/redis-sentinel-docker Redis Sentinel 10 [OK] redislabs/redismod An automated build of redismod - latest Redi… 9 [OK] insready/redis-stat Docker image for the real-time Redis monitor… 9 [OK] circleci/redis CircleCI images for Redis 5 [OK] centos/redis-32-centos7 Redis in-memory data structure store, used a… 5 clearlinux/redis Redis key-value data structure server with t… 3 tiredofit/redis Redis Server w/ Zabbix monitoring and S6 Ove… 1 [OK] wodby/redis Redis container image with orchestration 1 [OK] xetamus/redis-resource forked redis-resource 0 [OK]
拉取官方的镜像,(这个示例中,官方镜像也是stars最多的):
[root@server02 ~]# docker pull redis Using default tag: latest latest: Pulling from library/redis 6ec7b7d162b2: Pull complete 1f81a70aa4c8: Pull complete 968aa38ff012: Pull complete 884c313d5b0b: Pull complete 6e858785fea5: Pull complete 78bcc34f027b: Pull complete Digest: sha256:0f724af268d0d3f5fb1d6b33fc22127ba5cbca2d58523b286ed3122db0dc5381 Status: Downloaded newer image for redis:latest
查看镜像
[root@server02 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest ef47f3b6dc11 3 weeks ago 104MB
[root@server02 ~]# docker run --name=redis_test -p 6379:6379 -v /data:/data -d redis:latest redis-server --appendonly yes --requirepass "gs123456"
6fb880e4763b9bea8d9b9902d2021c96e855fc1e519992768d5e56f275abfa0b
[root@server02 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6fb880e4763b redis:latest "docker-entrypoint.s…" 7 seconds ago Up 6 seconds 0.0.0.0:6379->6379/tcp redis_test
[root@server02 ~]#
命令说明:
-p 6379:6379 : 将容器的6379端口映射到主机的6379端口 -v $PWD/data:/data : 将主机中当前目录下的data挂载到容器的/data redis-server --appendonly yes : 在容器执行redis-server启动命令,并打开redis持久化配置 --requirepass:redis密码
连接redis的几种方式:
docker exec -ti redis_test redis-cli -h localhost -p 6379 docker exec -ti redis_test redis-cli -h 127.0.0.1 -p 6379 docker exec -ti redis_test redis-cli -h 172.17.0.3 -p 6379
查看容器的ip:
[root@server02 ~]# docker inspect redis_test | grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2",
使用redis镜像执行redis-cli命令连接到刚启动的容器,主机IP为172.17.0.1
[root@server02 ~]# docker exec -it redis_test redis-cli 127.0.0.1:6379>
如果连接远程:
[root@server02 ~]# docker exec -it redis_test redis-cli -h 192.168.0.221 -p 6379 -a gs123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.0.221:6379>