Class: Kamal::Dev::Devcontainer

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

Overview

Represents a single devcontainer instance with its parsed configuration

Provides methods to access Docker configuration and generate Docker run commands.

Examples:

Creating a devcontainer

config = {
  image: "ruby:3.2",
  ports: [3000, 5432],
  mounts: [{source: "gem-cache", target: "/usr/local/bundle", type: "volume"}],
  env: {"RAILS_ENV" => "development"},
  options: ["--cpus=2", "--memory=4g"],
  user: "vscode",
  workspace: "/workspace"
}
devcontainer = Devcontainer.new(config)
command = devcontainer.docker_run_command(name: "myapp-dev-1")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Devcontainer

Initialize devcontainer with parsed configuration



35
36
37
38
39
40
41
42
43
44
# File 'lib/kamal/dev/devcontainer.rb', line 35

def initialize(config)
  @image = config[:image]
  @ports = config[:ports] || []
  @mounts = config[:mounts] || []
  @env = config[:env] || {}
  @options = config[:options] || []
  @user = config[:user]
  @workspace = config[:workspace]
  @secrets = config[:secrets] || {}
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



22
23
24
# File 'lib/kamal/dev/devcontainer.rb', line 22

def env
  @env
end

#imageObject (readonly)

Returns the value of attribute image.



22
23
24
# File 'lib/kamal/dev/devcontainer.rb', line 22

def image
  @image
end

#mountsObject (readonly)

Returns the value of attribute mounts.



22
23
24
# File 'lib/kamal/dev/devcontainer.rb', line 22

def mounts
  @mounts
end

#optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'lib/kamal/dev/devcontainer.rb', line 22

def options
  @options
end

#portsObject (readonly)

Returns the value of attribute ports.



22
23
24
# File 'lib/kamal/dev/devcontainer.rb', line 22

def ports
  @ports
end

#secretsObject (readonly)

Returns the value of attribute secrets.



22
23
24
# File 'lib/kamal/dev/devcontainer.rb', line 22

def secrets
  @secrets
end

#userObject (readonly)

Returns the value of attribute user.



22
23
24
# File 'lib/kamal/dev/devcontainer.rb', line 22

def user
  @user
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



22
23
24
# File 'lib/kamal/dev/devcontainer.rb', line 22

def workspace
  @workspace
end

Instance Method Details

#docker_run_command(name:) ⇒ Array<String>

Generate full Docker run command array

Examples:

devcontainer.docker_run_command(name: "myapp-dev-1")
#=> ["docker", "run", "-d", "--name", "myapp-dev-1", "-p", "3000:3000", ..., "ruby:3.2"]


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/kamal/dev/devcontainer.rb', line 102

def docker_run_command(name:)
  command = ["docker", "run"]

  # Run in detached mode
  command << "-d"

  # Container name
  command << "--name"
  command << name

  # Add all flags
  command.concat(docker_run_flags)

  # Image must be last
  command << @image

  command
end

#docker_run_flagsArray<String>

Generate Docker run flags from configuration



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/kamal/dev/devcontainer.rb', line 49

def docker_run_flags
  flags = []

  # Port mappings (-p HOST:CONTAINER)
  @ports.each do |port|
    flags << "-p"
    flags << "#{port}:#{port}"
  end

  # Volume mounts (-v SOURCE:TARGET)
  @mounts.each do |mount|
    flags << "-v"
    flags << "#{mount[:source]}:#{mount[:target]}"
  end

  # Environment variables (-e KEY=VALUE)
  @env.each do |key, value|
    flags << "-e"
    flags << "#{key}=#{value}"
  end

  # Secrets (Base64-encoded) (-e KEY_B64=encoded_value)
  @secrets.each do |key, encoded_value|
    flags << "-e"
    flags << "#{key}_B64=#{encoded_value}"
  end

  # Docker run options (--cpus=2, --memory=4g, etc.)
  flags.concat(@options)

  # Remote user (--user USER)
  if @user
    flags << "--user"
    flags << @user
  end

  # Workspace folder (-w /workspace)
  if @workspace
    flags << "-w"
    flags << @workspace
  end

  flags
end