docker创建私有仓库

由于网速和大中华局域网效果,使得我们在DockerHub下载镜像的速度很慢,甚至一些国内的镜像仓库,也感觉速度不是很好。所以,很有必要在本地或者一个我们访问很快速的地方(自己的云服务器)搭建一套镜像仓库。有了这样一个仓库,不仅可以提高下载速度,而且可以增加我们个性化定制的镜像,以备后续使用。这篇将介绍怎样搭建本地镜像。

话外篇,配置docker代理

如果有一个很快的代理,当然也可以直接通过配置docker代理,以达到快速下载镜像的目的。在搭建本地镜像的过程也需要从DockerHub上下载完整镜像文件,如果访问缓慢的话,下载将会非常缓慢。设置代理的方法非常简单,这里以CentOS6.5为例。

找到/etc/default/docker,打开编辑,找到下面export http_proxy部分,去掉注释并修改为自己的代理即可。

# Docker Upstart and SysVinit configuration file

# Customize location of Docker binary (especially for development testing).
#DOCKER="/usr/local/bin/docker"

# Use DOCKER_OPTS to modify the daemon startup options.
#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"

# If you need Docker to use an HTTP proxy, it can also be specified here.
export http_proxy="http://xxxx:port"

# This is also a handy place to tweak where Docker's temporary files go.
#export TMPDIR="/mnt/bigdrive/docker-tmp"
DOCKER_OPTS="--insecure-registry dl.dockerpool.com:5000"

创建私有仓库

本文记录以Docker官方提供的镜像Registry 创建本地私有仓库,创建方式和启动一个普通镜像的方式是一样。

1.在私有仓库服务器快速创建镜像仓库,运行如下代码:

docker run -p 5000:5000 registry:2.0

运行上述命令后,会从DockerHub上拉取registry镜像并在本地启动Registry服务,并监听5000端口。 

2.列出本地镜像

docker images

  

可以看到registry的镜像和一个本地ubuntu:12.04的镜像

3.重新标记一个本地镜像为私有仓库的版本,这里将本地的ubuntu 12.04标记为localhost:5000/ubuntu:1204。

docker tag ubuntu:12.04 localhost:5000/ubuntu:1204

再次查看镜像可以看到多了一个标记为localhost:5000/ubuntu:1204的镜像

4.将本地镜像推送到本地仓库中

docker push localhost:5000/ubuntu:1204

5.查看本地仓库中的镜像列表

curl http://localhost:5000/v2/ubuntu/tags/list

结果如下:

{"name":"ubuntu","tags":["1204"]}

 6.从本地仓库拉取一个镜像,在这之前先执行如下命令移除本地未使用的镜像,保证从本地仓库拉取的镜像不是从缓存中获取。

 docker rmi -f $(docker images -q -a )

之后再查看镜像,只剩下registry这个镜像

拉取本地仓库中的镜像

docker pull localhost:5000/ubuntu:1204

Unable to find image 'localhost:5000/ubuntu:1204' locally
1204: Pulling from localhost:5000/ubuntu
b796a17a2688: Pull complete 
273721eafe54: Pull complete 
7dd38dbb5eda: Pull complete 
32190de3770a: Already exists

之后查看镜像如下:

  

最后正常启动

docker run --name mytestubuntu localhost:5000/ubuntu:1204 

欢迎关注公众号「gushidefengzheng」古时的风筝

原文地址:https://www.cnblogs.com/fengzheng/p/5168951.html