1 下载
下载 6.2.7 版本:
[root@service-monitoring ~]# docker pull redis:6.2.7
6.2.7: Pulling from library/redis
025c56f98b67: Pull complete
060e65aed679: Pull complete
b95291e865b7: Pull complete
e3023c0b11d1: Pull complete
143500497a02: Pull complete
c38298c98c43: Pull complete
Digest: sha256:a93c14584715ec5bd9d2648d58c3b27f89416242bee0bc9e5fb2edc1a4cbec1d
Status: Downloaded newer image for redis:6.2.7
docker.io/library/redis:6.2.7
[root@service-monitoring conf]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.42-oracle dd6675b5cfea 9 days ago 569MB
redis 6.2.7 4b1123a829a1 4 months ago 113MB
2 配置文件
为方便挂载,新建目录及文件:
[root@service-monitoring home]# mkdir redis6/conf -p
[root@service-monitoring home]# cd redis6/conf/
[root@service-monitoring conf]# touch redis.conf
[root@service-monitoring conf]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 27 13:24 redis.conf
3 启动
在 Docker 中启动一个 Redis 6.2.7 容器,并将容器内部的 Redis 服务端口映射到主机的 6379 端口,同时将 Redis 数据持久化到主机上,并使用自定义的 Redis 配置文件来启动 Redis 服务。
# 用于启动一个新的容器
# 将容器内部的 Redis 服务端口(默认为 6379)映射到主机的 6379 端口,以便可以通过主机的 IP 地址和端口来访问 Redis 服务。
# 为容器指定一个名称【redis】,方便后续管理和操作。
docker run -p 6379:6379 --name redis \
# 将主机上的 /home/redis6/data 目录挂载到容器内部的 /data 目录,以便可以将 Redis 数据持久化到主机
-v /home/redis6/data:/data \
# 将主机上的 /home/redis6/conf/redis.conf 文件挂载到容器内部的 /etc/redis/redis.conf 文件,以便可以使用自定义的 Redis 配置文件
-v /home/redis6/conf/redis.conf:/etc/redis/redis.conf \
# 指定要使用的 Redis 镜像及版本号,并在后台运行容器
-d redis:6.2.7 \
# 在容器内部执行的命令,启动 Redis 服务,并使用 /etc/redis/redis.conf 配置文件
redis-server /etc/redis/redis.conf
[root@service-monitoring conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5bce12836608 redis:6.2.7 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis
e380fdc23c78 mysql:5.7.42-oracle "docker-entrypoint.s…" 14 hours ago Up 14 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@service-monitoring conf]#
4 测试
[root@service-monitoring conf]# docker exec -it redis bash
root@5bce12836608:/data# ls /
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@5bce12836608:/data# redis-cli
127.0.0.1:6379> # Ctrl+D 退出
root@5bce12836608:/data# whereis redis-cli
redis-cli: /usr/local/bin/redis-cli
root@5bce12836608:/data# cd /usr/local/bin
root@5bce12836608:/usr/local/bin# ls -l
total 27484
-rwxrwxr-x 1 root root 661 Dec 6 06:51 docker-entrypoint.sh
-rwxr-xr-x 1 root root 2289664 Dec 7 2021 gosu
-rwxr-xr-x 1 root root 6787672 Dec 6 06:52 redis-benchmark
lrwxrwxrwx 1 root root 12 Dec 6 06:52 redis-check-aof -> redis-server
lrwxrwxrwx 1 root root 12 Dec 6 06:52 redis-check-rdb -> redis-server
-rwxr-xr-x 1 root root 6686032 Dec 6 06:52 redis-cli
lrwxrwxrwx 1 root root 12 Dec 6 06:52 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 12369368 Dec 6 06:52 redis-server
root@5bce12836608:/usr/local/bin# redis-cli
127.0.0.1:6379> set java 'Edge'
OK
127.0.0.1:6379> get java
"Edge"
127.0.0.1:6379> # Ctrl+D 退出
root@5bce12836608:/usr/local/bin#
5 客户端连接
使用 ARDM 连接访问,就看到刚才 set 进去的 java:
6 自动重启
随云服务重启,Redis 内部也要全部重启,如何设置呢?
root@5bce12836608:/usr/local/bin# Ctrl+D 退出容器内部的终端
exit
[root@service-m[root@service-monitoring redis6]# docker update mysql --restart=always
mysql
[root@service-monitoring redis6]# docker update redis --restart=always
redis
[root@service-monitoring redis6]# onitoring conf]#
[root@service-monitoring redis6]# docker update mysql --restart=always
mysql
[root@service-monitoring redis6]# docker update redis --restart=always
redis
[root@service-monitoring redis6]#
也可在 docker run 时加参数--restart=always。
7 配置文件
[root@service-monitoring conf]# vim redis.conf
[root@service-monitoring conf]# docker restart redis
redis
[root@service-monitoring conf]#
redis.conf空的,写入两条配置:
此时本地客户端果然就连接失败咯:
填充密码:
立马又正常咯:
8 历史更改
A = add
C = change
D=delete
[root@service-monitoring ~]# docker diff redis
C /root
A /root/.bash_history
A /root/.rediscli_history
C /etc
A /etc/redis
A /etc/redis/redis.conf
[root@service-monitoring ~]#
做了这么多更新,能否将当前快照记录下来呢?当然!
[root@service-monitoring ~]# docker commit -a JavaEdge -m "init new another redis" redis redis:myRedissha256:3c83dc8d500b7d76d681cf34be344694bc4ae914ac1f450a91f5c54384bb8ba0
[root@service-monitoring ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis myRedis 3c83dc8d500b 25 seconds ago 113MB
mysql 5.7.42-oracle dd6675b5cfea 9 days ago 569MB
redis 6.2.7 4b1123a829a1 4 months ago 113MB
hello-world latest feb5d9fea6a5 19 months ago 13.3kB
[root@service-monitoring ~]#
假如我现在重复执行一遍:
[root@service-monitoring ~]# docker commit -a JavaEdge -m "init new another redis" redis redis:myRedissha256:9a91ea9de0d3d62b382d85c16c80de591d5624c6710e74fec3464ebf97ff644f
[root@service-monitoring ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis myRedis 9a91ea9de0d3 17 seconds ago 113MB
<none> <none> 3c83dc8d500b 2 minutes ago 113MB
mysql 5.7.42-oracle dd6675b5cfea 9 days ago 569MB
redis 6.2.7 4b1123a829a1 4 months ago 113MB
hello-world latest feb5d9fea6a5 19 months ago 13.3kB
[root@service-monitoring ~]#
可见,产生了游离镜像,移除它们
[root@service-monitoring ~]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
deleted: sha256:3c83dc8d500b7d76d681cf34be344694bc4ae914ac1f450a91f5c54384bb8ba0
Total reclaimed space: 0B
[root@service-monitoring ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis myRedis 9a91ea9de0d3 2 minutes ago 113MB
mysql 5.7.42-oracle dd6675b5cfea 9 days ago 569MB
redis 6.2.7 4b1123a829a1 4 months ago 113MB
hello-world latest feb5d9fea6a5 19 months ago 13.3kB
[root@service-monitoring ~]#
9 tag更改
[root@service-monitoring ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis myRedis 9a91ea9de0d3 2 minutes ago 113MB
mysql 5.7.42-oracle dd6675b5cfea 9 days ago 569MB
redis 6.2.7 4b1123a829a1 4 months ago 113MB
hello-world latest feb5d9fea6a5 19 months ago 13.3kB
[root@service-monitoring ~]# docker tag redis:myRedis redis:myredis-1.0
[root@service-monitoring ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis myRedis 9a91ea9de0d3 4 minutes ago 113MB
redis myredis-1.0 9a91ea9de0d3 4 minutes ago 113MB
mysql 5.7.42-oracle dd6675b5cfea 9 days ago 569MB
redis 6.2.7 4b1123a829a1 4 months ago 113MB
hello-world latest feb5d9fea6a5 19 months ago 13.3kB
[root@service-monitoring ~]#
其实就相当于新增了一个引用,但具体对象所占内存不变。
docker run -p 8379:6379 --name redisABC \
-d redis:myredis-1.0
8f9bfe41f9fa5ebb8a40cba4c4532ca4e8cbb353e144c7a85d8fe4f4d75f12d7
[root@service-monitoring ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f9bfe41f9fa redis:myredis-1.0 "docker-entrypoint.s…" 24 seconds ago Up 23 seconds 0.0.0.0:8379->6379/tcp, :::8379->6379/tcp redisABC
5bce12836608 redis:6.2.7 "docker-entrypoint.s…" 5 hours ago Up 4 hours 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis
e380fdc23c78 mysql:5.7.42-oracle "docker-entrypoint.s…" 18 hours ago Up 18 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@service-monitoring ~]#
客户端连接也是完全没问题
注意到此时我们对复制出的镜像执行:
[root@service-monitoring ~]# docker diff redisABC
[root@service-monitoring ~]#
输出为空,因为我们本质上并没有创建新的 redis.conf 文件或者 etc 目录
10 共享镜像
10.1 export/import
[root@service-monitoring ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f9bfe41f9fa redis:myredis-1.0 "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:8379->6379/tcp, :::8379->6379/tcp redisABC
5bce12836608 redis:6.2.7 "docker-entrypoint.s…" 6 hours ago Up 6 hours 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis
e380fdc23c78 mysql:5.7.42-oracle "docker-entrypoint.s…" 20 hours ago Up 20 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@service-monitoring ~]# docker export -o myRedis.tar redisABC
[root@service-monitoring ~]# ll
total 111548
-rw-r--r-- 1 root root 1634304 Apr 2 16:28 loki-linux-amd64.zip
-rw------- 1 root root 112590336 Apr 27 20:08 myRedis.tar
-rw------- 1 root root 0 Apr 10 10:47 nohup.out
[root@service-monitoring ~]#
就可以将这个 tar 包通过 scp 传输到其他服务器上了。
[root@service-monitoring redis6]# scp /home/redis6/myRedis.tar root@目标机器ip:/home/redis6/
root@目标机器ip's password:
myRedis.tar 100% 107MB 2.4MB/s 00:45 B
[root@service-monitoring redis6]#
到目标机器看下,他来了他来了!
[root@icv-monitor-platform-dev redis6]# ll
total 109956
-rw------- 1 root root 112590336 Apr 27 20:20 myRedis.tar
[root@icv-monitor-platform-dev redis6]#
在目标机器可继续执行 docker import
[root@icv-monitor-platform-dev redis6]# docker import myRedis.tar redis:myRedis-another
sha256:33d956d31be6bd9434f80cd9ad52bbcf5ab9e068c30936810b09f1a06e815342
[root@icv-monitor-platform-dev redis6]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis myRedis-another 33d956d31be6 46 seconds ago 109MB
[root@service-monitoring redis6]#
想直接运行它?docker run 是无法启动的!必须使用源启动参数,这谁能天天记得启动脚本啊?要求也太高了吧!而且若源镜像已经被删了,目标镜像也无法启动!太恶心了,干脆忘了这导出导入的命令吧!
那怎么实现跨服务器呢?换个好用的命令实现!先删除该镜像:
[root@service-monitoring redis6]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis myRedis-another 33d956d31be6 46 seconds ago 109MB
redis myRedis 9a91ea9de0d3 2 hours ago 113MB
redis myredis-1.0 9a91ea9de0d3 2 hours ago 113MB
mysql 5.7.42-oracle dd6675b5cfea 9 days ago 569MB
redis 6.2.7 4b1123a829a1 4 months ago 113MB
hello-world latest feb5d9fea6a5 19 months ago 13.3kB
[root@service-monitoring redis6]# docker rmi 33d956d31be6
Untagged: redis:myRedis-another
Deleted: sha256:33d956d31be6bd9434f80cd9ad52bbcf5ab9e068c30936810b09f1a06e815342
Deleted: sha256:f961cdcaa8bba925086da29e930d27d98982775e56486ef1d7086f903c3934df
[root@service-monitoring redis6]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis myRedis 9a91ea9de0d3 2 hours ago 113MB
redis myredis-1.0 9a91ea9de0d3 2 hours ago 113MB
mysql 5.7.42-oracle dd6675b5cfea 9 days ago 569MB
redis 6.2.7 4b1123a829a1 4 months ago 113MB
hello-world latest feb5d9fea6a5 19 months ago 13.3kB
[root@service-monitoring redis6]# docker rmi feb5d9fea6a5
Error response from daemon: conflict: unable to delete feb5d9fea6a5 (must be forced) - image is being used by stopped container 60180275418c
这个错误表明删除镜像时存在容器仍在使用该镜像,需要先停止并删除这些容器才能成功删除对应的镜像。可按照以下步骤操作:
- 查看正在运行的容器和他们使用的镜像,docker ps,在输出信息中看到CONTAINER ID和IMAGE等字段。
- 停止容器,命令为
docker stop <container_id>
,其中<container_id>为具体的容器ID,如果有多个容器,需要逐一停止它们 - 删除容器,命令为
docker rm <container_id>
,其中<container_id>为具体的容器ID - 最后,尝试删除镜像
docker rmi <image_id>
如果不希望手动逐一停止和删除容器,可以使用docker stop $(docker ps -a -q)
和docker rm $(docker ps -a -q)
来批量停止和删除所有容器。如果确定要强制删除被容器引用的镜像,可以加上-f
选项,命令为docker rmi -f <image_id>
。
10.2 save/load 独立网络环境使用
# 注意这里没有指定 tag,会将所有 redis 迁移
[root@service-monitoring redis6]# docker save -o myRedis-new.tar redis
[root@service-monitoring redis6]# ll
total 113676
drwxr-xr-x 2 root root 4096 Apr 27 14:22 conf
drwxr-xr-x 2 polkitd root 4096 Apr 27 14:23 data
-rw------- 1 root root 116395520 Apr 27 21:26 myRedis-new.tar
[root@service-monitoring redis6]#
[root@service-monitoring redis6]# scp /home/redis6/myRedis-new.tar root@目标机器ip:/home/redis6/
root@目标机器ip's password:
myRedis.tar 100% 107MB 2.4MB/s 00:45 B
[root@service-monitoring redis6]#
[root@icv-monitor-platform-dev redis6]# docker load -i myRedis-new.tar b5ebffba54d3: Loading layer [==================================================>] 84MB/84MB c191d50afdbb: Loading layer [==================================================>] 338.4kB/338.4kB
f7c2005f3c1e: Loading layer [==================================================>] 4.165MB/4.165MB
e53fdc658ef8: Loading layer [==================================================>] 27.83MB/27.83MB
be0cee722bb2: Loading layer [==================================================>] 2.048kB/2.048kB
dd104008a3b7: Loading layer [==================================================>] 4.096kB/4.096kB
Loaded image: redis:6.2.7
5d52cb8d663b: Loading layer [==================================================>] 5.632kB/5.632kB
Loaded image: redis:myRedis
Loaded image: redis:myredis-1.0
[root@icv-monitor-platform-dev redis6]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis myRedis 9a91ea9de0d3 3 hours ago 113MB
redis myredis-1.0 9a91ea9de0d3 3 hours ago 113MB
redis 6.2.7 4b1123a829a1 4 months ago 113MB
hello-world latest feb5d9fea6a5 19 months ago 13.3kB
[root@icv-monitor-platform-dev redis6]#
可见,这是将所有 redis tag都搞过来了,所以迁移时,最好指定 tag。现在迁移过来3 个image 了,随机挑一个来启动它:
[root@icv-monitor-platform-dev redis6]# docker run -P --name redisABC -d redis:myredis-1.0
28888809ef748666e72248a743c711c38efc3f0c441ac594962bb84756f1c69a
[root@icv-monitor-platform-dev redis6]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28888809ef74 redis:myredis-1.0 "docker-entrypoint.s…" 20 seconds ago Up 19 seconds 0.0.0.0:32768->6379/tcp, :::32768->6379/tcp redisABC
[root@icv-monitor-platform-dev redis6]#
10.3 Docker Hub push/pull
点击 create repository:
填写 name、description,最后点击 create:
看到仓库:
准备 push 的镜像:
[root@service-monitoring redis6]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis myRedis 9a91ea9de0d3 4 hours ago 113MB
[root@service-monitoring redis6]# docker tag redis:myRedis 13236519712/java-redis:myRedis-v1.0
[root@service-monitoring redis6]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
13236519712/java-redis myRedis-v1.0 9a91ea9de0d3 4 hours ago 113MB
redis myRedis 9a91ea9de0d3 4 hours ago 113MB
[root@service-monitoring redis6]# docker push 13236519712/java-redis:myRedis-v1.0
The push refers to repository [docker.io/13236519712/java-redis]
5d52cb8d663b: Preparing
dd104008a3b7: Preparing
be0cee722bb2: Preparing
e53fdc658ef8: Preparing
f7c2005f3c1e: Preparing
c191d50afdbb: Waiting
b5ebffba54d3: Waiting
denied: requested access to the resource is denied
[root@service-monitoring redis6]#
需要先登录哦:
[root@service-monitoring redis6]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 13236519712
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@service-monitoring redis6]#
密码会被加密保存在本地哦:
重新 push:
[root@service-monitoring redis6]# docker push 13236519712/java-redis:myRedis-v1.0
The push refers to repository [docker.io/13236519712/java-redis]
5d52cb8d663b: Pushed
dd104008a3b7: Mounted from library/redis
be0cee722bb2: Mounted from library/redis
e53fdc658ef8: Mounted from library/redis
f7c2005f3c1e: Mounted from library/redis
c191d50afdbb: Mounted from library/redis
b5ebffba54d3: Mounted from library/redis
myRedis-v1.0: digest: sha256:8fba1134ecd7cc32f8dc954793354d1beb17764924644c8825278359252859b2 size: 1780
[root@service-monitoring redis6]#
hub 中可见已上传的内容:
最后登出:
docker logout
那么,此时,其他开发者就能直接拉取了。
[root@service-monitoring redis6]# docker pull 13236519712/java-redis:myRedis-v1.0
myRedis-v1.0: Pulling from 13236519712/java-redis
Digest: sha256:8fba1134ecd7cc32f8dc954793354d1beb17764924644c8825278359252859b2
Status: Image is up to date for 13236519712/java-redis:myRedis-v1.0
docker.io/13236519712/java-redis:myRedis-v1.0
[root@service-monitoring redis6]#
标签:ago,monitoring,service,redis,一键,Docker,root,docker,Redis
From: https://blog.51cto.com/u_11440114/6235723