docker学习笔记——镜像仓库制作(公有+私有+harbor)(代码片段)

爱敲代码的三毛 爱敲代码的三毛     2022-12-02     661

关键词:

文章目录


一、官方仓库

  • 公有仓库
  • 私有仓库

1.官方镜像仓库

公有的官方镜像仓库dockerhub

1) web页面登录

2) Linux命令行登录

[root@docker ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: hehayu # 用户
Password: 			# 密码
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
退出登录
[root@docker ~]# docker logout
Removing login credentials for https://index.docker.io/v1/

2. dockerhup镜像上传、下载

1) 镜像上传

我们从dockerhub上下载的公开镜像是不能直接上传的,要先tag(打标签,类似于重新指定路径并命名)

[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    5d0da3dc9764   12 months ago   231MB
[root@docker ~]# docker tag centos:latest hehayu/centos:v1
[root@docker ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
hehayu/centos   v1        5d0da3dc9764   12 months ago   231MB
centos          latest    5d0da3dc9764   12 months ago   231MB

把已打标记的容器镜像上传到公有仓库

[root@docker ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: hehayu
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@docker ~]# docker push hehayu/centos:v1
The push refers to repository [docker.io/hehayu/centos]
74ddd0ec08fa: Mounted from library/centos 
v1: digest: sha256:a1801b843b1bfaf77c501e7a6d3f709401a1e0c83863037fa3aab063a7fdb9dc size: 529

2) 镜像下载

可以先删除掉原来的镜像

[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@docker ~]# docker pull hehayu/centos:v1
v1: Pulling from hehayu/centos
a1d0c7532777: Already exists 
Digest: sha256:a1801b843b1bfaf77c501e7a6d3f709401a1e0c83863037fa3aab063a7fdb9dc
Status: Downloaded newer image for hehayu/centos:v1
docker.io/hehayu/centos:v1
[root@docker ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
hehayu/centos   v1        5d0da3dc9764   12 months ago   231MB

3. 镜像加速器

1) 阿里云加速器

修改/etc/docker/daemon.json

重启服务

systemctl daemon-reload
systemctl restart docker

二、docker本地容器镜像仓库

  • 在局域内使用
  • 方便与其它系统进行集成
  • 上传下载大镜像时

环境准备

2台CentOS机器,都安装docker环境。

一台用来测试参仓库下载,一台安装registry做镜像仓库。

要在同一网段,关闭防火墙SELinux

1.使用registry容器镜像实现本地非安全镜像仓库

1) 下载registry容器镜像

 [root@registry ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
79e9f2f55bf5: Pull complete 
0d96da54f60b: Pull complete 
5b27040df4a2: Pull complete 
e2ead8259a04: Pull complete 
3790aef225b9: Pull complete 
Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@registry ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
registry     latest    b8604a3fe854   10 months ago   26.2MB

2) 创建用于挂载至registry镜像启动的仓库中,便于容器镜像持久保存

[root@registry ~]# mkdir /opt/dockerregistry

3) 启动容器获取镜像仓库

  • –restart=always:一种重启策略,假如物理机关机重启,容器也会自动重启,不用手动重启
  • /var/lib/registr :registry指定要挂载到的目录
[root@registry ~]# docker run -d -p 5000:5000 --restart=always -v /opt/dockerregistry:/var/lib/registry registry:latest
c9b86d83720fe550142063fdebc87adadd64a4d6840c8d471399f2d5726233db
[root@registry ~]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS         PORTS                                       NAMES
c9b86d83720f   registry:latest   "/entrypoint.sh /etc…"   7 seconds ago   Up 5 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   nostalgic_cerf

4) 验证是否可用

[root@registry ~]# curl http://192.168.44.120:5000/v2/_catalog
"repositories":[]
 [root@registry ~]# vim /etc/docker/daemon.json
 
 "insecure-registries": ["http://192.168.44.120:5000"]
 
 
 [root@registry ~]# systemctl restart docker
 
给镜像打标记
[root@registry ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
registry     latest    b8604a3fe854   10 months ago   26.2MB
centos       latest    5d0da3dc9764   12 months ago   231MB
[root@registry ~]# docker tag centos:latest 192.168.44.120:5000/centos:v1

[root@registry ~]# docker images
REPOSITORY                   TAG       IMAGE ID       CREATED         SIZE
registry                     latest    b8604a3fe854   10 months ago   26.2MB
192.168.44.120:5000/centos   v1        5d0da3dc9764   12 months ago   231MB
centos                       latest    5d0da3dc9764   12 months ago   231MB

上传到仓库

[root@registry ~]# docker push 192.168.44.120:5000/centos:v1
The push refers to repository [192.168.44.120:5000/centos]
74ddd0ec08fa: Pushed 
v1: digest: sha256:a1801b843b1bfaf77c501e7a6d3f709401a1e0c83863037fa3aab063a7fdb9dc size: 529

[root@registry ~]# ls /opt/dockerregistry/
docker
[root@registry ~]# ls /opt/dockerregistry/docker/
registry
[root@registry ~]# ls /opt/dockerregistry/docker/registry/
v2
[root@registry ~]# ls /opt/dockerregistry/docker/registry/v2/
blobs  repositories
[root@registry ~]# ls /opt/dockerregistry/docker/registry/v2/repositories/
centos

5) 在其它主机中使用此镜像仓库

第一步修改:/usr/lib/systemd/system/docker.service ,容器间互联(–link)

[root@docker ~]# vim /usr/lib/systemd/system/docker.service

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd 
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

第二步创建:/etc/docker/daemon.json

# vim /etc/docker/daemon.json 
添加镜像仓库主机ip
"insecure-registries": ["http://192.168.122.33:5000"]

第三步:重启

[root@docker ~]# systemctl daemon-reload;systemctl restart docker

第四步:测试下载

[root@docker ~]# docker pull 192.168.44.120:5000/centos:v1
v1: Pulling from centos
a1d0c7532777: Pull complete 
Digest: sha256:a1801b843b1bfaf77c501e7a6d3f709401a1e0c83863037fa3aab063a7fdb9dc
Status: Downloaded newer image for 192.168.44.120:5000/centos:v1
192.168.44.120:5000/centos:v1
[root@docker ~]# docker images
REPOSITORY                   TAG       IMAGE ID       CREATED         SIZE
192.168.44.120:5000/centos   v1        5d0da3dc9764   12 months ago   231MB

2. 使用Harbor实现本地通过web进行管理的非安全仓库

  • vmware公司开源
  • 良好的中文界面
  • web管理界面
  • 使用广泛

环境准备

1) 工具准备

  • 使用docker-compose工具进行启动

在harbor机器上安装并启动docker


# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# yum repolist
# yum -y install docker-ce
# systemctl start docker

再安装 compose

[root@harhor ~]# curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 11.6M  100 11.6M    0     0  2568k      0  0:00:04  0:00:04 --:--:-- 3427k
将可执行权限应用于二进制文件
[root@harhor ~]# chmod +x /usr/local/bin/docker-compose
创建指向/usr/bin或路径中任何其他目录的符号链接
[root@harhor ~]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
[root@harhor ~]# docker-compose version
docker-compose version 1.27.4, build 40524192
docker-py version: 4.3.1
CPython version: 3.7.7
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

查看版本
docker-compose version

2) 获取harbor

harbor下载地址

我这里是tar xf harbor-offline-installer-v1.10.14.tgz版本

[root@harhor ~]# tar xf harbor-offline-installer-v1.10.14.tgz
[root@harhor ~]# cd harbor
[root@harhor harbor]# vim harbor.yml
把这一项修改为本机ip
hostname: 192.168.44.110
默认的admin密码
harbor_admin_password: Harbor12345

3) 启动

# ./install.sh
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating harbor-portal ... done
Creating harbor-db     ... done
Creating redis         ... done
Creating registry      ... done
Creating registryctl   ... done
Creating harbor-core   ... done
Creating harbor-jobservice ... done
Creating nginx             ... done
✔ ----Harbor has been installed and started successfully.----

在浏览器上访问http://本机ip

公开项目: 下载镜像不需要docker login登录,但上传镜像还是需要docker
login登录
私有项目: 都需要docker login登录才以上传下载

4) 镜像上传下载操作

在docker主机下载一个随便下一个镜像测试,并打上标记

[root@docker ~]# docker pull centos         
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    5d0da3dc9764   12 months ago   231MB

[root@docker ~]# docker tag centos:latest 192.168.44.110/test/centos-test:v1             

[root@docker ~]# docker images
REPOSITORY                        TAG       IMAGE ID       CREATED         SIZE
192.168.44.110/test/centos-test   v1        5d0da3dc9764   12 months ago   231MB
centos                            latest    5d0da3dc9764   12 months ago   231MB

在docker主机上安装docker,并修改配置文件

因为docker用https通讯,所以还需要做证书,太麻烦。
配置"insecure-registries": [“harbor服务器IP”]来使用http通讯

[root@docker ~]# vim /etc/docker/daemon.json

        "insecure-registries": ["http://192.168.44.110"]

重启docker
# systemctl restart docekr

上传镜像

[root@docker ~]# docker login http://192.168.44.110
Username: admin
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@docker ~]# docker push 192.168.44.110/test/centos-test:v1
The push refers to repository [192.168.44.110/test/centos-test]
74ddd0ec08fa: Pushed 
v1: digest: sha256:a1801b843b1bfaf77c501e7a6d3f709401a1e0c83863037fa3aab063a7fdb9dc size: 529

回到网页查看


docker学习笔记-仓库(代码片段)

Docker仓库是镜像存储、分发、部署的关键,制作好应用程序镜像后上传到仓库,使用Dockerdaemon从仓库拉取后运行,我们可以使用官方共有仓库dockerhub或者搭建私有仓库DockerHub包含众多镜像,无需登录就可以搜索和使用注册DockerHub... 查看详情

企业运维实战--最全docker学习笔记1.docker简介安装部署镜像构建dockerfile详解镜像构建镜像优化本地私有仓库搭建(代码片段)

企业运维实战--Docker学习笔记1.Docker简介、安装部署、镜像构建、Dockerfile详解、镜像构建、镜像优化、本地私有仓库搭建前言--Docker简介一、Docker安装部署二、镜像的构建三、Dockerfile详解四、镜像构建--nginx五、镜像优化六、本地... 查看详情

docker学习笔记六:docker搭建企业级私有仓库

前言Docker不仅是一个强大的服务器部署工具,而且它还有一个官方的DockerHubregistry用于储存Docker镜像。上传镜像到DockerHub是免费的,上传的镜像文件同时也对公共领域开放,而这可能不是我们想要的。本文介绍如何配置一个私有... 查看详情

centos7构建docker私有镜像仓库(代码片段)

...库来完成。镜像仓库分为公有仓库和私有仓库两种,例如DockerHub就属于公有仓库,公有仓库的优点是可以直接使用,无须自己维护。但考虑到访问效率和镜像安全等方面的原因,企业可构建自己内部的私有仓库,供内部员工上传... 查看详情

docker学习笔记——私有仓库部署

1、系统环境:CentOS7.3  172.16.0.44 registry 已安装docker  172.16.0.45 client  已安装docker2、未加密仓库部署  下载registry镜像  docker pull registry& 查看详情

docker学习笔记之创建私有仓库

Docker的使用越来越多,在实际应用中,我们可能不止一台服务器,也可能不只是在同一个云上,那么面对同一个镜像,要部署到不同的云,不同的服务器,有什么便捷的方式呢?当然,有同学可能会说使用官方仓库的。这个固然... 查看详情

docker私有仓库以及dockershell

Docker搭建私有仓库公有云:比如百度云,dockerhub私有云:比如搭建到某个内网,docker搭建私有仓库:下载一个镜像docker默认使用的是dockerhubdocker仓库服务器就是docker注册服务器//注意dockerpull和dockerspush的区别,如将docker镜像上传... 查看详情

docker建立私有仓库(代码片段)

Docker(五)建立私有仓库随着创建的镜像日益增多,就需要一个保存镜像的地方,这就是仓库。仓库目前有两张:1:公有仓库2:私有仓库私有仓库建立[[email protected]~]dockerpullregistryUsingdefaulttag:latestlatest:Pullingfromlibrary/registry4... 查看详情

4docker仓库

...的镜像2、仓库又分为公有仓库和私有仓库3、命令行登录docker官方维护的公有仓库()https://hub.docker.com/前提是您必须提前注册有账户名和 查看详情

docker学习记录-构建私有镜像仓库-harbor(代码片段)

Docker学习记录-构建私有镜像仓库-harbor一、前置条件安装docker安装docker-compose注意:harbor对docker要求版本比较高,请安装最新版本 二、下载harbor安装包download  中 harbor-offline-installer对应的版本。 三、上传到服务... 查看详情

docker镜像制作服务编排私有仓库

DockerNginx部署Redis部署Dockerfile镜像制作容器转为镜像dockerfileDocker服务编排DockerComposeDockerCompose安装使用dockercompose编排nginx+springboot项目Docker私有仓库私有仓库搭建将镜像上传至私有仓库Nginx部署案例:需求在Docker容器中部署... 查看详情

docker学习笔记——镜像容器仓库

Docker三个基本概念镜像(image)容器(container)仓库(Repository)镜像(image)搜索镜像docker search nginx获取镜像docker pull nginx指定Registry地址和具体的仓库名下载镜像,没有指定Registry地址,默认从DockerHub上下载docker&... 查看详情

docker学习笔记6

VIIIdocker​目录​​​虚拟化:41​​​​​dockerobjects:43​​​​​dockerimages:46​​​​​dockerregistry:48​​​docker:​介绍;​镜像管理;​容器管理;​网络访问;​数据管理;​镜像构建;​私有仓库;​核心技术;​... 查看详情

doceker学习------docker私有仓库的搭建

192.168.138.102:23451.私有仓库的搭建(dockerpullregistry),拉取最新的镜像2.查看拉取的仓库镜像(dockerimages)3.启用registry镜像(dockerrun-d-v/opt/registry:/var/lib/registry-p5000:5000--restart=always--nameregistryregistry)4.查看仓库是否启 查看详情

docker容器学习梳理--私有仓库registry使用

 但有时候使用DockerHub这样的公共仓库可能不方便,这种情况下用户可以使用registry创建一个本地仓库供私人使用,这点跟Maven的管理类似。使用私有仓库有许多优点:1)节省网络带宽,针对于每个镜像不用每个人都去中央仓... 查看详情

011.docker仓库管理(代码片段)

一 Docker仓库介绍docker仓库,即registry,实现了镜像的管理、分发,同时还包括用户的认证。dockerregistry仓库是一个无状态的、高可靠的服务器应用程序,用来存储docker镜像。docker.io为docker官方的仓库,默认所有的pull均是从官方仓... 查看详情

docker私有仓库搭建

由于公有仓库有时连接会出现超时,下载速度慢等情况故搭建私有仓库镜像server端可以login官方的DokerHub,可以pull,push和私有仓库但client只能操作自己搭建的仓库server    192.168.127.142client     192.168.... 查看详情

docker学习笔记(6-1)docker镜像与docker仓库

学习目标:  列出镜像 dockerimages  删除镜像dockerrmi   docker镜像:联合加载技术aufs实现的层叠的只读文件系统1、#使用dockerinfo查看存储驱动和存储位置存储位置:/var/lib/dockersudols-ls/var/docker/aufs  diff  layers  ... 查看详情