Module: Dockerspec::Builder::ConfigHelpers

Included in:
Dockerspec::Builder
Defined in:
lib/dockerspec/builder/config_helpers.rb

Overview

Some helpers to get container image information from its JSON data.

Note: Keep in mind that not all the available Dockerfile instructions can be checked in the docker image. You should use Serverspec::RSpec::Resources#docker_run to check some instructions like FROM, RUN, ADD and COPY (see the examples there).

Instance Method Summary collapse

Instance Method Details

#architectureString Also known as: arch

Returns the image architecture.

Examples:

RSpec Example

describe docker_build(path: '.') do
  its(:arch) { should eq 'amd64' }
end

Returns:

  • (String)

    The architecture name.



71
72
73
# File 'lib/dockerspec/builder/config_helpers.rb', line 71

def architecture
  @image.json['Architecture']
end

#cmdArray

Returns the image command (CMD).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:cmd) { should eq ['/usr/bin/supervisord'] }
end

RSpec Example Using Have Matchers

describe docker_build(path: '.') do
  it { should have_cmd ['/usr/bin/supervisord'] }
  # Or in string format:
  it { should have_cmd '/usr/bin/supervisord' }
end

Returns:

  • (Array)

    The image command.



138
139
140
# File 'lib/dockerspec/builder/config_helpers.rb', line 138

def cmd
  image_config['Cmd']
end

#entrypointArray

Returns the image entrypoint (ENTRYPOINT).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:entrypoint) { should eq ['/entrypoint.sh'] }
end

RSpec Example Using Have Matchers

describe docker_build(path: '.') do
  it { should have_entrypoint ['/entrypoint.sh'] }
  # Or in string format:
  it { should have_entrypoint '/entrypoint.sh' }
end

Returns:

  • (Array)

    The image entrypoint.



282
283
284
# File 'lib/dockerspec/builder/config_helpers.rb', line 282

def entrypoint
  image_config['Entrypoint']
end

#envsHash Also known as: env

Returns the image environment (ENV).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:env) { should include 'container' => 'docker' }
end

RSpec Example Using Have Matchers

describe docker_build(path: '.') do
  it { should have_env 'container' => 'docker' }
end

RSpec Example Checking Only the Existence of the Env Variable

describe docker_build(path: '.') do
  it { should have_env 'container' }
end

Returns:

  • (Hash)

    The environment.



253
254
255
256
257
258
259
# File 'lib/dockerspec/builder/config_helpers.rb', line 253

def envs
  @env ||=
    image_config['Env'].each_with_object({}) do |var, memo|
      key, value = var.split('=', 2)
      memo[key] = value
    end
end

#exposeString

Returns the first exposed port (EXPOSE).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:expose) { should eq '80' }
end

Returns:

  • (String)

    The exposed port.



227
228
229
# File 'lib/dockerspec/builder/config_helpers.rb', line 227

def expose
  exposes.first
end

#exposesArray

Returns the image exposed ports (EXPOSE).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:exposes) { should include '80' }
end

RSpec Example Using Have Matchers

describe docker_build(path: '.') do
  it { should have_expose '80' }
end

RSpec Example Using Have Matchers with Integer Valuess

describe docker_build(path: '.') do
  it { should have_expose 80 }
end

RSpec Example Using Regular Expressions

describe docker_build(path: '.') do
  it { should have_expose(/80$/) }
end

Returns:

  • (Array)

    The exposed ports list.



211
212
213
# File 'lib/dockerspec/builder/config_helpers.rb', line 211

def exposes
  image_config['ExposedPorts'].keys.map { |x| x.delete('/tcp') }
end

#image_configHash

Returns the image configuration.

Returns:

  • (Hash)

    The image configuration.



39
40
41
# File 'lib/dockerspec/builder/config_helpers.rb', line 39

def image_config
  @image.json['Config']
end

#labelString

Returns the first label as a string (LABEL).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:label) { should eq 'description=My Container' }
end

Returns:

  • (String)

    The first label.



180
181
182
# File 'lib/dockerspec/builder/config_helpers.rb', line 180

def label
  labels.first.join('=')
end

#labelsHash

Returns the image labels (LABEL).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:labels) { should include 'description' => 'My Container' }
end

RSpec Example Using Have Matchers

describe docker_build(path: '.') do
  it { should have_label 'description' => 'My Container' }
end

RSpec Example Checking Only the Existence of the Label

describe docker_build(path: '.') do
  it { should have_label 'description' }
end

Returns:

  • (Hash)

    The labels list.



164
165
166
# File 'lib/dockerspec/builder/config_helpers.rb', line 164

def labels
  image_config['Labels']
end

#maintainerString

Returns the image maintainer or author (MAINTAINER).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:maintainer) { should eq 'John Doe "[email protected]"' }
end

RSpec Example Using Have Matchers

describe docker_build(path: '.') do
  it { should have_maintainer 'John Doe "[email protected]"' }
end

RSpec Example Using a Regular Expression

describe docker_build(path: '.') do
  it { should have_maintainer(/John Doe/) }
end

Returns:

  • (String)

    The maintainer.



115
116
117
# File 'lib/dockerspec/builder/config_helpers.rb', line 115

def maintainer
  @image.json['Author']
end

#onbuildString

Returns the first onbuild instruction (ONBUILD).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:onbuild) { should eq 'RUN echo onbuild' }
end

Returns:

  • (String)

    The onbuild instruction.



408
409
410
# File 'lib/dockerspec/builder/config_helpers.rb', line 408

def onbuild
  onbuilds.first
end

#onbuildsArray

Returns the onbuild instructions (ONBUILD).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:onbuilds) { should include 'RUN echo onbuild' }
end

RSpec Example Using Have Matchers

describe docker_build(path: '.') do
  it { should have_onbuild 'RUN echo onbuild' }
end

Returns:

  • (Array)

    The onbuild instructions.



392
393
394
# File 'lib/dockerspec/builder/config_helpers.rb', line 392

def onbuilds
  image_config['OnBuild']
end

#osString

Returns the image Operating System.

Examples:

RSpec Example

describe docker_build(path: '.') do
  its(:os) { should eq 'linux' }
end

Returns:

  • (String)

    The OS name.



89
90
91
# File 'lib/dockerspec/builder/config_helpers.rb', line 89

def os
  @image.json['Os']
end

#sizeInteger

Returns the image size in bytes.

Examples:

RSpec Example

describe docker_build(path: '.') do
  its(:size) { should be < 20 * 2**20 } # 20M
end

Returns:

  • (Integer)

    The image size in bytes.



55
56
57
# File 'lib/dockerspec/builder/config_helpers.rb', line 55

def size
  @image.json['VirtualSize']
end

#stopsignalString

Returns the stop signal (STOPSIGNAL).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:stopsignal) { should eq 'SIGTERM' }
end

RSpec Example Using Have Matchers

describe docker_build(path: '.') do
  it { should have_stopsignal 'SIGTERM' }
end

Returns:

  • (String)

    The stop signal name.



429
430
431
# File 'lib/dockerspec/builder/config_helpers.rb', line 429

def stopsignal
  image_config['StopSignal']
end

#userString

Returns the image user (USER).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:user) { should eq 'nobody' }
end

RSpec Example Using Have Matchers

describe docker_build(path: '.') do
  it { should have_user 'nobody' }
end

Returns:

  • (String)

    The username.



345
346
347
# File 'lib/dockerspec/builder/config_helpers.rb', line 345

def user
  image_config['User']
end

#volumeString

Returns the first volume (VOLUME).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:volume) { should eq '/data1' }
end

Returns:

  • (String)

    The first volume.



324
325
326
# File 'lib/dockerspec/builder/config_helpers.rb', line 324

def volume
  volumes.first
end

#volumesArray

Returns the image volumes (VOLUME).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:volumes) { should include '/data1' }
end

RSpec Example Using Have Matchers

describe docker_build(path: '.') do
  it { should have_volume '/data1' }
end

RSpec Example Using Regular Expressions

describe docker_build(path: '.') do
  it { should have_volume %r{^/data[0-9]+$} }
end

Returns:

  • (Array)

    The image volume list.



308
309
310
# File 'lib/dockerspec/builder/config_helpers.rb', line 308

def volumes
  image_config['Volumes'].keys
end

#workdirString

Returns the image workdir (WORKDIR).

Examples:

Basic RSpec Example

describe docker_build(path: '.') do
  its(:workdir) { should eq '/opt/myapp' }
end

RSpec Example Using Have Matchers

describe docker_build(path: '.') do
  it { should have_workdir '/opt/myapp' }
end

RSpec Example Using Regular Expressions

describe docker_build(path: '.') do
  it { should have_workdir %r{^/opt/myapp} }
end

Returns:

  • (String)

    The workdir.



371
372
373
# File 'lib/dockerspec/builder/config_helpers.rb', line 371

def workdir
  image_config['WorkingDir']
end