倉庫(Repository)是Docker另一個重要的概念,是集中存放鏡像的地方。由於Docker中的鏡像管理的靈感很大程度來源於Git,所以可以對比Git倉庫來理解Docker倉庫。
如同一個Git倉庫含有多次提交和多個項目版本,一個Docker倉庫也可以有多個版本的鏡像。很多新手容易混淆註冊服務器(Registry)和倉庫,一個倉庫可以認爲是一個項目,比如nginx,ubuntu,而註冊服務器是託管這些倉庫和項目的地方,一個註冊服務器下面可能會有很多倉庫。Docker官方提供了一個公有的註冊服務器Docker Hub(https://hub.docker.com/),如同Docker中Github。
如同Guthub支持搜索項目一樣,Docker Hub網站也支持搜索鏡像倉庫,比如搜索ubuntu項目
可以看到一共搜索出3萬多個倉庫,而且支持對搜索結果的二次篩選,展示的倉庫名中沒有‘/’ 的一般表示Docker官方提供的倉庫,而帶有 ‘/’ 的一般是第三方上傳的鏡像,其中 ‘/’之前的爲用戶名,例如上面搜索結果中的 rastasheep/ubuntu-sshd 表示由用戶rastasheep上傳的ubuntu-sshd鏡像,點擊進去可以看到倉庫的詳情,包括簡介和詳細介紹,tag以及拉取鏡像倉庫的命令。
如果不想直接訪問Docker Hub,也可以使用在之前的文章中已經介紹過docker search命令,該命令可以查找指定的鏡像倉庫,例如搜索nginx鏡像
不過該命令無法展示倉庫的詳細信息,如果想要詳細瞭解一個倉庫,比如tag信息,建議還是去Docker Hub網站上去查看。
可以使用docker pull 命令來拉取倉庫中的某個鏡像,拉取時可以指定拉取某個具體tag,否則默認使用latest標籤。例如拉取17.10版的ubuntu:
[root@localhost ~]# docker pull ubuntu:17.10
17.10: Pulling from ubuntu
b51049254730: Pull complete
eb184887c0e1: Pull complete
a956aea8c8d6: Pull complete
9146812b99b2: Pull complete
e570d2375d84: Pull complete
7465a8617a6a: Pull complete
Digest: sha256:ef769d2f0f7247019125eba0642973f7812443e16889afe519f7ab493542924b
Status: Downloaded newer image for ubuntu:17.10
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 17.10 7465a8617a6a 59 minutes ago 93.83 MB
nginx backup 410962fdbc38 3 days ago 106.6 MB
nginx latest 2ecc072be0ec 9 days ago 108.3 MB
ubuntu 14.04 b44ce450cb60 5 weeks ago 188 MB
Docker Hub
如果我們想要在Docker Hub 中發佈自己的鏡像,則我們需要先註冊Docker Hub賬號和登錄,可以直接在Docker Hub網站上註冊和登陸,這個步奏比較簡單不再贅述,這裏主要介紹下命令行模式下的註冊和登錄。
docker login / logout
[root@localhost ~]# docker login --help
Usage: docker login [OPTIONS] [SERVER]
Register or log in to a Docker registry server, if no server is
specified "https://index.docker.io/v1/" is the default.
-e, --email= Email
--help=false Print usage
-p, --password= Password
-u, --username= Username
docker login 命令可以註冊或者登錄一個註冊服務器,如果沒有指定註冊服務器,則默認使用Docker官方的 "https://index.docker.io/v1/"。
登錄時如果指定賬號不存在,則會提示創建了新賬號並且給你的註冊郵箱發送一封確認郵件。
確認之後即可以用該賬號來登錄Docker Hub,docker logout 可以用來退出登錄。
由於網絡環境的因素,我們訪問Docker Hub官方註冊服務器有時會出現速度很慢或者無法訪問的問題,其實國內也有不少雲服務商提供倉庫鏡像服務,比如阿里雲Hub(https://dev.aliyun.com/search.html)和 時速雲Hub(https://hub.tenxcloud.com/)。
下面演示在時速雲上搜索ubuntu鏡像並把鏡像pull下來
[root@localhost ~]# docker pull index.tenxcloud.com/tenxcloud/ubuntu:latest
latest: Pulling from index.tenxcloud.com/tenxcloud/ubuntu
511136ea3c5a: Pull complete
f3c84ac3a053: Pull complete
a1a958a24818: Pull complete
9fec74352904: Pull complete
d0955f21bf24: Pull complete
36d5fb3e7ebd: Pull complete
1110b073af0d: Pull complete
753494f2b946: Pull complete
eeb69b27fb97: Pull complete
f3cb29872d39: Pull complete
8725d1748344: Pull complete
7c30ef94acad: Pull complete
4ac1b2fa5d87: Pull complete
ec887a998177: Pull complete
f82ec9e0fed1: Pull complete
fe0a5483fdac: Pull complete
302005b17ee8: Pull complete
921181ee92a9: Pull complete
2d4cb9fc6d24: Already exists
Digest: sha256:0334de0f53881d00c7af17aedfcad5c094634e14d1898a4aed13b8fcc990a61d
Status: Downloaded newer image for index.tenxcloud.com/tenxcloud/ubuntu:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
nginx latest 2ecc072be0ec 13 days ago 108.3 MB
centos v7.0 fae454d6fc7b 3 weeks ago 434.5 MB
mysql 5.6.37 c6d1fd492efc 4 weeks ago 299 MB
ubuntu 14.04 b44ce450cb60 5 weeks ago 188 MB
quay.io/coreos/etcd v3.0.4 3b17a5f34e6c 15 months ago 43.3 MB
index.tenxcloud.com/tenxcloud/ubuntu latest 2d4cb9fc6d24 23 months ago 251 MB
如同我們可以用git push命令來推送我們的代碼提交,我們也可以用docker push 命令來向註冊服務器推送自己的鏡像(或倉庫),不過首先要docker login登錄自己的賬號。下面我們演示把從時速雲上拉下來的鏡像push到Docker Hub 上。
[root@localhost ~]# docker login -e [email protected] -u 1054948153 -p zxc123456
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
[root@localhost ~]# docker tag index.tenxcloud.com/tenxcloud/ubuntu:latest 1054948153/ubuntu:v0.1
[root@localhost ~]# docker push 1054948153/ubuntu:v0.1
The push refers to a repository [1054948153/ubuntu] (len: 1)
2d4cb9fc6d24: Image already exists
2d4cb9fc6d24: Buffering to Disk
921181ee92a9: Image already exists
302005b17ee8: Image already exists
f82ec9e0fed1: Image successfully pushed
f82ec9e0fed1: Buffering to Disk
ec887a998177: Image already exists
4ac1b2fa5d87: Image already exists
7c30ef94acad: Image already exists
f3cb29872d39: Image successfully pushed
eeb69b27fb97: Image successfully pushed
753494f2b946: Image successfully pushed
1110b073af0d: Image successfully pushed
1110b073af0d: Buffering to Disk
36d5fb3e7ebd: Image already exists
9fec74352904: Image successfully pushed
a1a958a24818: Image successfully pushed
f3c84ac3a053: Image successfully pushed
f3c84ac3a053: Buffering to Disk
Digest: sha256:369ee5d4b2e4bba91011ddd99ae180b3e322c0dbec40f6980aee6b45f9f619b4
成功之後便可以在Docker Hub上查看該倉庫。
以上就是關於Docker 倉庫的簡單操作^^