Build, Ship and Anywhere

书接上回,镜像已构建完成,是时候把我们的镜像 如git本地仓库向github上推送一般 推送至docker Registry。这里我们选择阿里云,为什么?因为众所周知的原因,docker hub 慢呀。

1. 登录阿里云Docker Registry

sudo docker login –username=xx@qq.com registry.cn-hangzhou.aliyuncs.com

2. 将镜像push到Registry

$ sudo docker login --username=xx@qq.com registry.cn-hangzhou.aliyuncs.com

# 命令: sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:[镜像版本号]
$ sudo docker tag f1eed31931c4 registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:1.0

# 命令: sudo docker push registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:[镜像版本号]
$ sudo docker push registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:1.0

image-20200327003924008

3. 拉取

验证已push的阿里云docker image-先删除已有的镜像-再拉取镜像-再创建容器

删除镜像: docker rmi f1eed31931c4

Error response from daemon: conflict: unable to delete f1eed31931c4 (must be forced) - image is referenced in multiple repositories

查看镜像:docker images

REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
mywebapi                                                latest              f1eed31931c4        26 hours ago        208MB
registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi   1.0                 f1eed31931c4        26 hours ago        208MB
node                                                    latest              07e774543bdf        6 days ago          939MB
golang                                                  latest              6586e3d10e96        11 days ago         803MB
nginx                                                   latest              2073e0bcb60e        11 days ago         127MB
mcr.microsoft.com/dotnet/core/aspnet                    3.1                 e28362768eed        11 days ago         207MB
mysql                                                   latest              791b6e40940c        11 days ago         465MB
centos                                                  latest              470671670cac        3 weeks ago         237MB
hello-world                                             latest              fce289e99eb9        13 months ago       1.84kB
  • 对于被删除的ImageID,这里存在多个REPOSITORY名字引用

  • 即删除时指定名称,而不是IMAGE ID。

  • 然后再删除IMAGE ID就可以了


[root@MiWiFi-R2100-srv ~]# docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
mywebapi                                                latest              f1eed31931c4        26 hours ago        208MB
registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi   1.0                 f1eed31931c4        26 hours ago        208MB
node                                                    latest              07e774543bdf        6 days ago          939MB
golang                                                  latest              6586e3d10e96        11 days ago         803MB
nginx                                                   latest              2073e0bcb60e        11 days ago         127MB
mcr.microsoft.com/dotnet/core/aspnet                    3.1                 e28362768eed        11 days ago         207MB
mysql                                                   latest              791b6e40940c        11 days ago         465MB
centos                                                  latest              470671670cac        3 weeks ago         237MB
hello-world                                             latest              fce289e99eb9        13 months ago       1.84kB
[root@MiWiFi-R2100-srv ~]# docker rmi mywebapi
Untagged: mywebapi:latest
[root@MiWiFi-R2100-srv ~]# docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi   1.0                 f1eed31931c4        26 hours ago        208MB
node                                                    latest              07e774543bdf        6 days ago          939MB
golang                                                  latest              6586e3d10e96        11 days ago         803MB
nginx                                                   latest              2073e0bcb60e        11 days ago         127MB
mcr.microsoft.com/dotnet/core/aspnet                    3.1                 e28362768eed        11 days ago         207MB
mysql                                                   latest              791b6e40940c        11 days ago         465MB
centos                                                  latest              470671670cac        3 weeks ago         237MB
hello-world                                             latest              fce289e99eb9        13 months ago       1.84kB
[root@MiWiFi-R2100-srv ~]# docker rmi f1eed31931c4
Untagged: registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:1.0
Untagged: registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi@sha256:bbe9a94bc16e0336f24fac8a26e031687454434caf37cfd7773e433f3dc8cf04
Deleted: sha256:f1eed31931c406b632f75cb11526e45d22cddea3c29b70043c0a5ee2a0feb0c5
Deleted: sha256:98d4eec18e372143c997a37114169ec6d250ba212758fbc2f6948aaf3d0c392d
Deleted: sha256:a8a248621e4110edd327a316133bc475472e065a67e7ba92ffa0c76ba0b95867
Deleted: sha256:2b19bfb7451dde52d5427e88ca9d24c268a685af34f2d855fdba9bc48c82cd74
Deleted: sha256:badd3be10903a86fbbecada82fd1d0a6e11ab07d040d47a80d82f3aead47cffa
Deleted: sha256:4fbb352ef5d9cf37d2b0f2b12ad1ad950831cd4c025e8f5ec9085b0dbd2926d0
Deleted: sha256:f6e579c411e1fe59b1560d60687da9f3c0b6eae12ac0aea5cdc27130a1ce8a90
[root@MiWiFi-R2100-srv ~]# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
node                                   latest              07e774543bdf        6 days ago          939MB
golang                                 latest              6586e3d10e96        11 days ago         803MB
nginx                                  latest              2073e0bcb60e        11 days ago         127MB
mcr.microsoft.com/dotnet/core/aspnet   3.1                 e28362768eed        11 days ago         207MB
mysql                                  latest              791b6e40940c        11 days ago         465MB
centos                                 latest              470671670cac        3 weeks ago         237MB
hello-world                            latest              fce289e99eb9        13 months ago       1.84kB

拉取:sudo docker pull registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:1.0

[root@MiWiFi-R2100-srv ~]# sudo docker pull registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:1.0
1.0: Pulling from randyfield/mywebapi
bc51dd8edc1b: Already exists 
6848f14cfbf7: Already exists 
01ae9269f21a: Already exists 
e5a46ebecd63: Already exists 
8425bc024e3a: Already exists 
a52e6ca2844b: Pull complete 
Digest: sha256:bbe9a94bc16e0336f24fac8a26e031687454434caf37cfd7773e433f3dc8cf04
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:1.0
registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:1.0

4. 创建容器

docker run –name ali-mywebapi -d -p 81:5000 f1eed31931c4

[root@MiWiFi-R2100-srv ~]# docker run --name ali-mywebapi -d -p 81:5000 registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi
Unable to find image 'registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:latest' locally
docker: Error response from daemon: manifest for registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:latest not found: manifest unknown: manifest unknown.
See 'docker run --help'.

[root@MiWiFi-R2100-srv ~]# docker rm -f ali-mywebapi 
Error: No such container: ali-mywebapi

##注意看这条命令,是有问题的。
[root@MiWiFi-R2100-srv ~]# docker run --name ali-mywebapi -d -p 81:5000 registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi
Unable to find image 'registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:latest' locally
docker: Error response from daemon: manifest for registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:latest not found: manifest unknown: manifest unknown.
See 'docker run --help'.
[root@MiWiFi-R2100-srv ~]# docker run --name ali-mywebapi -d -p 81:5000 f1eed31931c4
7ac0461905065b32acbda35c72f88fc28dccb1760837282565846b8b0ef62b7b

[root@MiWiFi-R2100-srv ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
7ac046190506        f1eed31931c4        "dotnet docker-aspne…"   11 seconds ago      Up 10 seconds       0.0.0.0:81->5000/tcp   ali-mywebapi
[root@MiWiFi-R2100-srv ~]# 

综上,为什么docker run --name ali-mywebapi -d -p 81:5000 registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi

daemon,会提示??

Unable to find image 'registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:latest' locally
docker: Error response from daemon: manifest for registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:latest not found: manifest unknown: manifest unknown.
See 'docker run --help'.

原来是因为命令中没有指定tag,我们在push时,是有指定tag的。

docker run –name ali-mywebapi -d -p 81:5000 registry.cn-hangzhou.aliyuncs.com/randyfield/mywebapi:1.0

5. 想象一下

打开阿里云的仓库

image-20200327003828165

镜像就80M,想象一下,如果我们把发布的应用做成镜像,上传至仓库,无论私有(搭建私有docker镜像库,会不成为下一篇博文呢)或者公有,然后部署时,执行 docker pull , docker run ,是不是很方便?部署就完成了。