docker.ts

Overview

The [docker.ts](/projects/291/68795) file provides a utility function to automate the building and pushing of Docker images, specifically focusing on a base image variant. Its primary purpose is to:

This file is a specialized helper within the broader Docker Image Automation system, ensuring efficient image creation workflows by leveraging remote tag existence checks and build caching.


Detailed Explanation

Imports


Function: buildAndPushDockerImages

export const buildAndPushDockerImages = async (dockerhub: Dockerhub, name: string) => {
  ...
}

Purpose

This asynchronous function orchestrates building and pushing a base Docker image tagged with a unique hash. It prevents unnecessary builds by checking if the image with the computed tag already exists.

Parameters

Parameter

Type

Description

`dockerhub`

`Dockerhub`

Represents Dockerhub credentials and configuration used for authentication during push and build.

`name`

`string`

Base name for the image; the function constructs the full image name as `${dockerhub.username}/${name}-base`.

Behavior & Workflow

  1. Construct Base Image Name:

    The base image name is built as:

    {dockerhub.username}/{name}-base
    

    For example, if dockerhub.username is "alice" and name is "blockchain-node", the base image would be "alice/blockchain-node-base".

  2. Calculate Base Tag:

    Calls getBaseHash() to get a unique hash representing the base image version. This hash is typically content-based and ensures reproducibility and cache consistency.

  3. Check Remote Tag Existence:

    Uses hasTag(baseImage, baseTag) to verify if the image with the computed tag already exists on the Docker registry.

  4. Conditional Build and Push:

    If the tag does not exist, it triggers buildAndPushImage() with the following options:

    • image: The full base image name.

    • context: Relative path '../../..' indicating the Docker build context directory.

    • dockerFile: Relative path '../../../Dockerfile.node' specifying the Dockerfile to use.

    • auth: Dockerhub credentials for authentication.

    • buildArgs: Build argument BUILDKIT_INLINE_CACHE=1 to enable inline caching.

    • env: Environment variable DOCKER_BUILDKIT=1 to enable BuildKit during build.

    • tags: Array with the computed base tag.

    • cacheFroms: Array of images used for build caching, including the base image with current tag and the latest tag.

Return Value

Usage Example

import { Dockerhub } from '..'
import { buildAndPushDockerImages } from './docker'

const dockerhub = new Dockerhub({ username: 'alice', token: '...' })
const imageName = 'blockchain-node'

await buildAndPushDockerImages(dockerhub, imageName)
// Builds and pushes alice/blockchain-node-base:<hash> if not already present

Important Implementation Details


Interaction with Other System Components


Mermaid Diagram: Class and Function Structure

Since this file contains a single exported asynchronous function and uses imported utilities, a flowchart best illustrates its workflow and dependencies:

flowchart TD
  A[Start: buildAndPushDockerImages(dockerhub, name)]
  B[getBaseHash()]
  C[Construct baseImage and baseTag]
  D{Check if tag exists?}
  E[Tag exists -> Skip build]
  F[Tag missing -> Call buildAndPushImage()]
  G[Build and push image with options]
  H[End]

  A --> B --> C --> D
  D -- Yes --> E --> H
  D -- No --> F --> G --> H

Summary

The [docker.ts](/projects/291/68795) file is a focused utility that encapsulates the logic needed to:

It is a critical part of the automated Docker image lifecycle within the overall deployment automation framework, interfacing with Docker registries and build tooling to streamline container image management.


**End of documentation for [docker.ts](/projects/291/68795).**