三步搭建私有Docker Registry(V2)服務(wù)器

最近需要在學(xué)校內(nèi)網(wǎng)使用docker,所以有一個Registry會比較方便。網(wǎng)上的教程不少過時了或者操作麻煩梭冠,經(jīng)過踩坑無數(shù)之后總結(jié)了兩個快速部署方法。

網(wǎng)上方法千奇百怪,長篇大論看得心累,所以我希望三步之內(nèi)解決這件事疲憋,那么開始吧。


準(zhǔn)備工作:

安裝Docker

你需要安裝1.6.0以上的版本的Docker梁只。

sudo curl -sSL https://get.docker.com/ | sh
# 設(shè)置Docker以非Root用戶運(yùn)行缚柳,確保安全埃脏。
sudo usermod -aG docker your-user
# 安裝Compose:
curl -L https://github.com/docker/compose/releases/download/1.7.0-rc1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

獲取SSL證書

如果要使用域名綁定私有倉庫,必須開啟SSL喂击。

git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
./letsencrypt-auto certonly -d docker.zuolan.me
選擇第二個剂癌,自動生成證書

生成下面文字即為成功:

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
.........
.........
 - If you like Let's Encrypt, please consider supporting our work by:
   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

方法一、自動搭建(強(qiáng)烈推薦)

第一步:配置

克隆倉庫翰绊。

git clone https://github.com/vmware/harbor ~/harbor

編輯配置佩谷。

vim ~/harbor/Deploy/harbor.cfg

模板如下:

## Configuration file of Harbor

#The IP address or hostname to access admin UI and registry service.
#DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
########################################
#下面輸入你的倉庫網(wǎng)址,比如“docker.zuolan.me”监嗜。
########################################
hostname = docker.zuolan.me


#The protocol for accessing the UI and token/notification service, by default it is http.
#It can be set to https if ssl is enabled on nginx.
ui_url_protocol = https

#Email account settings for sending out password resetting emails.
#####################################
#這里的設(shè)置可以無視谐檀,只有密碼找回才會用到。
#####################################
email_server = smtp.mydomain.com 
email_server_port = 25
email_username = sample_admin@mydomain.com
email_password = abc
email_from = admin <sample_admin@mydomain.com>

##The password of Harbor admin, change this before any production use.
#####################
#下面輸入你的管理員密碼裁奇。
#####################
harbor_admin_password = password

##By default the auth mode is db_auth, i.e. the credentials are stored in a local database.
#Set it to ldap_auth if you want to verify a user's credentials against an LDAP server.
auth_mode = db_auth

#The url for an ldap endpoint.
#########
#可以不填桐猬。
#########
ldap_url = ldaps://ldap.zuolan.me

#The basedn template to look up a user in LDAP and verify the user's password.
################################################
#我的的域名是zuolan.me,所以這里我填dc=zuolan,dc=me刽肠。
################################################
ldap_basedn = uid=%s,ou=people,dc=zuolan,dc=me

#The password for the root user of mysql db, change this before any production use.
#####################
#下面輸入你的數(shù)據(jù)庫密碼溃肪。
#####################
db_password = password

#Turn on or off the self-registration feature
self_registration = on
#####

第二步:配置Nginx

 cd ~/Deploy/config/nginx

移動你的證書到cert/目錄。

cp yourdomain.com.crt cert/
cp yourdomain.com.key cert/ 

備份一下原文件音五,使用https配置惫撰。

mv nginx.conf nginx.conf.bak && cp nginx.https.conf nginx.conf

然后vim nginx.conf,要改的地方很少躺涝,如下:

  server {
    listen 443 ssl;
    # 下面改成你的域名
    server_name docker.zuolan.me;

    # SSL
    # 這里證書地址如果你是letsencrypt申請的不用修改這里
    ssl_certificate /etc/nginx/cert/fullchain.pem;
    ssl_certificate_key /etc/nginx/cert/privkey.pem;

    ...

  server {
    listen 80;
    server_name docker.zuolan.me;
    rewrite ^/(.*) https://$server_name$1 permanent;

第三步:構(gòu)建運(yùn)行

$ cd ~/harbor/Deploy
$ ./prepare
Generated configuration file: ./config/ui/env
Generated configuration file: ./config/ui/app.conf
Generated configuration file: ./config/registry/config.yml
Generated configuration file: ./config/db/env
$ docker-compose up

沒有問題的話已經(jīng)運(yùn)行起來了~~

第四步:測試

現(xiàn)在你可以通過域名pull鏡像了:

docker pull ubuntu
docker tag ubuntu docker.zuolan.me/ubuntu
docker push docker.zuolan.me/ubuntu
docker pull docker.zuolan.me/ubuntu

方法二厨钻、自己搭建(不推薦小白操作)

準(zhǔn)備工作

新建一個文件夾以便管理。

mkdir ~/docker-registry && cd $_
mkdir data nginx && mkdir nginx/certs
vim docker-compose.yml

填寫下面的內(nèi)容到docker-compose.yml:

nginx:
  image: "tutum/nginx"
  ports:
    - 80:80
    - 443:443
  links:
    - registry:registry
  volumes:
    - ./nginx/:/etc/nginx/conf.d
    - /root/app/:/app/
registry:
  image: registry:2
  ports:
    - 127.0.0.1:5000:5000
  environment:
    REGISTRY_HTTP_TLS_CERTIFICATE: /etc/nginx/conf.d/certs/fullchain.pem
    REGISTRY_HTTP_TLS_KEY: /etc/nginx/conf.d/certs/privkey.pem
    REGISTRY_HTTP_SECRET: yourpassword
    REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY: /data
  volumes:
    - ./data:/data
    - ./nginx/:/etc/nginx/conf.d

移動證書到自定義目錄:

cat /etc/letsencrypt/live/docker.zuolan.me/fullchain.pem > ~/docker-registry/nginx/certs/fullchain.pem
cat /etc/letsencrypt/live/docker.zuolan.me/privkey.pem > ~/docker-registry/nginx/certs/privkey.pem

然后配置Nginx文件即可:

vim ~/docker-registry/nginx/registry.conf

域名修改一下坚嗜,復(fù)制粘貼即可夯膀。

upstream docker-registry {
  server registry:5000;
}

server {
  listen 443;
  server_name docker.zuolan.me;

  # SSL
  ssl on;
  ssl_certificate /etc/nginx/conf.d/certs/fullchain.pem;
  ssl_certificate_key /etc/nginx/conf.d/certs/privkey.pem;

  # disable any limits to avoid HTTP 413 for large image uploads
  client_max_body_size 0;

  # required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486)
  chunked_transfer_encoding on;

  location /v2/ {
    # Do not allow connections from docker 1.5 and earlier
    # docker pre-1.6.0 did not properly set the user agent on ping, catch "Go *" user agents
    if ($http_user_agent ~ "^(docker\/1\.(3|4|5(?!\.[0-9]-dev))|Go ).*$" ) {
      return 404;
    }

    # To add basic authentication to v2 use auth_basic setting plus add_header
    # auth_basic "registry.localhost";
    # auth_basic_user_file /etc/nginx/conf.d/registry.password;
    # add_header 'Docker-Distribution-Api-Version' 'registry/2.0' always;

    proxy_pass                          http://docker.zuolan.me;
    proxy_set_header  Host              $http_host;   # required for docker client's sake
    proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
    proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto $scheme;
    proxy_read_timeout                  900;
  }
}

啟動倉庫

cd ~/docker-registry
docker-compose up

測試:

現(xiàn)在你可以通過域名pull鏡像了:

docker pull ubuntu
docker tag ubuntu docker.zuolan.me/ubuntu
docker push docker.zuolan.me/ubuntu
docker pull docker.zuolan.me/ubuntu

官方文檔:Deploying a registry server

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市苍蔬,隨后出現(xiàn)的幾起案子诱建,更是在濱河造成了極大的恐慌,老刑警劉巖碟绑,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件涂佃,死亡現(xiàn)場離奇詭異,居然都是意外死亡蜈敢,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門汽抚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來抓狭,“玉大人,你說我怎么就攤上這事造烁》窆” “怎么了午笛?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長苗桂。 經(jīng)常有香客問我药磺,道長,這世上最難降的妖魔是什么煤伟? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任癌佩,我火速辦了婚禮,結(jié)果婚禮上便锨,老公的妹妹穿的比我還像新娘围辙。我一直安慰自己,他們只是感情好放案,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布姚建。 她就那樣靜靜地躺著,像睡著了一般吱殉。 火紅的嫁衣襯著肌膚如雪掸冤。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天友雳,我揣著相機(jī)與錄音稿湿,去河邊找鬼。 笑死沥阱,一個胖子當(dāng)著我的面吹牛缎罢,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播考杉,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼策精,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了崇棠?” 一聲冷哼從身側(cè)響起咽袜,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎枕稀,沒想到半個月后询刹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡萎坷,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年凹联,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片哆档。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡蔽挠,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出瓜浸,到底是詐尸還是另有隱情澳淑,我是刑警寧澤比原,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站杠巡,受9級特大地震影響量窘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜氢拥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一蚌铜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧兄一,春花似錦厘线、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至骂束,卻和暖如春耳璧,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背展箱。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工旨枯, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人混驰。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓攀隔,卻偏偏與公主長得像,于是被迫代替她去往敵國和親栖榨。 傳聞我的和親對象是個殘疾皇子昆汹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容