一、概述
之前详细讲述了如何自定义一个镜像,那也只是自己玩呀,怎么才能将自己的镜像分享给同事或者更多的人呢?一种方法是将自定义镜像上传到阿里云镜像仓库,然后让别人去阿里云仓库下载。还有一种是建立自己的镜像仓库,然后将镜像上传到自己的仓库。接下来我要讲的是通过阿里云来达到共享的目的。
二、操作
docker
1. 编写Dockerfile
FROM centos MAINTAINER javacfox<javacfox@sina.com> RUN yum -y install vim CMD echo `build success` CMD /bin/bash
2. build镜像
[root@localhost docker]# docker build -f /opt/docker/Dockerfile1 -t javacfox/centos:1.2 . Sending build context to Docker daemon 3.072 kB Step 1/5 : FROM centos ---> 831691599b88 Step 2/5 : MAINTAINER javacfox<javacfox@sina.com> ---> Running in ce003236bae6 ---> bbc74738ac5a Removing intermediate container ce003236bae6 Step 3/5 : RUN yum -y install vim ---> Running in df30a09f44d9 CentOS-8 - AppStream 567 kB/s | 5.8 MB 00:10 CentOS-8 - Base 731 kB/s | 2.2 MB 00:03 CentOS-8 - Extras 4.0 kB/s | 6.7 kB 00:01 Dependencies resolved. ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: vim-enhanced x86_64 2:8.0.1763-13.el8 AppStream 1.4 M Installing dependencies: gpm-libs x86_64 1.20.7-15.el8 AppStream 39 k vim-common x86_64 2:8.0.1763-13.el8 AppStream 6.3 M vim-filesystem noarch 2:8.0.1763-13.el8 AppStream 48 k which x86_64 2.21-12.el8 BaseOS 49 k Transaction Summary ================================================================================ Install 5 Packages Total download size: 7.8 M Installed size: 31 M Downloading Packages: (1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm 216 kB/s | 39 kB 00:00 (2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 267 kB/s | 48 kB 00:00 (3/5): which-2.21-12.el8.x86_64.rpm 62 kB/s | 49 kB 00:00 (4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 809 kB/s | 1.4 MB 00:01 (5/5): vim-common-8.0.1763-13.el8.x86_64.rpm 707 kB/s | 6.3 MB 00:09 -------------------------------------------------------------------------------- Total 669 kB/s | 7.8 MB 00:11 warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY CentOS-8 - AppStream 1.3 MB/s | 1.6 kB 00:00 Importing GPG key 0x8483C65D: Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>" Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : which-2.21-12.el8.x86_64 1/5 Installing : vim-filesystem-2:8.0.1763-13.el8.noarch 2/5 Installing : vim-common-2:8.0.1763-13.el8.x86_64 3/5 Installing : gpm-libs-1.20.7-15.el8.x86_64 4/5 Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64 4/5 Installing : vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5 Running scriptlet: vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5 Running scriptlet: vim-common-2:8.0.1763-13.el8.x86_64 5/5 Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5 Verifying : vim-common-2:8.0.1763-13.el8.x86_64 2/5 Verifying : vim-enhanced-2:8.0.1763-13.el8.x86_64 3/5 Verifying : vim-filesystem-2:8.0.1763-13.el8.noarch 4/5 Verifying : which-2.21-12.el8.x86_64 5/5 Installed: gpm-libs-1.20.7-15.el8.x86_64 vim-common-2:8.0.1763-13.el8.x86_64 vim-enhanced-2:8.0.1763-13.el8.x86_64 vim-filesystem-2:8.0.1763-13.el8.noarch which-2.21-12.el8.x86_64 Complete! ---> 3fba200b93c5 Removing intermediate container df30a09f44d9 Step 4/5 : CMD echo `build success` ---> Running in 863f5dc67f0e ---> 3b9ab5a63978 Removing intermediate container 863f5dc67f0e Step 5/5 : CMD /bin/bash ---> Running in 987449a5e90f ---> 0382c2281d03 Removing intermediate container 987449a5e90f Successfully built 0382c2281d03
3. 访问网站并在网站上创建仓库:https://cr.console.aliyun.com/cn-hangzhou/instances/repositories
- 注册并登录阿里云
- 下次再选
- 在阿里云上创建仓库
- 点击管理,进入管理界面
$ sudo docker login --username=javacfox registry.cn-hangzhou.aliyuncs.com $ sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/javacfoxs/centos:[镜像版本号] $ sudo docker push registry.cn-hangzhou.aliyuncs.com/javacfoxs/centos:[镜像版本号]
4. push到远程仓库(执行从远程仓库复制出来的命令)
[root@localhost docker]# docker login --username=javacfox registry.cn-hangzhou.aliyuncs.com Password: Login Succeeded [root@localhost docker]# docker tag 0382c2281d03 registry.cn-hangzhou.aliyuncs.com/javacfoxs/centos:1.2 [root@localhost docker]# docker push registry.cn-hangzhou.aliyuncs.com/javacfoxs/centos:1.2 The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/javacfoxs/centos] b93ffef08f20: Pushed eb29745b8228: Pushed 1.2: digest: sha256:0db7adc7423991b5a8d4342e22a951cca00c617f1b8e6f9d6e0b2f0663d7cc6d size: 741 [root@localhost docker]#
注意:可以选择仓库,阿里云,github,本地仓库等;创建在阿里云的才能在阿里云上查到。