问题是Jenkins不能自己删除所有的图像。有时它会删除所有的图像,有时只删除部分图像,留下悬空的图像。在某些时候,它是随机发生的。
我的设置:
- Jenkins2.346.2
- Docker 2017年10月20日,构建号100c 701
- Ubuntu 20.04.4语言版本
Jenkins文件中的一些代码片段:
pipeline {
agent any
options {
skipStagesAfterUnstable()
buildDiscarder(logRotator(numToKeepStr: '30'))
timestamps()
}
...build some jar file...
stages {
stage("Build docker images") {
steps {
script {
echo "Bulding docker images"
def buildArgs = """\
-f Dockerfile \
."""
def image = docker.build(
"simple-java-maven-app:latest",
buildArgs)
}
}
}
stage("Push to Dockerhub") {
steps {
script {
echo "Pushing the image to docker hub"
def localImage = "${params.Image_Name}:${params.Image_Tag}"
def repositoryName = "generaltao725/${localImage}"
sh "docker tag ${localImage} ${repositoryName} "
...push to hub...
}
}
}
}
post {
always {
script {
echo 'I will always say Hello again!'
sh "docker rmi -f generaltao725/simple-java-maven-app simple-java-maven-app"
sh "docker system prune -f"
sh "docker images"
}
}
}
}
完整的代码在这里https://github.com/GeneralTao2/simple-java-maven-app/blob/for_sharing/Jenkinsfile
日志片段:
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
21:23:40 I will always say Hello again!
[Pipeline] sh
21:23:41 + docker rmi -f generaltao725/simple-java-maven-app simple-java-maven-app
21:23:41 Untagged: generaltao725/simple-java-maven-app:latest
21:23:41 Untagged: simple-java-maven-app:latest
21:23:41 Deleted: sha256:daffc41b3af93166db4c19d8b4414051b3b4518e6ddd257c748ab6706060ca0d
21:23:41 Deleted: sha256:68b669ea8fdc6f95d9b3804098adc846d08e205f01a5766a9ce9c406a82f41d2
21:23:41 Deleted: sha256:1eafd5ac1d9d3f6e3b578ac0faea1cf6bbda85ab1a2106b590e6a67cc2cfa887
21:23:41 Deleted: sha256:a4f900510305bbd11d46c1b09dabbb03864e46e1b3e9fe4839dbd96a917f6958
21:23:41 Deleted: sha256:f0a6ad878e8be605a4753d9b1aa2d076fcdd3040ddc6d220d60d03e27f4a3864
[Pipeline] sh
21:23:41 + docker system prune -f
21:23:41 Total reclaimed space: 0B
[Pipeline] sh
21:23:42 + docker images
21:23:42 REPOSITORY TAG IMAGE ID CREATED SIZE
21:23:42 openjdk 11 47a932d998b7 2 months ago 654MB
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
在docker images
之后,在管道执行之后,我实际看到的是:
root@470d20ccbca3:/# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
generaltao725/simple-java-maven-app latest baa11625ecc8 23 hours ago 674MB
<none> <none> 2a8333ccbffb 23 hours ago 674MB
我正在做它几天,没有什么具体的关于它的文件...我将很高兴得到任何帮助!
1条答案
按热度按时间wswtfjt71#
我想使用的方法仍然不起作用。但是我找到了一个解决方法,效果差不多。你可以从主机端使用
docker exec
:它可以由cron或任何web-hook处理程序触发。我使用webhook,到目前为止,它工作正常。