How to Build and Push a Docker Image in CircleCI
CircleCI builds images with setup_remote_docker, which gives the job a remote Docker environment.
Add the setup_remote_docker step (or use the machine executor), log in with secret env vars, then build and push tagged at $CIRCLE_SHA1.
Build with a remote Docker env
Enable remote Docker, log in with project env vars, then build and push.
.circleci/config.yml
jobs:
build-image:
docker:
- image: cimg/base:current
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- run: echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USER" --password-stdin
- run: docker build -t myorg/app:$CIRCLE_SHA1 .
- run: docker push myorg/app:$CIRCLE_SHA1Gotchas
- On the
dockerexecutor you must addsetup_remote_dockerto rundocker build; the job container has no daemon. docker_layer_caching: truereuses layers across runs but is a paid feature on most plans.- Store
DOCKER_USER/DOCKER_PASSWORDas project or context env vars, never in the config.
Related guides
How to Build and Push a Docker Image in GitHub ActionsBuild and push a Docker image in GitHub Actions with docker/build-push-action - log in to GHCR, set tags, and…
How to Cache Dependencies in CircleCICache dependencies in CircleCI with save_cache and restore_cache - checksum-based keys, restore-key fallbacks…