《Docker技术入门与实践》Docker入门5 使用私有仓库

基于容器运行

  1. 拉取registry容器

    sudo docker pull registry
    在这里插入图片描述

  2. 运行registry容器被指定仓库路径

    sudo docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry registry

  3. 将一个容器标记为私库的版本

    docker tag docker.io/hello-world:latest xx:5000/hello-world:v1

在这里插入图片描述

  1. 推送到私有仓库

    docker push xx:5000/hello-world:v1

报https错误的话, 需要修改 /etc/docker/daemon.json , 将仓库地址添加进去。
在这里插入图片描述

  1. 访问http://xx:5000/v2/_catalog查看仓库内的镜像

    {
    “repositories”: [
    “hello-world”
    ]
    }

  2. 其他机器使用docker pull xx:5000/hello-world:v1 拉取即可。如果https错误,修改/etc/docker/daemon.json 同上。

  3. 添加用户认证
    这里可以通过nginx来做简单验证。
    重新配置registry,设置为 127.0.0.1:5000,让其只能通过内网访问,再让nginx反向代理到127.0.0.1:5000.
    nginx配置:

     upstream docker-registry{
     server 127.0.0.1:5000;
     }
     server {
         listen 500;
         proxy_set_header Host $http_host;
         proxy_set_header X-Real-IP $remote_addr;
     
     location /{
       auth_basic "Please input username/password";
       auth_basic_user_file docker-registry-htpasswd;
       proxy_pass http://docker-registry;
     }   
    

    }

通过htpasswd设置密码。
安装htpasswd yum -y install httpd ,添加用户密码

htpasswd  /usr/local/nginx/conf/docker-registry-htpasswd user1
....
New password: 
Re-type new password:

重启nginx
在这里插入图片描述

效果
在这里插入图片描述

  1. 批量上传镜像到私有仓库

  2. 仓库配置文件

创建脚本
vim push.sh
编辑脚本内容
#!/bin/bash
# This script will upload the given local images to a registry server ($registry is the default value).
# Usage: push_images image1 [image2…]
# Author: Mongo
# Create: 2016-05-26
#The registry server address where you want push the images into(Please instead of your private registry’s domain name)
registry=willem.top:6666
### DO NOT MODIFY THE FOLLOWING PART, UNLESS YOU KNOW WHAT IT MEANS. YOU CAN ALSO LEARN IT FROM HERE.###
echo_g () {
[ $# -ne 1 ] && return 0
echo -e “\033[32m$1\033[0m”
}
echo_b () {
[ $# -ne 1 ] && return 0
echo -e “\033[34m$1\033[0m”
}
usage() {
sudo docker images
echo “Usage: $0 registry1:tag1 [registry2:tag2…]”
}
[ $# -lt 1 ] && usage && exit
echo_b “The registry server is r e g i s t r y " f o r i m a g e i n " registry" for image in " registry"forimagein"@”
do
echo_b “Uploading $image…”
sudo docker tag $image r e g i s t r y / registry/ registry/image
sudo docker push r e g i s t r y / registry/ registry/image
sudo docker rmi r e g i s t r y / registry/ registry/image
echo_g "Push KaTeX parse error: Expected 'EOF', got '#' at position 67: ….sh 编辑该脚本的内容 #̲!/bin/sh # Thi…registry is the default value).
# Usage: push_all
# Author: Mongo
# Create: 2016-05-26
for image in sudo docker images|grep -v "REPOSITORY"|grep -v "<none>"|awk '{print $1":"$2}'
do
./push.sh KaTeX parse error: Expected 'EOF', got '#' at position 50: ….sh 编辑该脚本的内容 #̲!/bin/bash # T…registry is the default value).
# Usage: push_images image1 [image2…]
# Author: Mongo
# Create: 2014-05-26
#The registry server address where you want push the images into
registry=willem.top:6666
### DO NOT MODIFY THE FOLLOWING PART, UNLESS YOU KNOW WHAT IT MEANS ###
echo_g () {
[ $# -ne 1 ] && return 0
echo -e “\033[32m$1\033[0m”
}
echo_b () {
[ $# -ne 1 ] && return 0
echo -e “\033[34m$1\033[0m”
}
usage() {
sudo docker images
echo “Usage: $0 registry1:tag1 [registry2:tag2…]”
}
[ $# -lt 1 ] && usage && exit
echo_b “The registry server is r e g i s t r y &quot; f o r i m a g e i n &quot; registry&quot; for image in &quot; registry"forimagein"@”
do
echo_b “Downloading $image…”
sudo docker pull r e g i s t r y / registry/ registry/image
sudo docker tag r e g i s t r y / registry/ registry/image $image
sudo docker rmi r e g i s t r y / registry/ registry/image
echo_g “Download $image Success!”
done
给三个脚本可执行的权限
chmod +x ./push.sh ./pushall.sh ./pull.sh
执行脚本
# 上传指定镜像(可上传多个,中间用空格隔开)
./push.sh ImageName[:TagName] [ImageName[:TagName] ···]
# 例如:./push.sh busybox:latest ubutnu:14.04
# 上传所有镜像
./pushall.sh
# 下载指定镜像(可上传多个,中间用空格隔开)
./pull.sh ImageName[:TagName] [ImageName[:TagName] ···]
# 例如:./pull.sh busybox:latest ubutnu:14.04

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值