> For the complete documentation index, see [llms.txt](https://docs.duplocloud.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.duplocloud.com/docs/automation-platform/introduction-to-ci-cd/circleci/build-and-push-docker-image.md).

# Build and Push Docker Image

## Introduction

The goal of this section is to show how you can build a docker image and push it to ECR.

It does three basic things:

* Logs in to AWS ECR using just-in-time (JIT) AWS credentials from Duplo
* Builds and tags your docker image, with the tag based on the git commit SHA.
* Pushes your docker image

## Example Workflow

Here is an example CircleCI workflow that builds a docker image and pushes it to ECR.

To use it you will need to change following environment variables:

* `DOCKER_REPO`
* `DOCKER_IMAGE_NAME`
* `DUPLO_SERVICE_NAME`
* `DOCKER_REPO`
* `ECR_REGION`

```yaml
version: 2.1
parameters:
  create_image:
    type: boolean
    default: false
orbs: 
  vpn: titel-media/openvpn@0.1.1
  newman: postman/newman@0.0.2
  dynamo-lock: gastfreund/dynamo-lock@1.0.1

defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: cimg/node:17.5.0
  environment:
    TENANT_NAME: dev01
    DOCKER_REPO: public.ecr.aws/p9c8y2k3
    DOCKER_IMAGE_NAME: demo-npm-service
    DUPLO_SERVICE_NAME: nginx
    ECR_REGION: us-west-2
jobs:
  BuildAndTest:
    <<: *defaults
    steps:
      - checkout

      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          - v1-dependencies-
      - run: npm install
      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}
      - persist_to_workspace:
          root: ~/repo
          paths: .
  PublishDockerContainerRC:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/repo
      - setup_remote_docker:
          version: 19.03.13
      - run:
          name: Install Dependencies
          command: |
            source ./.circleci/duplo_utils.sh && install_dependencies
      - run:
          name: Create and Push Docker container
          command: |
            source ./.circleci/duplo_utils.sh
            tag=$(node -p "require('./package.json').version")
            docker_tag=$(get_docker_tag_rc $tag)
            echo "Starting build for container: $docker_tag"
            docker build -t $docker_tag . 
            push_container_rc $tag
```

Above example of CircleCI requires DuploCloud utility shell script file which has to be checked in with your CircleCI file. This utility file can be found here: <https://github.com/duplocloud/demo-npm-service/tree/master/.circleci>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.duplocloud.com/docs/automation-platform/introduction-to-ci-cd/circleci/build-and-push-docker-image.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
