利用docker搭建本地私有镜像仓库

主机名 角色
sht-sgmhadoopcm-01 Docker Repository

sht-sgmhadoopnn-01

Docker Client

1. 在两台节点分别安装docker

https://www.cnblogs.com/ilifeilong/p/11687143.html

2. cm-01节点下载docker官方提供的registry镜像

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE


# docker search registry
NAME                                 DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
registry                             The Docker Registry 2.0 implementation for s…   2725                [OK]                
.................

# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
c87736221ed0: Pull complete 
1cc8e0bb44df: Pull complete 
54d33bcb37f5: Pull complete 
e8afc091c171: Pull complete 
b4541f6d3db6: Pull complete 
Digest: sha256:8004747f1e8cd820a148fb7499d71a76d45ff66bac6a29129bfdbfdc0154d146
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
registry            latest              f32a97de94e1        7 months ago        25.8MB

3.cm-01节点新建registry容器

# docker run --detach --publish 5000:5000 --name registry-container --hostname registry registry
191a5a61a411b4ffc3a2edb43c3b675d30c2e52365044333f065297af02b9a81
# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
191a5a61a411        registry            "/entrypoint.sh /etc…"   7 seconds ago       Up 5 seconds        0.0.0.0:5000->5000/tcp   registry-container

查看当前registry镜像仓库,当前并未有任何镜像

# curl -X GET http://sht-sgmhadoopcm-01:5000/v2/_catalog
{"repositories":[]}

 4. 在nn-01节点上模拟客户端上传和下载镜像

新建或修改文件daemon.json,添加如下内容

# cat /etc/docker/daemon.json
{
  "registry-mirror": [
    "https://registry.docker-cn.com"
  ],
  "insecure-registries": [
    "sht-sgmhadoopcm-01:5000"
  ]
}

重启docker服务

# systemctl stop docker
# systemctl start docker

上传镜像

为需要上传的映像文件添加新的tag

# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
centos/mysql             version7            b7138378001a        4 hours ago         4.57GB
cenots/centos7-mysql57   latest              d46f0eee5c07        25 hours ago        7.94GB
centos                   version7.1          babd2f913fc9        45 hours ago        262MB
centos                   version7            0f3e07c0138f        2 weeks ago         220MB

# docker tag centos/mysql:version7 sht-sgmhadoopcm-01:5000/centos/mysql:version7

# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
centos/mysql                           version7            b7138378001a        4 hours ago         4.57GB
sht-sgmhadoopcm-01:5000/centos/mysql   version7            b7138378001a        4 hours ago         4.57GB
cenots/centos7-mysql57                 latest              d46f0eee5c07        25 hours ago        7.94GB
centos                                 version7.1          babd2f913fc9        45 hours ago        262MB
centos                                 version7            0f3e07c0138f        2 weeks ago         220MB

上传镜像至registry

# docker push sht-sgmhadoopcm-01:5000/centos/mysql
The push refers to repository [sht-sgmhadoopcm-01:5000/centos/mysql]
3cd7cc19db60: Pushed 
version7: digest: sha256:e74958454feca52975d3a1ef6f4147bd68dab2c8200d9626ea017f2f5ae7e20b size: 530

在registry查看镜像

# curl -X GET http://sht-sgmhadoopcm-01:5000/v2/_catalog
{"repositories":["centos/mysql"]}

# curl -X GET http://sht-sgmhadoopcm-01:5000/v2/centos/mysql/tags/list
{"name":"centos/mysql","tags":["version7"]}

 下载镜像

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              version7.1          babd2f913fc9        45 hours ago        262MB
centos              version7            0f3e07c0138f        2 weeks ago         220MB

# docker pull sht-sgmhadoopcm-01:5000/centos/mysql:version7
version7: Pulling from centos/mysql
2dedd01a3283: Pull complete 
Digest: sha256:e74958454feca52975d3a1ef6f4147bd68dab2c8200d9626ea017f2f5ae7e20b
Status: Downloaded newer image for sht-sgmhadoopcm-01:5000/centos/mysql:version7
sht-sgmhadoopcm-01:5000/centos/mysql:version7

# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
sht-sgmhadoopcm-01:5000/centos/mysql   version7            b7138378001a        5 hours ago         4.57GB
centos                                 version7.1          babd2f913fc9        45 hours ago        262MB
centos                                 version7            0f3e07c0138f        2 weeks ago         220MB

为了方便使用,将下载下来的镜像重命名

# docker tag sht-sgmhadoopcm-01:5000/centos/mysql:version7 centos/mysql:version7

# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
centos/mysql                           version7            b7138378001a        5 hours ago         4.57GB
sht-sgmhadoopcm-01:5000/centos/mysql   version7            b7138378001a        5 hours ago         4.57GB
centos                                 version7.1          babd2f913fc9        45 hours ago        262MB
centos                                 version7            0f3e07c0138f        2 weeks ago         220MB
# docker rmi sht
-sgmhadoopcm-01:5000/centos/mysql:version7 Untagged: sht-sgmhadoopcm-01:5000/centos/mysql:version7 Untagged: sht-sgmhadoopcm-01:5000/centos/mysql@sha256:e74958454feca52975d3a1ef6f4147bd68dab2c8200d9626ea017f2f5ae7e20b # docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos/mysql version7 b7138378001a 5 hours ago 4.57GB centos version7.1 babd2f913fc9 45 hours ago 262MB centos version7 0f3e07c0138f 2 weeks ago 220MB
原文地址:https://www.cnblogs.com/ilifeilong/p/11708183.html