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
# File 'lib/docker-compose/models/compose_container.rb', line 9

def initialize(hash_attributes)
  @attributes = {
    label: 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])
  }.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)



190
191
192
# File 'lib/docker-compose/models/compose_container.rb', line 190

def add_dependency(dependency)
  @dependencies << dependency
end

#deleteObject

Delete the container



181
182
183
184
# File 'lib/docker-compose/models/compose_container.rb', line 181

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

#exist?Boolean

Check if the container exists or not

Returns:

  • (Boolean)


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

def exist?
  !@container.nil?
end

#killObject

Kill the container



174
175
176
# File 'lib/docker-compose/models/compose_container.rb', line 174

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

#running?Boolean

Check if a container is already running or not

Returns:

  • (Boolean)


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

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

#startObject

Start the container and its dependencies



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/docker-compose/models/compose_container.rb', line 149

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



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

def stats
  @container.json
end

#stopObject

Stop the container



167
168
169
# File 'lib/docker-compose/models/compose_container.rb', line 167

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