Build Docker image only if Code Repository has been updated (Jenkins)

Currently, in a Jenkins job, I build a Docker image with files from a git repository. When this repository is changed/updated I would like to either:

  1. Be able to detect this in Jenkins so I can change my Bash script to rebuild the Docker image from my Dockerfile
  2. Find some other method of building my Docker image that does not require me to build it every time I run the Jenkins job (I run the job hourly, and it takes nearly 10 minutes to build the Docker image)

What is a clean and efficient solution to this? Should I just build the image every time I run the job after all?

  • Best way to pass config file during Docker build
  • Docker-compose v2 expose port to machine
  • Restricting file access of Docker container
  • How to debug dockerized self-detaching program?
  • kubernetes error : skipping pod synchronization
  • How restart a stopped docker container
  • Comparing Docker Tags
  • Missing files after docker commit
  • How to stop all containers when one container stops with docker-compose?
  • Executing bash then running commands in docker
  • How to connect SSMS to Linux SqlServer in docker container?
  • docker-php-ext-install - why always using dev?
  • 2 Solutions collect form web for “Build Docker image only if Code Repository has been updated (Jenkins)”

    This one is simple, first what are you using to host your git? hopefully github / gitlab? if so, “polling” SCM like @amit mentioned is overkill.

    why let jenkins look for changes, when the git server can alert jenkins on changes?

    What you want to do is set up a jenkins job that builds a docker image for you

    then on either github/gitlab, set up a web hook (project settings -> web hooks) to the jenkins project.

    this way, when you push changes to GH/GL, it will send a payload to jenkins, which would then run your build project

    You can configure a “Poll SCM” build trigger – this will cause Jenkins to periodically check your git repository for changes and the build step will be triggered only if changes are detected. (I assume your build step will be doing the docker build).

    You can find more information here –

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.