[英]Best way to stop Docker container in Jenkins
I have a CI-server (jenkins) which is building new Docker images.我有一个 CI 服务器(jenkins),它正在构建新的 Docker 镜像。 Now I want to run a new Docker container when the build is succesful.
现在我想在构建成功时运行一个新的 Docker 容器。 But therefor I have to stop the previous running container.
但因此我必须停止之前运行的容器。
What's the best way to perform this?执行此操作的最佳方法是什么?
localhost:5000/test/myapp:"${BUILD_ID}
is the name of my new images. So I'm using the build-id as tag. First I thought to perform: localhost:5000/test/myapp:"${BUILD_ID}
是我的新镜像的名称。所以我使用 build-id 作为标签。首先我想执行:
docker stop localhost:5000/dbm/my-php-app:${BUILD_ID-1}
But this isn't a right solution because when a build would fail, this would be wrong.但这不是一个正确的解决方案,因为当构建失败时,这将是错误的。
Build 1: succes -> run container 1
Build 2: failed -> run container 1
Build 3: succes -> stop container (3-1) =2 --> wrong (isn't running)
What could be a solution?什么是解决方案? Proposals where I have to change the tag-idea are also welcome
也欢迎我必须更改标签想法的建议
The docker stop
command takes a docker container name as parameter, not a docker image ID . docker stop
命令将docker 容器名称作为参数,而不是docker 镜像 ID 。
You would have to name your container when you run it:当你运行它时,你必须命名你的容器:
# build the new image
docker build -t localhost:5000/test/myapp:"${BUILD_ID}" .
# remove the existing container
docker rm -f myjob && echo "container myjob removed" || echo "container myjob does not exist"
# create and run a new container
docker run -d --name myjob localhost:5000/test/myapp:"${BUILD_ID}"
Just replace myjob
with a better suited name in this example.在此示例中,只需将
myjob
替换为更合适的名称即可。
Thanks to @Thomasleveil I found the answer on my question:感谢@Thomasleveil,我找到了问题的答案:
# build the new image
docker build -t localhost:5000/test/myapp:"${BUILD_ID}" .
# remove old container
SUCCESS_BUILD=`wget -qO- http://jenkins_url:8080/job/jobname/lastSuccessfulBuild/buildNumber`
docker rm -f "${SUCCESS_BUILD}" && echo "container ${SUCCESS_BUILD} removed" || echo "container ${SUCCESS_BUILD} does not exist"
# run new container
docker run -d -p 80:80 --name "${BUILD_ID}" localhost:5000/test/myapp:${version}
If you are using Jenkins Pipeline and want to gracefully stop and remove containers then here is the solution.如果您正在使用Jenkins Pipeline并希望优雅地停止和删除容器,那么这里是解决方案。
First you have to check is any container is present, and if there is any then you will have to stop and remove it.首先,您必须检查是否存在任何容器,如果有,则必须停止并移除它。 Here is the code
这是代码
def doc_containers = sh(returnStdout: true, script: 'docker container ps -aq').replaceAll("\n", " ")
if (doc_containers) {
sh "docker stop ${doc_containers}"
}
The variable doc_containers
will store the container IDs, and you can perform the empty check.变量
doc_containers
将存储容器 ID,您可以执行空检查。 If container is not available then docker stop
command should not be executed.如果容器不可用,则不应执行
docker stop
命令。
Here is the pipeline code这是管道代码
stage('Clean docker containers'){
steps{
script{
def doc_containers = sh(returnStdout: true, script: 'docker container ps -aq').replaceAll("\n", " ")
if (doc_containers) {
sh "docker stop ${doc_containers}"
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.