---
url: /en/build/showcase/docker-build-and-push-to-cnb-artifact.md
description: >
  Demonstrates how to build a Docker image within a CNB pipeline and push it to
  CNB Docker Artifact Registry using built-in pipeline credentials.
---
This article describes how to build a Docker image in a pipeline and push it to an artifact registry.

## Push Docker Images

CNB pipelines include built-in credentials of the triggerer,
which can be used to log into CNB Docker Artifact and push images directly.
To push to third-party registries, add `imports` for credentials and a `docker login` step.

::: tabs
@tab Push to CNB Artifact

```yaml title=".cnb.yml"
main:
  push:
    - services:
        - docker
      stages:
        - name: set docker tag
          script: echo -n "${CNB_DOCKER_REGISTRY}/${CNB_REPO_SLUG_LOWERCASE}:latest"
          exports:
            info: IMAGE_TAG
        - name: docker build
          script: docker build -t $IMAGE_TAG .
        - name: push image
          script: docker push $IMAGE_TAG
```

@tab Push to Third-party Registry

```yaml title=".cnb.yml"
main:
  push:
    - services:
        - docker
      imports: https://cnb.build/<your-repo-slug>/-/blob/main/xxx/docker-envs.yml
      stages:
        - name: docker login
          script: echo "${DOCKER_PWD}" | docker login -u ${DOCKER_USER} --password-stdin ${DOCKER_REGISTRY}
        - name: set docker tag
          script: echo -n "${DOCKER_REGISTRY}/${DOCKER_GROUP}/${DOCKER_REPO_NAME}:$CNB_COMMIT_SHORT"
          exports:
            info: IMAGE_TAG
        - name: docker build
          script: docker build -t $IMAGE_TAG .
        - name: push image
          script: docker push $IMAGE_TAG
```

The `docker-envs.yml` file content:

```yaml
DOCKER_USER: user
DOCKER_PWD: password
DOCKER_REGISTRY: docker.io
DOCKER_GROUP: group_name
DOCKER_REPO_NAME: repo_name
```

:::
