github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/client/container_inspect.go (about)

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