前提条件:
- 服务器已经安装Docker(我的服务器是CentOS 7)
- 服务器已经安装Docker Compose
满足以上条件时就可以开始搭建了:
1. 生成用户密码文件:(运行下面命令后会在当前目录的auth目录下生成文件htpasswd,用户名:bounter,密码:111111)
$ mkdir auth $ mkdir data $ docker run --entrypoint htpasswd registry:2 -Bbn bounter 111111 > auth/htpasswd
2. 当前目录添加 docker-compose.yml,内容如下:(注意把挂载的路径改成你服务器的实际路径)
registry: restart: always image: registry:2 ports: - 5000:5000 environment: REGISTRY_AUTH: htpasswd REGISTRY_AUTH_HTPASSWD_PATH: /auth/htpasswd REGISTRY_AUTH_HTPASSWD_REALM: Registry Realm volumes: - /home/simon/docker-registry/data:/var/lib/registry - /home/simon/docker-registry/auth:/auth
3. 启动:docker-compose up -d
4. 客户端设置允许http不安全访问私有仓库:(vi /usr/lib/systemd/system/docker.service )
ExecStart=/usr/bin/dockerd --insecure-registry localhost:5000 systemctl daemon-reload systemctl restart docker
5. 操作私有仓库(加上认证之后必须先登录才能推送和拉取)
1)登录:docker login localhost:5000
2)推送:
docker tag openjdk:8 localhost:5000/openjdk:8
docker push localhost:5000/openjdk:8
3)拉取:docker pull localhost:5000/openjdk:8
4)查看:curl --user bounter:111111 localhost:5000/v2/_catalog