为了能实现镜像的异地使用以及进行更好的镜像管理,将自定义的镜像上传至dockerhub或阿里云私有仓库,并验证在其他节点进行拉取。
1 上传docker镜像到dockerhub中
首先得有一个dockerhub账号,访问dockerhub。
第一步:登录dockerhub
root@master:/home/hqc/docker_learning/flask-hello-world# docker login -u error12
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
# 登录成功!其中error12为我的dockerhub账户ID
第二步:给镜像新打一个标签
不然将会push失败
root@master:/home/hqc/docker_learning/flask-hello-world# docker push flask-hello-world:v1.0
The push refers to repository [docker.io/library/flask-hello-world]
d22d05d670e3: Preparing
8e0da839bae9: Preparing
42ce61e841fa: Preparing
a9b125f164c3: Preparing
e24045f8c247: Preparing
b7b662b31e70: Waiting
6f5234c0aacd: Waiting
8a5844586fdb: Waiting
a4aba4e59b40: Waiting
5499f2905579: Waiting
a36ba9e322f7: Waiting
denied: requested access to the resource is denied
# push失败
root@master:/home/hqc/docker_learning/flask-hello-world# docker tag 6c3d77891c6a dockerhub/flask-hello-world:v1.0
root@master:/home/hqc/docker_learning/flask-hello-world# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask-hello-world v1.0 6c3d77891c6a 38 minutes ago 928MB
dockerhub/flask-hello-world v1.0 6c3d77891c6a 38 minutes ago 928MB
# 新打一个标签,并查看镜像
root@master:/home/hqc/docker_learning/flask-hello-world# docker push dockerhub/flask-hello-world:v1.0
denied: requested access to the resource is denied
# 仍然push失败,原因是:新打的标签名不对(dockerhub/flask-hello-world:v1.0),/前面应该为对应的账户ID。
第三步:push镜像到dockerhub公有仓库
root@master:/home/hqc/docker_learning/flask-hello-world# docker tag 6c3d77891c6a error12/flask-hello-world:v1.0
root@master:/home/hqc/docker_learning/flask-hello-world# docker push error12/flask-hello-world:v1.0
The push refers to repository [docker.io/error12/flask-hello-world]
d22d05d670e3: Pushed
8e0da839bae9: Pushed
42ce61e841fa: Mounted from error12/hqc-helloworld
a9b125f164c3: Mounted from error12/hqc-helloworld
e24045f8c247: Mounted from error12/hqc-helloworld
b7b662b31e70: Mounted from error12/hqc-helloworld
6f5234c0aacd: Mounted from error12/hqc-helloworld
8a5844586fdb: Mounted from error12/hqc-helloworld
a4aba4e59b40: Mounted from error12/hqc-helloworld
5499f2905579: Mounted from error12/hqc-helloworld
a36ba9e322f7: Mounted from error12/hqc-helloworld
v1.0: digest: sha256:af862fb9415a8121710e7d87b0ddb085df8a70cc744d6ebe4a8cd64f6d242b2a size: 2636
# 更改之后,成功!!!
dockerhub界面中也可以查看,并且提供下载。
2 上传镜像到阿里云镜像私有仓库
01 注册阿里云账户
02 创建个人实例
里边可新建命名空间(注意:个人实例只提供三个命名空间,需谨慎创建)和镜像仓库。
03 创建命名空间
04 创建镜像仓库
代码源应该选择本地仓库
- 可在阿里云平台界面中直接创建,可选择公有或私有仓库;
- 也可通过命令行创建
# 首先得先退出之前登录的dockerhub仓库
root@master:/home/hqc/docker_learning/flask-hello-world# docker logout
Removing login credentials for https://index.docker.io/v1/
# 登录阿里云仓库
root@master:/home/hqc/docker_learning/flask-hello-world# docker login --username=errorhqc兮 registry.cn-beijing.aliyuncs.com
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@master:/home/hqc/docker_learning/flask-hello-world# docker tag 6c3d77891c6a registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world:v1.0
# 和上传到dockerhub类似,也需要打一个新标签
root@master:/home/hqc/docker_learning/flask-hello-world# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world v1.0 6c3d77891c6a 59 minutes ago 928MB
dockerhub/flask-hello-world v1.0 6c3d77891c6a 59 minutes ago 928MB
error12/flask-hello-world v1.0 6c3d77891c6a 59 minutes ago 928MB
flask-hello-world v1.0 6c3d77891c6a 59 minutes ago 928MB
# 查看镜像
root@master:/home/hqc/docker_learning/flask-hello-world# docker push registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world:v1.0
The push refers to repository [registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world]
d22d05d670e3: Pushed
8e0da839bae9: Pushed
42ce61e841fa: Mounted from hqc-k8s/docker_images
a9b125f164c3: Mounted from hqc-k8s/docker_images
e24045f8c247: Mounted from hqc-k8s/docker_images
b7b662b31e70: Mounted from hqc-k8s/docker_images
6f5234c0aacd: Mounted from hqc-k8s/docker_images
8a5844586fdb: Mounted from hqc-k8s/docker_images
a4aba4e59b40: Mounted from hqc-k8s/docker_images
5499f2905579: Mounted from hqc-k8s/docker_images
a36ba9e322f7: Mounted from hqc-k8s/docker_images
v1.0: digest: sha256:af862fb9415a8121710e7d87b0ddb085df8a70cc744d6ebe4a8cd64f6d242b2a size: 2636
# push成功!并且发现速度非常快,至少比dockerhub快
可见,push成功,自动创建了一个默认为私有
的镜像仓库。
具体操作官方有详细介绍:
3 在其他节点上拉取阿里云私有仓库的镜像
# 登录我的阿里云仓库
root@node01:/home/user# docker login --username=errorhqc兮 registry.cn-beijing.aliyuncs.com
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@node01:/home/user# docker pull registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world:v1.0
v1.0: Pulling from hqc-k8s/ali-flask-hello-world
647acf3d48c2: Pull complete
b02967ef0034: Pull complete
e1ad2231829e: Pull complete
5576ce26bf1d: Pull complete
a66b7f31b095: Pull complete
05189b5b2762: Pull complete
af08e8fda0d6: Pull complete
287d56f7527b: Pull complete
dc0580965fb6: Pull complete
c1147962ae93: Pull complete
9310a3ae1b50: Pull complete
Digest: sha256:af862fb9415a8121710e7d87b0ddb085df8a70cc744d6ebe4a8cd64f6d242b2a
Status: Downloaded newer image for registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world:v1.0
registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world:v1.0
# 拉取成功
root@node01:/home/user# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world v1.0 6c3d77891c6a 14 hours ago 928MB
# 查看镜像