github.com/olljanat/moby@v1.13.1/client/container_inspect.go (about)

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