温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何搭建私有镜像仓库

发布时间:2021-10-14 14:15:33 来源:亿速云 阅读:167 作者:iii 栏目:编程语言

这篇文章主要介绍“如何搭建私有镜像仓库”,在日常操作中,相信很多人在如何搭建私有镜像仓库问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何搭建私有镜像仓库”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

Docker容器

什么是docker

docker是一个的容器实现,包括三个基本概念:

  • 镜像(Image): Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。

  • 容器(Container): 镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。

  • 仓库(Repository): 仓库可看成一个代码控制中心,用来保存镜像。 如何搭建私有镜像仓库

开启远程访问

docker开启远程访问有两种方式:

Configuring remote access with systemd unit file

编辑docker.service,添加-H fd:// -H tcp://127.0.0.1:2375启动参数,然后重新加载配置并重启

 $ sudo systemctl daemon-reload
 $ sudo systemctl restart docker.service
Configuring remote access with daemon.json

修改docker进程配置文件,添加hosts。(本文采用的方式)

  1. Set the hosts array in the /etc/docker/daemon.json to connect to the UNIX socket and an IP address, as follows:

{
  "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"]
}
  1. Restart Docker.

  2. Check to see whether the change was honored by reviewing the output of netstat to confirm dockerd is listening on the configured port.

$ sudo netstat -lntp | grep dockerd
tcp        0      0 127.0.0.1:2375          0.0.0.0:*               LISTEN      3758/dockerd

systemd vs daemon.json Configuring Docker to listen for connections using both the systemd unit file and the daemon.json file causes a conflict that prevents Docker from starting.

客户端访问远程docker进程需要配置环境变量:
DOCKER_HOST=tcp://172.31.0.250:2375

搭建私有镜像仓库

镜像仓库是集中存放docker镜像的地方,Docker Hub就是一个公共的镜像仓库。而在公司内使用,我们需要一个私有的仓库。 可以参照https://github.com/Quiq/docker-registry-ui中的example方案进行部署。该部署方案使用docker-compose,共包含三部分:

  1. register 最重要的镜像仓库服务

  2. register-ui 仓库的web ui

  3. httpd 前置http服务

docker-compose.yml文件如下:

version: "2.3"

services:
  httpd:
    image: httpd:2.4
    ports:
      - "80:80"
    volumes:
      - "./config/httpd.conf:/usr/local/apache2/conf/httpd.conf:ro"

  registry:
    image: registry:2
    ports:
      - "5000"
    volumes:
      - "./data/registry:/var/lib/registry"
    healthcheck:
      test: ["CMD", "wget", "-S", "localhost:5000/v2/"]
      interval: 5s
      timeout: 10s

  registry-ui:
    image: quiq/docker-registry-ui:latest
    ports:
      - "8000"
    volumes:
      - "./data/registry-ui:/opt/data"
      - "./config/registry-ui.yml:/opt/config.yml:ro"
    depends_on:
      registry:
        condition: service_healthy

从配置文件可以看到,该方案使用volumes把需要保存的数据映射到宿主机,配置文件放在./config下,数据文件放到./data下。

如果没有特殊要求,可以直接使用示例中的registry-ui.yml和httpd.conf配置文件,无需任何修改。使用docker-compose命令启动。

$ docker-compose up -d

启动后可以访问80端口的/ui进入web界面(本例中因为服务运行在本地,所以使用localhost)。 如何搭建私有镜像仓库

80端口的/v2请求,将被转发到镜像服务(http://registry:5000/v2)上。通过打上tag,就可以push到私服仓库了。

docker tag getting-started localhost/getting-started

如果是远程服务器,需要在客户端修改docker配置文件,增加私服的insecure配置:

$ cat /etc/docker/daemon.json
{ 
    "insecure-registries":["docker-hub.jc.com"] 
}

否则会遇到x509异常:

x509: certificate is valid for ingress.local, not docker-hub.jc.com

踩坑记

因为我使用rancher的ingress进行转发,结果在push的时候遇到

error parsing HTTP 413 response body: invalid character '<' looking for beginning of value: "<html>\r\n<head><title>413 Request Entity Too Large</title></head>\r\n<body>\r\n<center><h2>413 Request Entity Too Large</h2></center>\r\n<hr><center>nginx/1.19.2</center>\r\n</body>\r\n</html>\r\n"

解决办法是在ingress的annotations中设置大小,我这里设置了1g:

annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: "1g"

制作docker镜像文件

制作docker镜像文件有两种方式:

  1. 使用dockerfile进行构建

$ docker build -f /path/to/a/Dockerfile .

dockerfile示例:

FROM tomcat:8-jdk8
COPY target/pop-seller.war /usr/local/tomcat/webapps/ROOT.war
  1. 使用docker commit命令进行构建

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS              NAMES
c3f279d17e0a        ubuntu:12.04        /bin/bash           7 days ago          Up 25 hours                            desperate_dubinsky
197387f1b436        ubuntu:12.04        /bin/bash           7 days ago          Up 25 hours                            focused_hamilton

$ docker commit c3f279d17e0a  svendowideit/testimage:version3
f5283438590d

$ docker images
REPOSITORY                        TAG                 ID                  CREATED             SIZE
svendowideit/testimage            version3            f5283438590d        16 seconds ago      335.7 MB

Generally, it is better to use Dockerfiles to manage your images in a documented and maintainable way.

实践

POP项目中的实践:

docker run -p 8080:3001 --add-host=pop-seller:172.31.98.102 -v /host/path/logs:/data/logs --name pop-seller-web -d docker-hub.jc.com/pop-web:v2.2
  • -d: 让容器在后台运行。

  • -p: 将容器内部使用的网络端口随机映射到我们使用的主机上。

  • -v: 主机的目录 /host/path/logs 映射到容器的 /data/logs。 以日志为例,我们是不希望容器重启后日志丢失的,对于这样的场景,就可以将日志目录挂到主机上。

  • -e: 配置环境变量。比如接了apollo配置中心的的工程需要配置env和key,那么就需要添加环境变量:"JAVA_OPTS=-Denv=fat -Dapollo.key=12345678”

  • --add-host: 增加hosts配置,容器的hosts文件中会增加一条配置,在没有配置dns的开发环境就经常需要这样配置。

到此,关于“如何搭建私有镜像仓库”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI