Cloud customer?
Start for Free>
Upgrade in MyJFrog >
What's New in Cloud >





Overview

TheDockerBuildnative step performs a build to produce a Docker image from a Dockerfilein aGitReposource repository resource.


In the step configuration, you must provide the name (dockerFileName) and directory (dockerFileLocation) of theDockerfilethat contains the command to be processed by adocker buildcommand, as well as the name (dockerImageName) and tag (dockerImageTag) of the resulting image. The image is built on the build node, and information about that image is stored in the run state.

To build a Docker image that relies ona private base image:

  1. Define the base image as anImageresource, withautoPullset totrue.
  2. Specify the Image resource as one of theinputResourcesof the DockerBuild step.

To include artifacts in the Docker image that are not part of the GitRepo source repository:

  1. Define aFileSpecresource that specifies the files to include from Artifactory.
  2. Specify the FileSpec resource asone of theinputResourcesof the DockerBuild step.


Proper usage of DockerBuild step

DockerBuild andDockerPushsteps must be assigned to the sameaffinityGroupto share state. If this is not done, the output of DockerBuild will not be available for DockerPush. For more information on usingaffinityGroup, seeRunning multiple steps on the same build node.

Docker Build and Push Quickstart

ThisDocker Build and Pushquickstartdemonstrates the definition of a pipeline that uses the DockerBuild andDockerPushnative steps to build a single Docker Image, push it to Artifactory, and then publish the BuildInfo.

Page Contents


YAML Schema

The YAML schema for DockerBuild native step is as follows:

DockerBuild
pipelines: - name:  steps: - name:  type: DockerBuild configuration: #inherits all the tags from bash; //www.si-fil.com/confluence/display/JFROG/Bash affinityGroup:  dockerFileLocation:  dockerFileName:  dockerImageName:  dockerImageTag:  dockerOptions:  integrations: - name:  # required inputResources: - name:  # required, git repository containing your Dockerfile - name:  # optional base image - name:  # optional execution: onStart: - echo "Preparing for work..." onSuccess: - echo "Job well done!" onFailure: - echo "uh oh, something went wrong" onComplete: #always - echo "Cleaning up some stuff"

Tags

name

An alphanumeric string (underscores are permitted) that identifies the step.

type

Must beDockerBuildfor this step type.

configuration

Specifies all configuration selections for the step's execution environment.This step inherits theBash/PowerShellstep configuration tags, including these pertinenttags:

Tag

Description of usage

Required/Optional
affinityGroup Must specify an affinity group string that is the same as specified in a subsequentDockerPushstep. Optional
inputResources

Must specify:

  • aGitReporesource (that contains the Dockerfile)

Optionally, you may also specify:

  • 一个或多个Imageresources to pull base images used in the build or to trigger this build.
  • 一个或多个FileSpecresources that specify what files to include in the build context. These files are automatically copied todockerFileLocation.
Required/Optional


In addition,these tagscan be defined to support the step's native operation:

Tags derived from Bash

来自本地所有步骤Bashstep. This means that all steps share the same base set of tags from Bash, while native steps have their own additional tags as well that support the step's particular function. So it's important to be familiar with theBashstep definition, since it's the core of the definition of all other steps.

Tag

Description of usage

Required/Optional

dockerFileLocation

Directory containing the Dockerfile, which is the file that has Docker build configuration. This file is also used as the context for the Docker build. The path provided should be relative to the root of the input GitRepo repository. If no location is provided, the default is the root of the GitRepo repository. Required

dockerFileName

Name of the Dockerfile. Required

dockerImageName

The name of the Docker image to create. This can be set using environment variables or triggering a run using parameters. Required

dockerImageTag

The tag for the Docker image to create. This can be set using environment variables or triggering a run using parameters. Required
dockerOptions

Additional options for thedocker buildcommand.

Optional

Examples

The following examples use a GoLang Git repository represented by a GitRepo resource namedgosvc_appto create a Docker image that is published to Artifactory. They assume that an Artifactory integration namedMyArtifactoryhas been created, and that the Artifactory instance has a Docker repository mapped todocker.artprod.company.

The following resources declarations support these examples. Not all of these resources are used in all examples.

2022世界杯阿根廷预选赛赛程

resources: # Application source repository - name: gosvc_app type: GitRepo configuration: gitProvider: myGithub path: myuser/myrepo # replace with your repository name branches: include: master # Docker image in an Artifactory repository - name: base_image type: Image configuration: registry: myArtifactory sourceRepository: docker-local # replace with your repository name imageName: docker.artprod.mycompany.com/baseimage imageTag: latest autoPull: true # Files in an Artifactory repository - name: icon_files type: FileSpec configuration: sourceArtifactory: myArtifactory pattern: my-local-repo/all-my-images/ target: icons/

Build a Docker image from a source repository

This example builds a Docker image to a Docker registry in Artifactory. The tag for the image is set to the pipeline's run number.

管道:名称:demo_pipeline步骤:-名称:提单d_image type: DockerBuild configuration: dockerFileLocation: . dockerFileName: Dockerfile dockerImageName: docker.artprod.mycompany.com/gosvc # replace with your fully qualified Docker registry/image name dockerImageTag: ${run_number} inputResources: - name: gosvc_app integrations: - name: MyArtifactory

Build a Docker image with dockerOptions

This example demonstrates use of thedockerOptionstag to set thebuild-argoption for the Docker command. An environment variable namedbuild_number_env_variableis dynamically set to the pipeline's run number. The example assumes the environment variable is used in the Dockerfile commands.

管道:名称:demo_pipeline步骤:-名称:提单d_image type: DockerBuild configuration: dockerFileLocation: . dockerFileName: Dockerfile dockerImageName: docker.artprod.mycompany.com/gosvc # replace with your fully qualified Docker registry/image name dockerImageTag: ${run_number} dockerOptions: --build-arg build_number_env_variable=${run_number} inputResources: - name: gosvc_app integrations: - name: MyArtifactory

Build a Docker image with a private base image

This example builds a Docker image that relies on a private base image stored in an Artifactory Docker repository.

管道:名称:demo_pipeline步骤:-名称:提单d_image type: DockerBuild configuration: dockerFileLocation: . dockerFileName: Dockerfile dockerImageName: docker.artprod.mycompany.com/gosvc # replace with your fully qualified Docker registry/image name dockerImageTag: ${run_number} inputResources: - name: gosvc_app - name: base_image integrations: - name: MyArtifactory

Build a Docker image with files outside the current path

This example demonstrates building a Docker image that includes files outside of the current path. It pulls icon files stored in an Artifactory repository for integration art namedmy-local-repo. It is assumed that the Dockerfile has a command that will include the files in/iconsinto the image.

管道:名称:demo_pipeline步骤:-名称:提单d_image type: DockerBuild configuration: dockerFileLocation: . dockerFileName: Dockerfile dockerImageName: docker.artprod.mycompany.com/gosvc # replace with your fully qualified Docker registry/image name dockerImageTag: ${run_number} inputResources: - name: gosvc_app - name: icon_files integrations: - name: MyArtifactory

How it Works

When you use theDockerBuildnative step in a pipeline, it performs the following functions in the background:

  • cp (if there is a FileSpec input, copy those files to the root of the cloned GitRepo input)
  • docker build
  • add_run_variables (add several variables that are later used when pushing the Docker image or publishing build info)
  • jfrog rt build-collect-env (collect environment information to be later published as part of build info)
  • add_run_files (save information collected for build info)
  • No labels
Copyright © 2022 JFrog Ltd.