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, docker_container = nil) ⇒ 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
27
28
29
30
# File 'lib/docker-compose/models/compose_container.rb', line 9

def initialize(hash_attributes, docker_container = nil)
  @attributes = {
    label: hash_attributes[:label],
    loaded_from_environment: hash_attributes[:loaded_from_environment] || false,
    name: hash_attributes[:full_name] || ComposeUtils.generate_container_name(hash_attributes[:name], hash_attributes[:label]),
    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]),
    labels: prepare_labels(hash_attributes[:labels])
  }.reject { |key, value| value.nil? }

  prepare_compose_labels

  # Docker client variables
  @internal_image = nil
  @container = docker_container
  @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

#internal_imageObject (readonly)

Returns the value of attribute internal_image.



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

def internal_image
  @internal_image
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)



251
252
253
# File 'lib/docker-compose/models/compose_container.rb', line 251

def add_dependency(dependency)
  @dependencies << dependency
end

#deleteObject

Delete the container



242
243
244
245
# File 'lib/docker-compose/models/compose_container.rb', line 242

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

#exist?Boolean

Check if the container exists or not

Returns:

  • (Boolean)


272
273
274
# File 'lib/docker-compose/models/compose_container.rb', line 272

def exist?
  !@container.nil?
end

#killObject

Kill the container



235
236
237
# File 'lib/docker-compose/models/compose_container.rb', line 235

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

#loaded_from_environment?Boolean

Returns true if is a container loaded from environment instead compose file (i.e. a running container)

Returns:

  • (Boolean)


36
37
38
# File 'lib/docker-compose/models/compose_container.rb', line 36

def loaded_from_environment?
  attributes[:loaded_from_environment]
end

#running?Boolean

Check if a container is already running or not

Returns:

  • (Boolean)


265
266
267
# File 'lib/docker-compose/models/compose_container.rb', line 265

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

#startObject

Start the container and its dependencies



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/docker-compose/models/compose_container.rb', line 210

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



258
259
260
# File 'lib/docker-compose/models/compose_container.rb', line 258

def stats
  @container.json
end

#stopObject

Stop the container



228
229
230
# File 'lib/docker-compose/models/compose_container.rb', line 228

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