本地 Docker 启用 API、搭建私有仓库,并集成至 SpringBoot

环境

  • Archlinux

安装 docker

  • 安装 docker 及 docker-compose sudo pacman -S docker docker-compose
  • 使用国内源 sudo vim /etc/docker/daemon.json
{
  "registry-mirrors": [
    "https://registry.docker-cn.com"
  ]
}
  • 启动 docker sudo systemctl start docker
  • 查看相关版本信息 sudo docker info
  • 可选步骤,如果普通用户也需要使用 docker 则需加入 docker 用户组,如 sudo usermod -a -G docker seliote

开启 API

  • 编辑配置 sudo mkdir /etc/systemd/system/docker.service.d/ && sudo vim /etc/systemd/system/docker.service.d/execstart.conf,请勿直接编辑 /usr/lib/systemd/system/docker.service,会导致更新后配置丢失
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375
  • 重新启动 daemon sudo systemctl daemon-reload && sudo systemctl restart docker
  • 验证是否成功启用 docker -H tcp://127.0.0.1:2375 info

搭建私有仓库

  • 创建镜像存储路径 sudo mkdir /opt/registry && cd /opt/registry
  • 创建 htpasswd 认证文件 sudo mkdir auth sudo sh -c 'docker run --entrypoint htpasswd httpd:2 -Bbn admin Changeme_123 > auth/htpasswd'
  • 创建 docker-compose.yml,sudo vim docker-compose.yml
version: '3'
services:
  registry:
    container_name: registry
    restart: always
    image: registry:2
    ports:
      - 5000:5000
    environment:
      REGISTRY_AUTH: htpasswd
      REGISTRY_AUTH_HTPASSWD_PATH: /auth/htpasswd
      REGISTRY_AUTH_HTPASSWD_REALM: Registry Realm
    volumes:
      - ./auth:/auth
      - ./data:/var/lib/registry
  • 启动 docker 镜像 sudo docker-compose up -d

测试

  • 浏览器访问 http://localhost:5000/v2/_catalog
  • 测试推送
    • 本地拉取 hello-world 镜像 sudo docker pull hello-world
    • 打 TAG sudo docker tag hello-world:latest localhost:5000/hw
    • PUSH 至私有仓库 sudo docker push localhost:5000/hw
    • 删除本地镜像 sudo docker image remove localhost:5000/hw && sudo docker image remove hello-world
    • 浏览器访问 http://localhost:5000/v2/_catalog 可以看到新推送的镜像
    • 拉取私有仓库里的镜像

SpringBoot 集成

SpringBoot 2.3.0.M1 开始 spring-boot-maven-plugin 已经内置支持打包为 docker 镜像,具体插件配置样例如下

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <image>
            <name>127.0.0.1:5000/${project.artifactId}</name>
            <publish>true</publish>
        </image>
        <docker>
            <host>tcp://127.0.0.1:2375</host>
            <tlsVerify>false</tlsVerify>
            <publishRegistry>
                <url>127.0.0.1:5000</url>
                <username>admin</username>
                <password>Changeme_123</password>
            </publishRegistry>
        </docker>
    </configuration>
</plugin>

如果每次都需要下载新的镜像但又网络不好,有以下两种方式

  • <image> 标签中添加 <pullPolicy>IF_NOT_PRESENT</pullPolicy>,不推荐,而且是需要能完整下载过的
  • <image> 标签中添加,这个要求自己会爬
<env>
    <HTTP_PROXY>http://192.168.1.12:8118</HTTP_PROXY>
    <HTTPS_PROXY>http://192.168.1.12:8118</HTTPS_PROXY>
</env>

执行 ./mvnw spring-boot:build-image 或在 IDEA Maven 插件中执行,执行完成后即可在 http://localhost:5000/v2/_catalog 中查看到 publish 的镜像。

或者保持默认配置,不引入 <configuration> 标签中的内容,执行上述操作后,镜像则会被打包至本地。

该插件的具体配置可参考 官方文档

posted @ 2021-10-07 23:50  seliote  阅读(157)  评论(0)    收藏  举报