环境
- 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 vim /usr/lib/systemd/system/docker.service
...
>>> ExecStart=/usr/bin/dockerd -H fd://
# 上行修改为如下,无认证访问,不安全,如需公网访问请配置认证
<<< ExecStart=/usr/bin/dockerd -H fd:// -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
可以看到新推送的镜像 - 拉取私有仓库里的镜像
- 本地拉取 hello-world 镜像
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>
执行 ./mvnw spring-boot:build-image
或在 IDEA Maven 插件中执行,执行完成后即可在 http://localhost:5000/v2/_catalog
中查看到 publish 的镜像。
或者保持默认配置,不引入 <configuration>
标签中的内容,执行上述操作后,镜像则会被打包至本地。
该插件的具体配置可参考 官方文档。