Skip to content
Latchkey

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_SHA1

Gotchas

  • On the docker executor you must add setup_remote_docker to run docker build; the job container has no daemon.
  • docker_layer_caching: true reuses layers across runs but is a paid feature on most plans.
  • Store DOCKER_USER/DOCKER_PASSWORD as project or context env vars, never in the config.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →