Docker 教程之私有倉庫詳解
私有倉庫
有時(shí)候使用 Docker Hub 這樣的公共倉庫可能不方便,用戶可以創(chuàng)建一個(gè)本地倉庫供私人使用。
本節(jié)介紹如何使用本地倉庫。
docker-registry 是官方提供的工具,可以用于構(gòu)建私有的鏡像倉庫。
安裝運(yùn)行 docker-registry
容器運(yùn)行
在安裝了 Docker 后,可以通過獲取官方 registry 鏡像來運(yùn)行。
$ sudo docker run -d -p 5000:5000 registry
這將使用官方的 registry 鏡像來啟動(dòng)本地的私有倉庫。 用戶可以通過指定參數(shù)來配置私有倉庫位置,例如配置鏡像存儲(chǔ)到 Amazon S3 服務(wù)。
$ sudo docker run \ -e SETTINGS_FLAVOR=s3 \ -e AWS_BUCKET=acme-docker \ -e STORAGE_PATH=/registry \ -e AWS_KEY=AKIAHSHB43HS3J92MXZ \ -e AWS_SECRET=xdDowwlK7TJajV1Y7EoOZrmuPEJlHYcNP2k4j49T \ -e SEARCH_BACKEND=sqlalchemy \ -p 5000:5000 \ registry
此外,還可以指定本地路徑(如 /home/user/registry-conf )下的配置文件。
$ sudo docker run -d -p 5000:5000 -v /home/user/registry-conf:/registry-conf -e DOCKER_REGISTRY_CONFIG=/registry-conf/config.yml registry
默認(rèn)情況下,倉庫會(huì)被創(chuàng)建在容器的 /tmp/registry 下??梢酝ㄟ^ -v 參數(shù)來將鏡像文件存放在本地的指定路徑。 例如下面的例子將上傳的鏡像放到 /opt/data/registry 目錄。
$ sudo docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry registry
本地安裝
對于 Ubuntu 或 CentOS 等發(fā)行版,可以直接通過源安裝。
Ubuntu
$ sudo apt-get install -y build-essential python-dev libevent-dev python-pip liblzma-dev $ sudo pip install docker-registry
CentOS
$ sudo yum install -y python-devel libevent-devel python-pip gcc xz-devel $ sudo python-pip install docker-registry
也可以從 docker-registry 項(xiàng)目下載源碼進(jìn)行安裝。
$ sudo apt-get install build-essential python-dev libevent-dev python-pip libssl-dev liblzma-dev libffi-dev $ git clone https://github.com/docker/docker-registry.git $ cd docker-registry $ sudo python setup.py install
然后修改配置文件,主要修改 dev 模板段的 storage_path 到本地的存儲(chǔ)倉庫的路徑。
$ cp config/config_sample.yml config/config.yml
之后啟動(dòng) Web 服務(wù)。
$ sudo gunicorn -c contrib/gunicorn.py docker_registry.wsgi:application
或者
$ sudo gunicorn --access-logfile - --error-logfile - -k gevent -b 0.0.0.0:5000 -w 4 --max-requests 100 docker_registry.wsgi:application
此時(shí)使用 curl 訪問本地的 5000 端口,看到輸出 docker-registry 的版本信息說明運(yùn)行成功。
注:config/config_sample.yml 文件是示例配置文件。
在私有倉庫上傳、下載、搜索鏡像
創(chuàng)建好私有倉庫之后,就可以使用 docker tag 來標(biāo)記一個(gè)鏡像,然后推送它到倉庫,別的機(jī)器上就可以下載下來了。例如私有倉庫地址為 192.168.7.26:5000。
先在本機(jī)查看已有的鏡像。
$ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest ba5877dc9bec 6 weeks ago 192.7 MB ubuntu 14.04 ba5877dc9bec 6 weeks ago 192.7 MB
使用docker tag 將 ba58 這個(gè)鏡像標(biāo)記為 192.168.7.26:5000/test(格式為 docker tag IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG])。
$ sudo docker tag ba58 192.168.7.26:5000/test root ~ # docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu 14.04 ba5877dc9bec 6 weeks ago 192.7 MB ubuntu latest ba5877dc9bec 6 weeks ago 192.7 MB 192.168.7.26:5000/test latest ba5877dc9bec 6 weeks ago 192.7 MB
使用 docker push 上傳標(biāo)記的鏡像。
$ sudo docker push 192.168.7.26:5000/test
The push refers to a repository [192.168.7.26:5000/test] (len: 1)
Sending image list
Pushing repository 192.168.7.26:5000/test (1 tags)
Image 511136ea3c5a already pushed, skipping
Image 9bad880da3d2 already pushed, skipping
Image 25f11f5fb0cb already pushed, skipping
Image ebc34468f71d already pushed, skipping
Image 2318d26665ef already pushed, skipping
Image ba5877dc9bec already pushed, skipping
Pushing tag for rev [ba5877dc9bec] on {http://192.168.7.26:5000/v1/repositories/test/tags/latest}
用 curl 查看倉庫中的鏡像。
$ curl http://192.168.7.26:5000/v1/search
{"num_results": 7, "query": "", "results":
[{"description": "", "name": "library/miaxis_j2ee"},
{"description": "", "name": "library/tomcat"},
{"description": "", "name": "library/ubuntu"},
{"description": "", "name": "library/ubuntu_office"},
{"description": "", "name": "library/desktop_ubu"},
{"description": "", "name": "dockerfile/ubuntu"},
{"description": "", "name": "library/test"}]}
這里可以看到 {"description": "", "name": "library/test"},表明鏡像已經(jīng)被成功上傳了。
現(xiàn)在可以到另外一臺(tái)機(jī)器去下載這個(gè)鏡像。
$ sudo docker pull 192.168.7.26:5000/test Pulling repository 192.168.7.26:5000/test ba5877dc9bec: Download complete 511136ea3c5a: Download complete 9bad880da3d2: Download complete 25f11f5fb0cb: Download complete ebc34468f71d: Download complete 2318d26665ef: Download complete $ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE 192.168.7.26:5000/test latest ba5877dc9bec 6 weeks ago 192.7 MB
可以使用 這個(gè)腳本 批量上傳本地的鏡像到注冊服務(wù)器中,默認(rèn)是本地注冊服務(wù)器 127.0.0.1:5000。例如:
$ wget https://github.com/yeasy/docker_practice/raw/master/_local/push_images.sh; sudo chmod a+x push_images.sh
$ ./push_images.sh ubuntu:latest centos:centos7
The registry server is 127.0.0.1
Uploading ubuntu:latest...
The push refers to a repository [127.0.0.1:5000/ubuntu] (len: 1)
Sending image list
Pushing repository 127.0.0.1:5000/ubuntu (1 tags)
Image 511136ea3c5a already pushed, skipping
Image bfb8b5a2ad34 already pushed, skipping
Image c1f3bdbd8355 already pushed, skipping
Image 897578f527ae already pushed, skipping
Image 9387bcc9826e already pushed, skipping
Image 809ed259f845 already pushed, skipping
Image 96864a7d2df3 already pushed, skipping
Pushing tag for rev [96864a7d2df3] on {http://127.0.0.1:5000/v1/repositories/ubuntu/tags/latest}
Untagged: 127.0.0.1:5000/ubuntu:latest
Done
Uploading centos:centos7...
The push refers to a repository [127.0.0.1:5000/centos] (len: 1)
Sending image list
Pushing repository 127.0.0.1:5000/centos (1 tags)
Image 511136ea3c5a already pushed, skipping
34e94e67e63a: Image successfully pushed
70214e5d0a90: Image successfully pushed
Pushing tag for rev [70214e5d0a90] on {http://127.0.0.1:5000/v1/repositories/centos/tags/centos7}
Untagged: 127.0.0.1:5000/centos:centos7
Done
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
在docker-compose.yml文件中配置容器的環(huán)境變量方式
本文介紹了在docker-compose.yml文件中設(shè)置和使用環(huán)境變量的方法,包括直接設(shè)置環(huán)境變量、從文件讀取環(huán)境變量以及使用多個(gè)環(huán)境文件進(jìn)行不同部署環(huán)境的配置2024-11-11
Docker Volumn容器間共享數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了Docker Volumn容器間共享數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Docker中Cgroup資源配置的實(shí)現(xiàn)
Cgroup不僅可以限制被namespace?隔離起來的資源,還可以為資源設(shè)置權(quán)重、計(jì)算使用量、操控進(jìn)程啟停等,本文主要介紹了Docker中Cgroup資源配置的實(shí)現(xiàn),感興趣的可以了解一下2023-09-09
使用Docker部署Filestash文件管理器的實(shí)現(xiàn)步驟
Filestash是一款功能強(qiáng)大的網(wǎng)絡(luò)文件管理工具,它允許用戶在單一界面下管理分布于不同地點(diǎn)和平臺(tái)的數(shù)據(jù),本文就來介紹一下Docker部署Filestash文件管理器的實(shí)現(xiàn)步驟,感興趣的可以了解一下2025-07-07
Docker數(shù)據(jù)卷常用操作代碼實(shí)例
這篇文章主要介紹了Docker數(shù)據(jù)卷常用操作代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Docker部署ELK7.3.0日志收集服務(wù)最佳實(shí)踐
這篇文章主要介紹了Docker部署ELK7.3.0日志收集服務(wù)最佳實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Docker部署SpringBoot項(xiàng)目的實(shí)現(xiàn)步驟
本文主要介紹了Docker部署SpringBoot項(xiàng)目的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2007-02-02

