Class: Kamal::Dev::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/kamal/dev/builder.rb

Overview

Builder for building and pushing Docker images

Wraps Docker build and push operations with:

  • Build progress display
  • Tag management (timestamp, git SHA, custom)
  • Error handling for build failures
  • Registry authentication

Examples:

Build an image

builder = Kamal::Dev::Builder.new(config, registry)
builder.build(
  dockerfile: ".devcontainer/Dockerfile",
  context: ".",
  tag: "abc123"
)

Push an image

builder.push("myapp-dev:abc123")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, registry) ⇒ Builder

Initialize builder with configuration and registry

Parameters:

  • Configuration object

  • Registry object



31
32
33
34
# File 'lib/kamal/dev/builder.rb', line 31

def initialize(config, registry)
  @config = config
  @registry = registry
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



25
26
27
# File 'lib/kamal/dev/builder.rb', line 25

def config
  @config
end

#registryObject (readonly)

Returns the value of attribute registry.



25
26
27
# File 'lib/kamal/dev/builder.rb', line 25

def registry
  @registry
end

Instance Method Details

#build(dockerfile:, context: ".", tag: nil, image_base: nil, build_args: {}, secrets: {}) ⇒ String

Build Docker image from Dockerfile

Parameters:

  • Path to Dockerfile

  • (defaults to: ".")

    Build context path (default: ".")

  • (defaults to: nil)

    Image tag (optional, auto-generated if not provided)

  • (defaults to: nil)

    Base image name from config (optional, uses config.service if not provided)

  • (defaults to: {})

    Build arguments (optional)

  • (defaults to: {})

    Build secrets (optional)

Returns:

  • Full image reference with tag

Raises:

  • if build fails



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kamal/dev/builder.rb', line 46

def build(dockerfile:, context: ".", tag: nil, image_base: nil, build_args: {}, secrets: {})
  tag ||= registry.tag_with_timestamp

  # Use image_base if provided (new format), otherwise fall back to service name (old format)
  image_ref = if image_base
    registry.image_tag(image_base, tag)
  else
    registry.image_tag(config.service, tag)
  end

  # If git clone is enabled, wrap Dockerfile with entrypoint injection
  if config.git_clone_enabled?
    build_with_entrypoint(
      dockerfile: dockerfile,
      context: context,
      image: image_ref,
      build_args: build_args,
      secrets: secrets
    )
  else
    command = build_command(
      dockerfile: dockerfile,
      context: context,
      image: image_ref,
      build_args: build_args,
      secrets: secrets
    )

    execute_with_output(command, "Building image #{image_ref}...")
  end

  image_ref
end

#docker_available?Boolean

Check if Docker is available

Returns:

  • true if Docker is installed and running



114
115
116
117
# File 'lib/kamal/dev/builder.rb', line 114

def docker_available?
  result = execute_command(["docker", "version"])
  result[:success]
end

#image_exists?(image_ref) ⇒ Boolean

Check if image exists locally

Parameters:

  • Full image reference

Returns:

  • true if image exists



123
124
125
126
# File 'lib/kamal/dev/builder.rb', line 123

def image_exists?(image_ref)
  result = execute_command(["docker", "image", "inspect", image_ref])
  result[:success]
end

#loginBoolean

Authenticate with Docker registry

Returns:

  • true if login succeeded

Raises:

  • if login fails



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/kamal/dev/builder.rb', line 97

def 
  command = registry.

  # Login command uses password on command line, so we execute silently
  result = execute_command(command)

  unless result[:success]
    raise Kamal::Dev::RegistryError,
      "Docker login failed: #{result[:error]}"
  end

  true
end

#push(image_ref) ⇒ Boolean

Push Docker image to registry

Parameters:

  • Full image reference (registry/user/image:tag)

Returns:

  • true if push succeeded

Raises:

  • if push fails



85
86
87
88
89
90
91
# File 'lib/kamal/dev/builder.rb', line 85

def push(image_ref)
  command = ["docker", "push", image_ref]

  execute_with_output(command, "Pushing image #{image_ref}...")

  true
end

#tag_with_git_sha(base_image) ⇒ String?

Tag image with git SHA

Parameters:

  • Base image reference

Returns:

  • New image reference with git SHA tag or nil if not in git repo



145
146
147
148
149
150
151
152
153
154
# File 'lib/kamal/dev/builder.rb', line 145

def tag_with_git_sha(base_image)
  tag = registry.tag_with_git_sha
  return nil unless tag

  new_image = "#{base_image}:#{tag}"

  execute_command(["docker", "tag", base_image, new_image])

  new_image
end

#tag_with_timestamp(base_image) ⇒ String

Tag image with timestamp

Parameters:

  • Base image reference

Returns:

  • New image reference with timestamp tag



132
133
134
135
136
137
138
139
# File 'lib/kamal/dev/builder.rb', line 132

def tag_with_timestamp(base_image)
  tag = registry.tag_with_timestamp
  new_image = "#{base_image}:#{tag}"

  execute_command(["docker", "tag", base_image, new_image])

  new_image
end