Docker and Ruby

Docker and Ruby make one powerful combination. docker-api offers great usability. Check it out at https://github.com/swipely/docker-api.

images

Creating an image

1
2
3
require 'docker'

image = Docker::Image.create('fromImage' => 'base')

Tagging an image

1
image.tag('repo' => 'base2', 'force' => true)

Get image information

1
image.json

Run an image

1
image.run('ls -l')

Removing an image

1
image.remove(:force => true)

Saving an image

1
2
3
image.save('my_export.tar')
#or
image.save

Create an image with an inline string Dockerfile!

1
Docker::Image.build("from base\nrun touch /test")

List all images

1
Docker::Image.all

Get specific image

1
Docker::Image.get('df4f1bdecf40')

Check if image exists

1
Docker::Image.exist?('ef723dcdac09')

containers

Create container and list files with ls from image myImage.

1
container = Docker::Container.create('Cmd' => ['ls'], 'Image' => 'myImage')

Same as image, list info

1
container.json

Start a container

1
container.start

Stop a container

1
container.stop

Restart a container

1
container.restart

Pause a container

1
container.pause

Resume container

1
container.unpause

Kill container

1
container.kill

Kill container gracefully

1
container.kill(:signal => "SIGHUP")

Container top

1
container.top

Copy filesystem data from container

1
container.copy('/etc/hosts') { |chunk| puts chunk }

_Attach to container

1
container.attach(:stream => true, :stdin => nil, :stdout => true, :stderr => true, :logs => true, :tty => false)

Running exec

1
container.exec('date')

Delete a container

1
container.delete(:force => true)

Get a container

1
Docker::Container.get('500f53b25e6e')

List all containers

1
Docker::Container.all(:all => true)