Docker in CI/CD Pipelines
Docker is commonly used in CI/CD to create repeatable build environments, package applications as images, push images to registries, scan vulnerabilities, and deploy the same artifact to different environments.
CI/CD Docker flow
- Developer pushes code.
- Pipeline runs tests.
- Pipeline builds Docker image.
- Image is tagged with commit SHA or version.
- Image is scanned.
- Image is pushed to a registry.
- Deployment pulls the approved image.
Generic Docker pipeline commands
docker build -t myapp:$COMMIT_SHA . docker run --rm myapp:$COMMIT_SHA npm test docker tag myapp:$COMMIT_SHA registry.example.com/team/myapp:$COMMIT_SHA docker push registry.example.com/team/myapp:$COMMIT_SHA
GitLab CI/CD example
stages: - test - build run_tests: image: node:20 stage: test script: - npm ci - npm test build_image: image: docker:27 services: - docker:27-dind stage: build variables: DOCKER_TLS_CERTDIR: "/certs" IMAGE_TAG: "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" script: - echo "$CI_REGISTRY_PASSWORD" | docker login "$CI_REGISTRY" -u "$CI_REGISTRY_USER" --password-stdin - docker build -t "$IMAGE_TAG" . - docker push "$IMAGE_TAG"
Jenkins pipeline idea
pipeline { agent any stages { stage('Build Image') { steps { sh 'docker build -t myapp:${BUILD_NUMBER} .' } } stage('Push Image') { steps { sh 'docker push myregistry/myapp:${BUILD_NUMBER}' } } } }
Tagging best practices
- Use commit SHA for traceability.
- Use semantic version tags for releases.
- Avoid depending only on latest .
- Keep environment tags as pointers, not the only release record.
Security checks
- Scan images before deployment.
- Use trusted base images.
- Do not pass production secrets to untrusted builds.
- Use protected runners for production jobs.
- Review Dockerfile changes carefully.
Common mistakes
- Building image before tests run.
- Pushing every branch to production registry without rules.
- Using the same registry credentials everywhere.
- Not scanning images.
- Using untrusted Docker-in-Docker setups without understanding risk.
Best practices
- Build once, deploy the same image.
- Use immutable tags.
- Scan image before deployment.
- Keep CI logs free of secrets.
- Use registry cleanup policies carefully.
Related Melochip guides
- Docker Learning Path
- GitLab Docker Build Pipeline
- Jenkins MCP Server Guide
Official references
- Docker Build CI documentation
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
7 min read · Sep 19, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Sep 17, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Sep 17, 2026Related articles
Prepare for Docker interviews with practical questions and real-time scenarios covering images, containers, Do...
2 min read · Sep 17, 2026
Learn Docker environment variables and secrets, including docker run -e, env files, Compose environment, secre...
2 min read · Sep 17, 2026
Learn Docker security best practices for images, containers, users, secrets, registries, scanning, least privi...
2 min read · Sep 17, 2026
Learn Docker registries, image tags, Docker Hub, private registries, login, push, pull, naming rules, CI/CD us...
2 min read · Sep 17, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.