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.



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

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

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#containerObject (readonly)

Returns the value of attribute container.



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

def container
  @container
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



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

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)



219
220
221
# File 'lib/docker-compose/models/compose_container.rb', line 219

def add_dependency(dependency)
  @dependencies << dependency
end

#deleteObject

Delete the container



210
211
212
213
# File 'lib/docker-compose/models/compose_container.rb', line 210

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

#exist?Boolean

Check if the container exists or not

Returns:

  • (Boolean)


240
241
242
# File 'lib/docker-compose/models/compose_container.rb', line 240

def exist?
  !@container.nil?
end

#killObject

Kill the container



203
204
205
# File 'lib/docker-compose/models/compose_container.rb', line 203

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

#running?Boolean

Check if a container is already running or not

Returns:

  • (Boolean)


233
234
235
# File 'lib/docker-compose/models/compose_container.rb', line 233

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

#startObject

Start the container and its dependencies



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/docker-compose/models/compose_container.rb', line 178

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



226
227
228
# File 'lib/docker-compose/models/compose_container.rb', line 226

def stats
  @container.json
end

#stopObject

Stop the container



196
197
198
# File 'lib/docker-compose/models/compose_container.rb', line 196

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