您好,登錄后才能下訂單哦!
基于BOS怎樣搭建私有Docker Registry,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
Docker Registry 作為 Docker 的核心組件之一負責了鏡像的存儲以及分發(fā)。用戶只需要使用 Docker 的客戶端就可以直接和 Registry 進行交互,下載和上傳鏡像。
百度對象存儲 BOS (Baidu Object Storage) 提供穩(wěn)定、安全、高效以及高擴展存儲服務。
Baidu BOS storage driver 基于官方 Docker Registry 源碼,結(jié)合百度云 Go 語言SDK:https://github.com/guoyao/baidubce-sdk-go.git,通過實現(xiàn) storagedriver.StorageDriver 接口,提供了一個針對百度云 BOS 的 Storage Driver。
安裝 Docker Engine,如何安裝請參考 Docker官方文檔
下載 Registry 鏡像(或者通過源碼倉庫https://github.com/guoyao/distribution.git 自己 build 一個鏡像)
docker pull guoyao/registry:0.6.0
因為是從Docker官方Registry下載鏡像,速度會比較慢,可以配置國內(nèi)的鏡像加速
注冊百度云賬號,開通 BOS 服務,在百度云 BOS 控制臺新建一個 Bucket,假設 Bucket 名稱為 registry-test
獲取百度云 AK / SK
為 Registry 相關的配置新建一個單獨的目錄
mkdir registry && cd registry
在當前目錄下新建我們的賬戶密碼(最簡單的賬戶驗證方式)
mkdir auth htpasswd -Bbn admin 123456 > auth/htpasswd
啟動 Registry
docker run -d \ -v `pwd`/auth:/auth:ro \ -e REGISTRY_AUTH=htpasswd \ -e REGISTRY_AUTH_HTPASSWD_REALM=basic-realm \ -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \ -e REGISTRY_STORAGE=bos \ -e REGISTRY_STORAGE_BOS_ACCESSKEYID=Your_AK \ -e REGISTRY_STORAGE_BOS_ACCESSKEYSECRET=Your_SK \ -e REGISTRY_STORAGE_BOS_REGION=bj \ -e REGISTRY_STORAGE_BOS_BUCKET=registry-test \ -p 5000:5000 \ --restart=always \ guoyao/registry:0.6.0
詳細的 BOS STORAGE 的配置文檔在這里: https://github.com/guoyao/distribution/blob/release/0.6/docs/storage-drivers/bos.md
操作鏡像
Login
docker login localhost:5000
根據(jù)命令行提示輸入之前設置的用戶名:admin,密碼:123456;登錄成功會提示:Login Succeeded
Push
首先通過 Dockerfile 生成一個鏡像,或者修改一個已有鏡像的 tag,比如:localhost:5000/busybox
docker push localhost:5000/busybox
推送成功后就可以在百度云 BOS 控制臺查看到這個鏡像了
Pull
docker pull localhost:5000/busybox
上述方式只能通過 localhost 來 push 和 pull 鏡像,如果想通過公網(wǎng) 域名 或 IP,需要配置 --insecure-registry xxx.xxx.xxx.xxx 到 Docker Deamon 的啟動參數(shù)中,并重啟 Docker Deamon,否則會報錯:
Error response from daemon: Get https://xxx.xxx.xxx.xxx:5000/v1/users/: http: server gave HTTP response to HTTPS client
如果不希望配置 --insecure-registry 參數(shù),你需要購買一個獨立的域名(假設為 myregistrydomain.com),并且申請該域名的 https 證書,將證書 xxx.crt 和 xxx.key 文件放在當前 certs 子目錄下,然后在啟動 Registry 時,配置證書:
docker run -d \ -v `pwd`/auth:/auth:ro \ -e REGISTRY_AUTH=htpasswd \ -e REGISTRY_AUTH_HTPASSWD_REALM=basic-realm \ -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \ -v `pwd`/certs:/certs:ro \ -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/xxx.crt \ -e REGISTRY_HTTP_TLS_KEY=/certs/xxx.key \ -e REGISTRY_STORAGE=bos \ -e REGISTRY_STORAGE_BOS_ACCESSKEYID=Your_AK \ -e REGISTRY_STORAGE_BOS_ACCESSKEYSECRET=Your_SK \ -e REGISTRY_STORAGE_BOS_REGION=bj \ -e REGISTRY_STORAGE_BOS_BUCKET=registry-test \ -p 443:5000 \ --restart=always \ guoyao/registry:0.6.0
既然已經(jīng)使用 https 證書了,我們把對外暴露的端口換成 443 端口,然后就可以直接通過域名來操作鏡像了:
docker login myregistrydomain.com docker push myregistrydomain.com/busybox docker pull myregistrydomain.com/busybox
更多關于部署 Registry 的內(nèi)容,可以參考官方文檔:https://docs.docker.com/registry/deploying/
因為要啟動多個容器,這里我們使用 docker-compose 來做容器編排,首先安裝 docker-compose(需要使用 root 用戶來安裝),安裝方式如下:
curl -L https://github.com/docker/compose/releases/download/1.10.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose
創(chuàng)建 nginx 主配置文件
cat > auth/nginx.conf << 'EOF' events { worker_connections 1024; } http { upstream docker-registry { server registry:5000; } ## Set a variable to help us decide if we need to add the ## 'Docker-Distribution-Api-Version' header. ## The registry always sets this header. ## In the case of nginx performing auth, the header will be unset ## since nginx is auth-ing before proxying. map $upstream_http_docker_distribution_api_version $docker_distribution_api_version { '' 'registry/2.0'; } server { listen 443 ssl; server_name myregistrydomain.com; # SSL ssl_certificate /certs/xxx.crt; ssl_certificate_key /certs/xxx.key; # Recommendations from https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html ssl_protocols TLSv1.1 TLSv1.2; ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH'; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; # 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. auth_basic "Registry realm"; auth_basic_user_file /etc/nginx/conf.d/htpasswd; ## If $docker_distribution_api_version is empty, the header will not be added. ## See the map directive above where this variable is defined. add_header 'Docker-Distribution-Api-Version' $docker_distribution_api_version always; proxy_pass http://docker-registry; 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; } } } EOF
創(chuàng)建 docker-compose.yml 文件
nginx: image: nginx:1.9 ports: - 443:443 links: - registry:registry volumes: - ./auth:/etc/nginx/conf.d:ro - ./auth/nginx.conf:/etc/nginx/nginx.conf:ro - ./certs:/certs:ro registry: image: guoyao/registry:0.6.0 environment: - REGISTRY_STORAGE=bos - REGISTRY_STORAGE_BOS_ACCESSKEYID=Your_AK - REGISTRY_STORAGE_BOS_ACCESSKEYSECRET=Your_SK - REGISTRY_STORAGE_BOS_REGION=bj - REGISTRY_STORAGE_BOS_BUCKET=registry-test
啟動容器
docker-compose up -d
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。