在Windows中使用Docker學習Redis

測試環境

Wondows 10 專業版redis

Step 0

啓動Docker服務docker

Step 1

拉取 Redis 鏡像

$ 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
$ 
複製代碼

Step 2

啓動 redis 容器實例

$ docker run --name myredis -d -p6379:6379 redis  # 其中myredis是容器實例的名字
b41d7b7e794375e049af7091218e61fe252095563d83664b033081bab3570079
$ # 若是報相似以下錯誤:
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/myredis" is already in use by container "e879068da53b9c0c3bf7e604b9d0913ae79ac16c74b6056cb141d07e9e73485f". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
$ # 則須要先執行
$ docker container rm myredis  # 不必定是myredis,與上面執行run時指定的名字一致便可
myredis
$ # 而後再次執行run命令便可
複製代碼

查看啓動的容器實例

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
b41d7b7e7943        redis               "docker-entrypoint.s…"   15 seconds ago      Up 14 seconds       0.0.0.0:6379->6379/tcp   myredis
$ 
複製代碼

Step 3

執行容器中的 redis-cli 命令

$ docker exec -it myredis redis-cli  # 注意其中的myredis要與啓動時的名字一致
127.0.0.1:6379> 
複製代碼

Step 4

開始操做 redis 吧!

Step 5

退出 redis-cli

127.0.0.1:6379> quit
複製代碼

Step 6

中止 redis 容器

$ # 首先查看容器的ID
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
b41d7b7e7943        redis               "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes        0.0.0.0:6379->6379/tcp   myredis
$ # 可見ID爲:b41d7b7e7943,下面將其中止,能夠只輸入前幾位
$ docker stop b41d
b41d
$ # 驗證是否已中止
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
$ 
複製代碼

Have Fun!

相關文章
相關標籤/搜索