《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 registry"forimagein"registry" for image in "@”
do
echo_b “Uploading $image…”
sudo docker tag $image registry/registry/image
sudo docker push registry/registry/image
sudo docker rmi 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 registry&quot;forimagein&quot;registry&quot; for image in &quot;@”
do
echo_b “Downloading $image…”
sudo docker pull registry/registry/image
sudo docker tag registry/registry/image $image
sudo docker rmi 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

posted @ 2019-04-02 17:03  thewindkee  阅读(113)  评论(0编辑  收藏  举报