Build and Push Docker Image to Artifact
About 228 wordsLess than 1 minute
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.
Push to CNB Artifact
.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_TAGPush to Third-party Registry
.cnb.yml
main:
push:
- services:
- docker
imports: https://cnb.cool/<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_TAGThe docker-envs.yml file content:
DOCKER_USER: user
DOCKER_PWD: password
DOCKER_REGISTRY: docker.io
DOCKER_GROUP: group_name
DOCKER_REPO_NAME: repo_name