Skip to content

17. Docker + Git Workflows

Docker and Git are the two most essential tools in modern software delivery. When combined, they guarantee that the exact code a developer commits is perfectly packaged, versioned, and deployed with absolute consistency.

The modern deployment pipeline strictly intertwines version control (Git) with containerization (Docker).

Git Repository (Source Code + Dockerfile)
↓
Developer runs `git commit` & `git push`
↓
CI/CD Server detects changes and runs `docker build`
↓
Resulting Docker Image is tagged
↓
Image is uploaded (`docker push`) to a Container Registry

In this workflow, the Git Repository is the ultimate source of truth. If a server goes down, you don’t SSH into the server to fix it. You fix the code in Git, push it, let the CI/CD pipeline build a new Docker image, and deploy that new image.

When pushing an image to a registry, you must tag it to indicate what version of the application it contains. Serious projects use Semantic Versioning (SemVer) for their Docker tags.

MAJOR.MINOR.PATCH

  • myapp:1.0.0: Initial stable release.
  • myapp:1.0.1: A small bug fix (PATCH). The application behaves exactly the same, just with fewer bugs. Safe to upgrade automatically.
  • myapp:1.1.0: A new feature was added (MINOR). It is backwards compatible. Safe to upgrade.
  • myapp:2.0.0: A massive architectural change (MAJOR). It is not backwards compatible. Upgrading will likely break existing configurations or require database migrations.

By strictly tagging Docker images with SemVer, ops teams can confidently deploy myapp:1.1.x knowing it won’t break the system, while holding off on myapp:2.x until they are ready to migrate.

While Semantic Versioning is great for official public releases, it is too slow and manual for aggressive CI/CD pipelines where developers push code 50 times a day.

To trace a specific Docker image running in production back to the exact line of code that created it, modern CI/CD pipelines automatically tag Docker images using the Git Commit Hash (the short SHA).

Example: If a developer pushes a commit with the hash a83f91c, the CI pipeline builds the image and tags it as:

myapp:git-a83f91c

If a bug occurs in production on the image myapp:git-a83f91c, a developer can simply run git checkout a83f91c on their local laptop and instantly see the exact state of the code that is failing in production.

(Note: In practice, teams usually tag an image with BOTH the Git hash for internal tracking, and a SemVer tag for official releases).

A robust deployment strategy relies on strict discipline regarding how images are handled.

Once an image is built and tagged (e.g., myapp:1.1.0), you must never overwrite that tag with a new image. If you find a bug in 1.1.0, you fix it and push a completely new image tagged 1.1.1. Overwriting tags destroys the reproducibility of your infrastructure.

Avoid relying on the :latest tag in production. Because :latest is constantly overwritten every time someone merges code, you never truly know what version of the software is running on your servers. Always specify exact version tags in your compose.yaml or Kubernetes manifests.

Because Docker images are immutable packages, rolling back a failed deployment is instantaneous.

If you deploy myapp:2.0.0 and the site goes down, you don’t need to frantically revert Git commits, recompile code, and wait 10 minutes. You simply update your deployment manifest back to myapp:1.1.0, and the orchestrator will instantly spin up the old, perfectly working container from the registry cache.