docker的私有倉庫類似maven的私服,一般用於公司內部搭建一個類似docker hub的環境,這樣上傳、下載鏡像速度較快,本文將演示如何在mac上利用docker-machine搭建無需SSL證書的私有倉庫。
一、查看docker-machine虛擬機IP
docker-machine ip default
默認情況下docker-toolbox創建的虛擬機名稱為default,如果您的虛擬機名字不是這個,上面命令最后的default換成真實的虛擬機名字,假設default分配的IP為192.168.99.100
二、修改虛擬機中的docker啟動配置
由於docker最新版本默認訪問私服時,強制采用SSL安全連接,但一般內部應用時不需要這么高的安全級別,參考下面的做法降低安全設置:
docker-machine ssh default sudo vi /var/lib/boot2docker/profile
在profile文件最后加上:
EXTRA_ARGS="--insecure-registry 192.168.99.100:5000"
然后exit退出default,輸入以下命令重啟虛擬機
docker-machine restart default
三、創建私服容器
dao pull registry docker run -d -p 5000:5000 --restart=always -h registry \ --name registry \ -v /Users/yjmyzz/data/registry:/tmp/registry \ registry
第1行的dao pull registry表示將從daocloud.io上拉取registry鏡像,如果本機已經有該鏡像,可以省略。
-v 后面的路徑,大家改成實際路徑,這個目錄用於存放push到私有倉庫的images文件。
四、測試上傳、下載
4.1 先從daocloud.io上拉一個hello-world
hello-world這個鏡像只有960b,可以拿這個練手
dao pull hello-world
4.2 將hello-world打標簽成私服鏡像
docker tag hello-world 192.168.99.100:5000/hello-world
上面的ip要換真實的虛擬機ip,執行完以后,本機鏡像文件應該能看到這個images,見下圖:
注:原始鏡像hello-world與打tag后的鏡像具有相同的IMAGE ID,說明這二個鏡像就是同一個,只是tag不同而已。
4.3 上傳到私有倉庫
docker push 192.168.99.100:5000/hello-world
順利的話,應該很快就能上傳完:
➜ ~ docker push 192.168.99.100:5000/hello-world The push refers to a repository [192.168.99.100:5000/hello-world] (len: 1) Sending image list Pushing repository 192.168.99.100:5000/hello-world (1 tags) 3f12c794407e: Image successfully pushed 975b84d108f1: Image successfully pushed Pushing tag for rev [975b84d108f1] on {http://192.168.99.100:5000/v1/repositories/hello-world/tags/latest}
可以直接在瀏覽器里訪問:http://192.168.99.100:5000/v1/search,如果能看到
{ "num_results": 1, "query": "", "results": [ { "description": "", "name": "library/hello-world" } ] }
說明上傳成功
4.4 從私有倉庫下載
因為本機已經有hello-world的鏡像了,為了方便驗證,先把它刪除:
docker rmi -f hello-world 192.168.99.100:5000/hello-world #或 #docker rmi -f 975b84d108f1 #即:hello-world的IMAGE ID
然后下載:
docker pull 192.168.99.100:5000/hello-world
內網環境,應該很快就能下載完成:
➜ ~ docker pull 192.168.99.100:5000/hello-world Using default tag: latest Pulling repository 192.168.99.100:5000/hello-world 975b84d108f1: Download complete 3f12c794407e: Download complete Status: Downloaded newer image for 192.168.99.100:5000/hello-world:latest 192.168.99.100:5000/hello-world: this image was pulled from a legacy registry.
Important: This registry version will not be supported in future versions of docker.
注:如果私有倉庫要放置在公網上,建議還是按官方推薦的做法,設置SSL證書,強制走https協議,否則將有安全風險。
參考文章:
1. Docker私有Registry在CentOS6.X下安裝指南
2. 搭建私有 Docker 倉庫服務器
3. Use private docker registry in OS-X
4. Deploying a registry server
5. allow insecure registry in host provisioned with docker-machine
6. Adding trusted root certificates to the server
7. How To Set Up a Private Docker Registry on Ubuntu 14.04