1. 程式人生 > >搭建私有 Docker 倉庫伺服器

搭建私有 Docker 倉庫伺服器

Docker Hub 是 Docker 官方的公共倉庫伺服器,使用者在 DockerHub 上只能建立一個私有倉庫,這對於有些使用者是不夠用的,而且 DockerHub 伺服器的訪問速度也是個很大問題,那麼我們希望能在自己本地的伺服器上建立一個類似於 DockerHub 倉庫伺服器供團隊使用,這也是可以的。

我測試的環境是 Mac OSX 下,已經通過 Boot2Docker 工具安裝好 Docker 的環境,通過 命令boot2docker ip 可以檢視虛擬機器的 IP 是 192.168.59.104。那麼下面我就在自己虛擬機器上搭建一個私有的 Docker 倉庫伺服器:

安裝執行 Docker-Registry

執行官方提供的 registry 映象,將埠對映到主機的 5000 埠上,其它均使用預設配置:

[email protected] ⮀ ~ ⮀ docker run -d -p 5000:5000 registry
Unable to find image 'registry:latest' locally
6cfde7386ab2: Pull complete
9789d95d9fda: Pull complete
19443e64f223: Pull complete
b329371ab73c: Pull complete
f0daee9a4e8f: Pull complete
a66e50e56475: Pull complete
8ab3d2988df5: Pull complete
5f60fa7ea945: Pull complete
db22a140c899: Pull complete
5b2fff9306bd: Pull complete
511136ea3c5a: Already exists
f3c84ac3a053: Already exists
a1a958a24818: Already exists
9fec74352904: Already exists
d0955f21bf24: Already exists
registry:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Status: Downloaded newer image for registry:latest
8fb8e82e61822e593e10b59a4e7bbad18c789b34e3b38942d5b63dccb497ed09

上傳映象到私有倉庫

建立好私有倉庫之後,我們就可以向該倉庫上傳映象,別人也可以從該倉庫下載映象了。

檢視本地已有的映象:

[email protected] ⮀ ~ ⮀ docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
nginx                       latest              637d3b2f5fb5        4 days ago          93.44 MB
mysql                       latest              0feafece277d        11 days ago         282.9 MB
wordpress                   latest              f90659c8fdb9        2 weeks ago         451.5 MB
ubuntu                      latest              d0955f21bf24        4 weeks ago         188.3 MB
google/golang               latest              3cc1d7ae0e9c        11 weeks ago        611.3 MB
hello-world                 latest              e45a5af57b00        3 months ago        910 B

通過 docker tage 命令將 hello-world 這個映象標記為 192.168.59.104:5000/hello-world之後,再 push 到該映象到私有倉庫:

[email protected] ⮀ ~ ⮀ docker tag hello-world 192.168.59.104:5000/hello-world
[email protected] ⮀ ~ ⮀ docker push 192.168.59.104:5000/hello-world
FATA[0000] Error: v1 ping attempt failed with error: Get https://192.168.59.104:5000/v1/_ping: dial tcp 192.168.59.104:5000: connection refused. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry 192.168.59.104:5000` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/192.168.59.104:5000/ca.crt

發現報錯,使用 SO上類似問題 的解決方法可以解決上面的錯誤:

To use the --insecure-registry option, add it to the file /var/lib/boot2docker/profile inside the boot2docker VM. You can get into the VM with boot2docker ssh. The file contents should look like:

EXTRA_ARGS="--insecure-registry REGISTRY_IP:PORT" You will then need to restart boot2docker (e.g. boot2docker restart).

步驟如下:

  1. 使用 boot2docker ssh 登陸到 boot2docker 虛擬機器
  2. 修改 /var/lib/boot2docker/profile 檔案,向該檔案中增加一行:EXTRA_ARGS="--insecure-registry 192.168.59.104:5000"
  3. 退出該虛擬機器並使用命令 boot2docker restart 重啟 boot2docker

完成重啟之後,將私有倉庫伺服器執行起來,並 push hello-world 到該倉庫:

[email protected] ⮀ ~ ⮀ docker run -d -p 5000:5000 registry
4935607095a22655da1ef91feb6f569264a50529cb8d594d520fe62da81250db
[email protected] ⮀ ~ ⮀ docker push 192.168.59.104:5000/test
The push refers to a repository [192.168.59.104:5000/test] (len: 1)
Sending image list
Pushing repository 192.168.59.104:5000/test (1 tags)
511136ea3c5a: Image successfully pushed
31cbccb51277: Image successfully pushed
e45a5af57b00: Image successfully pushed
Pushing tag for rev [e45a5af57b00] on {http://192.168.59.104:5000/v1/repositories/test/tags/latest}

使用 Docker 的 RESTful API 可以檢視倉庫伺服器中的映象:

[email protected] ⮀ ~ ⮀ curl http://192.168.59.104:5000/v1/search
{"num_results": 1, "query": "", "results": [{"description": "", "name": "library/hello-world"}]}%

表示 hello-world 映象已成功長傳至私有倉庫伺服器了。

從私有倉庫中下載、搜尋映象

其他機器可以從私有倉庫伺服器上下載、搜尋映象等,與從 Docker Hub 上操作無異,只不過需要指出倉庫的位置,如:

[email protected] ⮀ ~ ⮀ docker rmi -f 192.168.59.104:5000/hello-world
Untagged: 192.168.59.104:5000/hello-world:latest
Deleted: e45a5af57b00862e5ef5782a9925979a02ba2b12dff832fd0991335f4a11e5c5
Deleted: 31cbccb51277105ba3ae35ce33c22b69c9e3f1002e76e4c736a2e8ebff9d7b5d
[email protected] ⮀ ~ ⮀ docker search 192.168.59.104:5000/hello-world
NAME                  DESCRIPTION   STARS     OFFICIAL   AUTOMATED
library/hello-world                 0
[email protected] ⮀ ~ ⮀ docker pull 192.168.59.104:5000/hello-world
Pulling repository 192.168.59.104:5000/hello-world
e45a5af57b00: Download complete
511136ea3c5a: Download complete
31cbccb51277: Download complete
Status: Downloaded newer image for 192.168.59.104:5000/hello-world:latest

參考資料: http://dockerpool.com/static/books/docker_practice/repository/local_repo.html