I am facing a problem in creating CI for docker containers. During CI build I have to remove the previous docker container and image, in this case, the build is failing when there is not any image on the server.
How can I execute this statement without stopping the build to fail?
docker rmi example/hello-world:latest
Unable to find image 'example/hello-world:latest' locally
docker: Error response from daemon:
The build is not failing in the docker stop
and docker rm
case:
docker stop zod || true && docker rm zod || true
How do I make sure the build doesn't fail if the image doesn't exist on the server?
This is my script for docker deployement:
docker build -t example/hello-world:latest .
docker stop zod || true && docker rm zod || true
docker rmi example/hello-world:latest
docker run --name zod -d -p 6000:6000 -dit example/hello-world:latest
CodePudding user response:
First check if the image exists, then remove it:
exists=$(docker images example/hello-world:latest | tail -n 2)
if [ -z $exists ]
then
docker rmi example/hello-world:latest
fi