Skip to content

Commit

Permalink
Merge pull request #188 from mastahyeti/build_from_tar
Browse files Browse the repository at this point in the history
Build image from tar file
  • Loading branch information
tlunter committed Sep 29, 2014
2 parents dab64b9 + 090ece3 commit cef2d5f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Docker.authenticate!('username' => 'docker-fan-boi', 'password' => 'i<3docker',
```

## Images
Just about every method here has a one-to-one mapping with the [Images](https://docs.docker.com/reference/api/docker_remote_api_v1.12/#22-images) section of the API. If an API call accepts query parameters, these can be passed as an Hash to it's corresponding method. Also, note that `Docker::Image.new` is a private method, so you must use `.create`, `.build`, `.build_from_dir`, or `.import` to make an instance.
Just about every method here has a one-to-one mapping with the [Images](https://docs.docker.com/reference/api/docker_remote_api_v1.12/#22-images) section of the API. If an API call accepts query parameters, these can be passed as an Hash to it's corresponding method. Also, note that `Docker::Image.new` is a private method, so you must use `.create`, `.build`, `.build_from_dir`, `build_from_tar`, or `.import` to make an instance.

```ruby
require 'docker'
Expand Down Expand Up @@ -160,6 +160,10 @@ Docker::Image.build("from base\nrun touch /test")
Docker::Image.build_from_dir('.')
# => Docker::Image { :id => 1266dc19e, :connection => Docker::Connection { :url => tcp://localhost, :options => {:port=>2375} } }

# Create an Image from a tar file.
Docker::Image.build_from_tar(File.open('docker_image.tar', 'r'))
# => Docker::Image { :id => 1266dc19e, :connection => Docker::Connection { :url => tcp://localhost, :options => {:port=>2375} } }

# Load all Images on your Docker server.
Docker::Image.all
# => [Docker::Image { :id => b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc, :connection => Docker::Connection { :url => tcp://localhost, :options => {:port=>2375} } }, Docker::Image { :id => 8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c, :connection => Docker::Connection { :url => tcp://localhost, :options => {:port=>2375} } }]
Expand Down
27 changes: 18 additions & 9 deletions lib/docker/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,13 @@ def build(commands, opts = {}, connection = Docker.connection, &block)
raise Docker::Error::UnexpectedResponseError
end

# Given a directory that contains a Dockerfile, builds an Image.
# Given File like object containing a tar file, builds an Image.
#
# If a block is passed, chunks of output produced by Docker will be passed
# to that block.
def build_from_dir(dir, opts = {}, connection = Docker.connection,
def build_from_tar(tar, opts = {}, connection = Docker.connection,
creds = nil, &block)

tar = Docker::Util.create_dir_tar(dir)

headers = build_headers(creds)

# The response_block passed to Excon will build up this body variable.
Expand All @@ -182,13 +180,24 @@ def build_from_dir(dir, opts = {}, connection = Docker.connection,
new(connection,
'id' => Docker::Util.extract_id(body),
:headers => headers)
ensure
unless tar.nil?
tar.close
FileUtils.rm(tar.path, force: true)
end
end

# Given a directory that contains a Dockerfile, builds an Image.
#
# If a block is passed, chunks of output produced by Docker will be passed
# to that block.
def build_from_dir(dir, opts = {}, connection = Docker.connection,
creds = nil, &block)

tar = Docker::Util.create_dir_tar(dir)
build_from_tar tar, opts, connection, creds, &block
ensure
unless tar.nil?
tar.close
FileUtils.rm(tar.path, force: true)
end
end
end

private

Expand Down

0 comments on commit cef2d5f

Please sign in to comment.