Skip to content

Commit

Permalink
Fix Docker::Image.create when m is nil
Browse files Browse the repository at this point in the history
Sometime when pulling for second time the API responds
with nil somewhere, leading to:

       undefined method `[]' for nil:NilClass
     # /var/lib/gems/2.1.0/gems/docker-api-1.13.7/lib/docker/image.rb:102:in `create'

Using reverse_each + find is O(1) compared to O(n) of select + last.
Also splitting it to several lines so there is no call on nil when
there is no image with 'id'.
  • Loading branch information
mikz authored and Michal Cichra committed Dec 3, 2014
1 parent 004f4ab commit 468db74
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/docker/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ def create(opts = {}, creds = nil, conn = Docker.connection)
headers = !credentials.nil? && Docker::Util.build_auth_header(credentials)
headers ||= {}
body = conn.post('/images/create', opts, :headers => headers)
id = Docker::Util.fix_json(body).select { |m| m['id'] }.last['id']
new(conn, 'id' => id, :headers => headers)
json = Docker::Util.fix_json(body)
image = json.reverse_each.find { |el| el && el.key?('id') }
new(conn, 'id' => image && image.fetch('id'), :headers => headers)
end

# Return a specific image.
Expand Down

0 comments on commit 468db74

Please sign in to comment.