github.com/xdlianrong208/docker-ce-comments@v17.12.1-ce-rc2+incompatible/components/engine/client/container_inspect.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"net/url"
     8  
     9  	"github.com/docker/docker/api/types"
    10  	"golang.org/x/net/context"
    11  )
    12  
    13  // ContainerInspect returns the container information.
    14  func (cli *Client) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
    15  	serverResp, err := cli.get(ctx, "/containers/"+containerID+"/json", nil, nil)
    16  	if err != nil {
    17  		return types.ContainerJSON{}, wrapResponseError(err, serverResp, "container", containerID)
    18  	}
    19  
    20  	var response types.ContainerJSON
    21  	err = json.NewDecoder(serverResp.body).Decode(&response)
    22  	ensureReaderClosed(serverResp)
    23  	return response, err
    24  }
    25  
    26  // ContainerInspectWithRaw returns the container information and its raw representation.
    27  func (cli *Client) ContainerInspectWithRaw(ctx context.Context, containerID string, getSize bool) (types.ContainerJSON, []byte, error) {
    28  	query := url.Values{}
    29  	if getSize {
    30  		query.Set("size", "1")
    31  	}
    32  	serverResp, err := cli.get(ctx, "/containers/"+containerID+"/json", query, nil)
    33  	if err != nil {
    34  		return types.ContainerJSON{}, nil, wrapResponseError(err, serverResp, "container", containerID)
    35  	}
    36  	defer ensureReaderClosed(serverResp)
    37  
    38  	body, err := ioutil.ReadAll(serverResp.body)
    39  	if err != nil {
    40  		return types.ContainerJSON{}, nil, err
    41  	}
    42  
    43  	var response types.ContainerJSON
    44  	rdr := bytes.NewReader(body)
    45  	err = json.NewDecoder(rdr).Decode(&response)
    46  	return response, body, err
    47  }