1. 程式人生 > >配置 Docker 映象下載的本地 mirror 服務

配置 Docker 映象下載的本地 mirror 服務

Docker registry 工具現在已經很好的支援了 mirror 功能,使用它可以配置一個本地的 mirror 服務,將 pull 過的映象 cache 在本地,這樣其它主機再次 pull 的時候會極大提高響應速度。

使用 docker-compose 啟動 registry mirror 服務

以 ubuntu 為例,首先要安裝 docker 和 docker-compose。

安裝 docker

$ sudo wget -qO- https://get.docker.com/ | sh

安裝 docker-compose

$ sudo pip install docker-compose

之後,在本地建立 /opt/data/registry 目錄,作為映象檔案的儲存位置;建立 /opt/data/redis 目錄,作為 redis 資料的存放位置。

編寫一個 docker-compose.yml 檔案。

該檔案將啟動一個 registry 容器監聽在本地的 5000 埠,並使用一個 redis 容器作為小檔案的 cache。

內容如下:

# This compose file will start 2 containers: registry and redis.
# registry container will listen on host port 5000,
# and depend on the redis container as the cache scheme.
registry: image: registry:latest cpu_shares: 10 environment: - STANDALONE=false - MIRROR_SOURCE=https://registry-1.docker.io - MIRROR_SOURCE_INDEX=https://index.docker.io - CACHE_REDIS_HOST=redis - CACHE_REDIS_PORT=6379 - DEBUG=false hostname
: docker-registry links: - redis:redis mem_limit: 512m ports: - "5000:5000" privileged: false restart: always user: root volumes: - /opt/data/registry:/tmp/registry redis: image: redis:3.0 cpu_shares: 10 expose: - "6379" mem_limit: 512m restart: always volumes: - /opt/data/redis:/data

之後,啟動服務。

$ docker-compose up -d

配置主機使用 mirror 服務

在其它主機上,配置 docker 的配置檔案(例如 /etc/default/docker),新增一行:

DOCKER_OPTS="$DOCKER_OPTS --registry-mirror http://localmirror:5000"

其中 localmirror 替換為剛才配置了 mirror 服務的機器地址。

之後重啟 docker 服務。

$ sudo service docker restart

測試

隨便下載一個映象,比如 ubuntu:14.04,正常需要十幾分鍾。

刪除下載的映象,再次下載,一分鐘不到,就下載完畢了。