Class: ComposeContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/docker-compose/models/compose_container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash_attributes) ⇒ ComposeContainer

Returns a new instance of ComposeContainer.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/docker-compose/models/compose_container.rb', line 8

def initialize(hash_attributes)
  @attributes = {
    label: hash_attributes[:label],
    image: ComposeUtils.format_image(hash_attributes[:image]),
    build: hash_attributes[:build],
    links: hash_attributes[:links],
    ports: prepare_ports(hash_attributes[:ports]),
    volumes: hash_attributes[:volumes],
    command: ComposeUtils.format_command(hash_attributes[:command]),
    environment: hash_attributes[:environment]
  }.reject{ |key, value| value.nil? }

  # Docker client variables
  @container = nil
  @dependencies = []
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/docker-compose/models/compose_container.rb', line 6

def attributes
  @attributes
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



6
7
8
# File 'lib/docker-compose/models/compose_container.rb', line 6

def dependencies
  @dependencies
end

Instance Method Details

#add_dependency(dependency) ⇒ Object

Add a dependency to this container (i.e. a container that must be started before this one)



158
159
160
# File 'lib/docker-compose/models/compose_container.rb', line 158

def add_dependency(dependency)
  @dependencies << dependency
end

#deleteObject

Delete the container



149
150
151
152
# File 'lib/docker-compose/models/compose_container.rb', line 149

def delete
  @container.delete(:force => true) unless @container.nil?
  @container = nil
end

#killObject

Kill the container



142
143
144
# File 'lib/docker-compose/models/compose_container.rb', line 142

def kill
  @container.kill unless @container.nil?
end

#running?Boolean

Check if a container is already running or not

Returns:

  • (Boolean)


172
173
174
# File 'lib/docker-compose/models/compose_container.rb', line 172

def running?
  @container.nil? ? false : self.stats['State']['Running']
end

#startObject

Start the container and its dependencies



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/docker-compose/models/compose_container.rb', line 117

def start
  # Start dependencies
  @dependencies.each do |dependency|
    dependency.start unless dependency.running?
  end

  # Create a container object
  if @container.nil?
    prepare_image
    prepare_container
  end

  @container.start unless @container.nil?
end

#statsObject

Return container statistics



165
166
167
# File 'lib/docker-compose/models/compose_container.rb', line 165

def stats
  @container.json
end

#stopObject

Stop the container



135
136
137
# File 'lib/docker-compose/models/compose_container.rb', line 135

def stop
  @container.kill unless @container.nil?
end