docker.ts

Overview

The [docker.ts](/projects/291/68797) file provides utility functions for automating Docker image lifecycle operations, specifically building, tagging, caching, and pushing images to Docker registries. It streamlines and standardizes how container images are created and deployed by:

This file is a core part of the Docker Image Automation workflow, enabling efficient, repeatable, and reliable container image management in deployment pipelines, especially when integrated with Pulumi infrastructure-as-code deployments.


Exports

Interface: BuildAndPushImageArgs

Defines the arguments accepted by the `buildAndPushImage` function.

Property

Type

Description

`auth`

`Dockerhub`

Authentication credentials for Docker Hub (username and password).

`buildArgs?`

`Record`

Optional key-value pairs passed as `--build-arg` to customize Docker build.

`env?`

`Record`

Optional environment variables injected during the Docker build process.

`cacheFroms?`

`Array`

Optional list of images to use as cache sources (`--cache-from`).

`context`

`string`

Build context directory for Docker (path where Dockerfile and source files reside).

`dockerFile?`

`string`

Optional path to a Dockerfile to use instead of the default `Dockerfile`.

`image`

`string`

Name of the Docker image to build (without tag).

`tags?`

`Array`

Optional list of tags to apply to the image. Defaults to `latest` if omitted.


Function: buildAndPushImage(args: BuildAndPushImageArgs): Promise<void>

Builds a Docker image with specified options, tags it with one or more tags, and pushes all tags to the Docker registry.

Parameters

Returns

Behavior and Implementation Details

  1. Docker Login:

    • Executes docker login using provided credentials (args.auth) to authenticate with the Docker registry.

    • Errors are suppressed by redirecting stderr to /dev/null.

  2. Tag Preparation:

    • Builds a list of image tags to assign.

    • If args.tags is provided, each tag is prefixed with the image name (image:tag).

    • Ensures the latest tag is included if not explicitly provided.

    • If no tags are specified, defaults to a single latest tag.

  3. Docker Build Arguments:

    • Starts with tags (-t image:tag) for all tags.

    • Adds build arguments (--build-arg key=value) from args.buildArgs.

    • Adds caching options (--cache-from image) for args.cacheFroms.

    • Specifies Dockerfile location if args.dockerFile is set (-f Dockerfile).

    • Appends the build context directory.

  4. Environment Variables:

    • Converts args.env key-value pairs into a single string of environment variable assignments prepended to the docker build command.

  5. Docker Build Execution:

    • Runs the assembled docker build command with --pull (to always pull latest base images) and targets platform linux/amd64.

    • Uses execSync with stdio: 'inherit' so build logs stream to the console.

  6. Push to Registry:

    • Unless Pulumi is in a dry-run mode (pulumi.runtime.isDryRun()), pushes each tagged image (docker push image:tag).

Usage Example

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

const auth: Dockerhub = { username: 'user', password: 'pass' }

await buildAndPushImage({
  auth,
  context: './app',
  image: 'user/my-app',
  tags: ['v1.0.0', 'stable'],
  buildArgs: { NODE_ENV: 'production' },
  env: { API_URL: 'https://api.example.com' },
  cacheFroms: ['user/my-app:latest'],
  dockerFile: 'Dockerfile.prod'
})

Function: hasTag(repository: string, tag: string): Promise<boolean>

Checks if a Docker image tag exists on Docker Hub.

Parameters

Returns

Implementation Details

Usage Example

const exists = await hasTag('user/my-app', 'v1.0.0')
if (!exists) {
  // Build and push the image since tag does not exist
}

Important Implementation Notes


Integration with Other System Components


Visual Diagram

classDiagram
    class BuildAndPushImageArgs {
        +auth: Dockerhub
        +buildArgs?: Record<string, string>
        +env?: Record<string, string>
        +cacheFroms?: Array<string>
        +context: string
        +dockerFile?: string
        +image: string
        +tags?: Array<string>
    }

    class buildAndPushImage {
        +async(args: BuildAndPushImageArgs): Promise<void>
    }

    class hasTag {
        +async(repository: string, tag: string): Promise<boolean>
    }

    buildAndPushImage ..> BuildAndPushImageArgs : uses
    buildAndPushImage ..> Dockerhub : uses auth
    hasTag ..> "Docker Hub API" : queries tag existence

Summary

The [docker.ts](/projects/291/68797) file is a focused utility module that automates Docker image build, tagging, caching, and push operations with support for multiple tags and build-time customization. Through synchronous Docker CLI invocations and integration with Pulumi, it fits seamlessly into deployment pipelines requiring robust container image management. The remote tag existence check via Docker Hub API further optimizes workflows by preventing unnecessary rebuilds and uploads, saving resources and time. This module is foundational for orchestrating consistent and efficient container deployments within the broader system infrastructure.