Docker Registry Commands
Start your registry
docker run -d -p 5000:5000 --name registry registry:2
Pull (or build) some image from the hub
docker pull ubuntu
Tag the image so that it points to your registry
docker image tag ubuntu localhost:5000/myfirstimage
Push it
docker push localhost:5000/myfirstimage
Pull it back
docker pull localhost:5000/myfirstimage
Now stop your registry and remove all data
docker container stop registry && docker container rm -v registry
查看注册了哪些镜像
访问:http://localhost:5000/v2/_catalog
用 Dockerfile 构建、发布
原文点击这里
- 普通推送:
docker tag image1 uid/image:1.0
docker push uid/image:1.0
docker tag image1 uid/image
docker push uid/image
- 使用本地仓库(把uid变为 registry 地址):
docker tag image2 localhost:5000/image:2.0
docker push localhost:5000/image:2.0
docker tag image2 localhost:5000/image
docker push localhost:5000/image
- 更新镜像:
docker pull localhost:5000/image:2.0
docker-compose up -d
docker image prune
docker pull localhost:5000/image
docker-compose up -d
docker image prune
- 更新镜像编写为一个脚本:
#!/bin/sh
docker pull localhost:5000/image:latest && docker-compose up -d && docker image prune -f # -f 无需确认,强制prune
- shell 命令关联
&&
代表上一个成功才执行下一个;||
代表上一个失败才执行下一个;; 分号
代表无关联顺序执行;&
代表前一个命令进入后台立即执行后面的命令;
常见问题
- 由于启动的 registry 服务不是安全可信赖的,在其他服务器上推送会显示:
$ docker push 10.1.2.9:5000/test
Using default tag: latest
The push refers to repository [10.1.2.9:5000/test]
Get "https://10.1.2.9:5000/v2/": http: server gave HTTP response to HTTPS client
a. 这时需要修改客户端的 Docker 引擎配置文件来信任这个仓库。修改客户端配置文件 /etc/docker/daemon.json,如果没有该文件或文件夹需手动进行创建。在文件中添加 insecure-registries,内容为 registry 的服务地址:(重启后生效)
// 客户端配置文件
{
"registry-mirrors": [ "https://pee6w651.mirror.aliyuncs.com"],
"insecure-registries": ["registry所在的ip地址:5000"]
}
使用dockerfile-maven-plugin
- 项目maven
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.10</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>${ip}:5000/${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.${project.packaging}</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
- pom 同级 Dockerfile
FROM java:8
LABEL maintainer="xxxcompany"
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 8080
ARG JAR_FILE
ADD ${JAR_FILE} /usr/share/myservice/myservice.jar
ENTRYPOINT ["java", "-jar", "/usr/share/myservice/myservice.jar"]
- 该插件使用的是守护线程的2375端口进行的build,所以需要本机进行 Expose开放(公网开放容易被挖矿,需要使用证书)
(我原本是命令行安装的。尝试了很多方法,最后还是选择下载docker desktop进行 Expose 管理。此时 docker 容器和镜像会被清空,注意备份)