Docker安装运行Redis

Mac环境

查询镜像:

zhoumatoMBP:~ zhou$ docker search redis

NAME                                 DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis                                Redis is an open source key-value store that…   5578                [OK]                
bitnami/redis                        Bitnami Redis Docker Image                      87                                      [OK]
sameersbn/redis                                                                      71                                      [OK]
hypriot/rpi-redis                    Raspberry Pi compatible redis image             31                                      
kubeguide/redis-master               redis-master with "Hello World!"                25                                      

拉取官方的镜像,(这个示例中,官方镜像也是stars最多的):

zhoumatoMBP:~ zhou$ docker pull redis

Using default tag: latest
latest: Pulling from library/redis
be8881be8156: Pull complete 
d6f5ea773ca3: Pull complete 
735cc65c0db4: Pull complete 
787dddf99946: Pull complete 
0733799a7c0a: Pull complete 
6d250f04811a: Pull complete 
Digest: sha256:858b1677143e9f8455821881115e276f6177221de1c663d0abef9b2fda02d065
Status: Downloaded newer image for redis:latest
zhoumatoMBP:~ zhou$ /Users/zhou/Library/Containers/com.docker.docker/Data/
-bash: /Users/zhou/Library/Containers/com.docker.docker/Data/: is a directory
zhoumatoMBP:~ zhou$ ls /Users/zhou/Library/Containers/com.docker.docker/Data/
backend.sock        tasks            vpnkit.pcap.sock
docker.sock        vms            vpnkit.port.sock
osxfs.sock        vpnkit.diag.sock
task.lock        vpnkit.eth.sock

查看一下是否成功:

zhoumatoMBP:~ zhou$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              4e8db158f18d        8 days ago          83.4MB
zhoumatoMBP:~ zhou$ docker images redis
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              4e8db158f18d        8 days ago          83.4MB

启动镜像:

zhoumatoMacBook-Pro:~ zhou$ docker run -p 6379:6379 -d redis:latest redis-server
30e18792709c1e70ec93007731c80062e603ad01e06795d23d4bc6d4c8cdf3ea

 

ps:

菜鸟教程:
docker run -p 6379:6379 -v $PWD/data:/data  -d redis:3.2 redis-server --appendonly yes

命令说明:
-p 6379:6379 : 将容器的6379端口映射到主机的6379端口
-v $PWD/data:/data : 将主机中当前目录下的data挂载到容器的/data
redis-server --appendonly yes : 在容器执行redis-server启动命令,并打开redis持久化配置

查看容器启动情况:

zhoumatoMBP:~ zhou$ docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d0b86da2f6b9        redis               "docker-entrypoint.s…"   8 minutes ago       Up 8 minutes        0.0.0.0:6379->6379/tcp   thirsty_lichterman

连接redis的几种方式:

docker exec -ti d0b86 redis-cli

docker exec -ti d0b86 redis-cli -h localhost -p 6379 
docker exec
-ti d0b86 redis-cli -h 127.0.0.1 -p 6379
docker exec
-ti d0b86 redis-cli -h 172.17.0.3 -p 6379

// 注意,这个是容器运行的ip,可通过 docker inspect redis_s | grep IPAddress 查看

 

查看容器的ip:

zhoumatoMacBook-Pro:~ zhou$ docker inspect redis_s | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",

 

 

======================================================

使用redis镜像执行redis-cli命令连接到刚启动的容器,主机IP为172.17.0.1

zhoumatoMacBook-Pro:~ zhou$ docker exec -it redis_s redis-cli
127.0.0.1:6379> 

如果连接远程:

zhoumatoMacBook-Pro:~ zhou$ docker exec -it redis_s redis-cli -h 192.168.1.100 -p 6379 -a your_password //如果有密码 使用 -a参数
192.168.1.100:6379> 

 

redis操作命令:

redis官网:https://redis.io/commands

博客参考:https://www.cnblogs.com/Anidot/articles/7850716.html

 

posted @ 2018-08-16 09:33  静静别跑  阅读(81183)  评论(1编辑  收藏