diff --git a/lib/docker/container.rb b/lib/docker/container.rb index 3c0cc044..5065df83 100644 --- a/lib/docker/container.rb +++ b/lib/docker/container.rb @@ -324,10 +324,15 @@ def store_file(path, file_content) archive_in_stream("/", overwrite: true) { output_io.read } end + # Keys that /containers/create takes as query parameters rather than in the + # request body. The daemon silently ignores them if they are sent in the + # body, so a misplaced key means the option has no effect at all. + CREATE_QUERY_KEYS = %w[name platform].freeze + # Create a new Container. def self.create(opts = {}, conn = Docker.connection) - query = opts.select {|key| ['name', :name].include?(key) } - clean_opts = opts.reject {|key| ['name', :name].include?(key) } + query = opts.select { |key| CREATE_QUERY_KEYS.include?(key.to_s) } + clean_opts = opts.reject { |key| CREATE_QUERY_KEYS.include?(key.to_s) } resp = conn.post('/containers/create', query, :body => MultiJson.dump(clean_opts)) hash = Docker::Util.parse_json(resp) || {} new(conn, hash) diff --git a/spec/docker/container_spec.rb b/spec/docker/container_spec.rb index d5c8a34f..45175280 100644 --- a/spec/docker/container_spec.rb +++ b/spec/docker/container_spec.rb @@ -163,6 +163,32 @@ expect(subject.json["Name"]).to eq("/bob") end end + + context 'when a platform is given' do + let(:opts) { {"platform" => "linux/amd64"} } + + # /containers/create takes platform as a query parameter. A "platform" + # body key is accepted and silently ignored by the daemon, so sending it + # in the body means the requested platform has no effect at all. + it 'sends the platform as a query parameter' do + expect(Docker.connection).to receive(:post).with( + '/containers/create', + hash_including('platform' => 'linux/amd64'), + anything + ).and_return('{"Id":"deadbeef"}') + + subject + end + + it 'does not send the platform in the body' do + expect(Docker.connection).to receive(:post) { |_path, _query, opts| + expect(MultiJson.load(opts[:body])).to_not have_key('platform') + '{"Id":"deadbeef"}' + } + + subject + end + end end describe '#rename' do