I am building a Docker image using a command line like the following:
docker build -t myimage .
Once this command has succeeded, then rerunning it is a no-op as the image specified by the Dockerfile
has not changed. Is there a way to detect if the Dockerfile
(or one of the build context files) subsequently changes without rerunning this command?
The docker build command builds Docker images from a Dockerfile and a “context”. A build's context is the set of files located in the specified PATH or URL . The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.
docker pull will always contact the registry and get an updated image. Try, for instance, docker pull ubuntu:18.04 if you have it locally and haven't pulled it recently.
looking at docker inspect $image_name
from one build to another, several information doesn't change if the docker image hasn't changed. One of them is the docker Id
. So, I used the Id
information to check if a docker has been changed as follows:
First, one can get the image Id
as follows:
docker inspect --format {{.Id}} $docker_image_name
Then, to check if there is a change after a build, you can follow these steps:
Code: Here is a working bash script implementing the above idea:
docker inspect --format {{.Id}} $docker_image_name > deploy/last_image_build_id.log
# I get the docker last image id from a file
last_docker_id=$(cat deploy/last_image_build_id.log)
docker build -t $docker_image_name .
docker_id_after_build=$(docker inspect --format {{.Id}} $docker_image_name)
if [ "$docker_id_after_build" != "$last_docker_id" ]; then
echo "image changed"
else
echo "image didn't change"
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With