From 468db74f1413671ca92be75ae895af819433476e Mon Sep 17 00:00:00 2001 From: Michal Cichra Date: Wed, 5 Nov 2014 20:14:17 +0100 Subject: [PATCH] Fix Docker::Image.create when `m` is nil 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'. --- lib/docker/image.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/docker/image.rb b/lib/docker/image.rb index 0157b5685..641f4520b 100644 --- a/lib/docker/image.rb +++ b/lib/docker/image.rb @@ -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.