Issue
I have a CI-server (jenkins) which is building new Docker images. Now I want to run a new Docker container when the build is succesful. 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:
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
Solution
Thanks to @Thomasleveil I found the answer on my question:
# 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}
Answered By - user5558501