2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-12-03 11:14:07 -05:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2015-12-04 16:55:15 -05:00
|
|
|
"github.com/docker/docker/reference"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/registry"
|
2015-12-21 20:05:55 -05:00
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/client"
|
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/container"
|
2016-01-07 19:18:34 -05:00
|
|
|
networktypes "github.com/docker/engine-api/types/network"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
2016-03-25 12:41:19 -04:00
|
|
|
func (cli *DockerCli) pullImage(image string, out io.Writer) error {
|
2015-11-18 17:20:54 -05:00
|
|
|
ref, err := reference.ParseNamed(image)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tag string
|
2015-12-10 14:01:34 -05:00
|
|
|
switch x := reference.WithDefaultTag(ref).(type) {
|
2015-12-04 16:55:15 -05:00
|
|
|
case reference.Canonical:
|
2015-11-18 17:20:54 -05:00
|
|
|
tag = x.Digest().String()
|
2015-12-04 16:55:15 -05:00
|
|
|
case reference.NamedTagged:
|
2015-11-18 17:20:54 -05:00
|
|
|
tag = x.Tag()
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
2015-11-18 17:20:54 -05:00
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(ref)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-07 19:55:17 -05:00
|
|
|
authConfig := cli.resolveAuthConfig(repoInfo.Index)
|
2016-01-24 11:59:24 -05:00
|
|
|
encodedAuth, err := encodeAuthToBase64(authConfig)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-04 17:02:06 -05:00
|
|
|
options := types.ImageCreateOptions{
|
2015-12-03 11:14:07 -05:00
|
|
|
Parent: ref.Name(),
|
|
|
|
Tag: tag,
|
2015-12-06 15:17:34 -05:00
|
|
|
RegistryAuth: encodedAuth,
|
2015-05-01 14:23:44 -04:00
|
|
|
}
|
2015-12-03 11:14:07 -05:00
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
responseBody, err := cli.client.ImageCreate(context.Background(), options)
|
2015-12-03 11:14:07 -05:00
|
|
|
if err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-12-03 11:14:07 -05:00
|
|
|
defer responseBody.Close()
|
|
|
|
|
2015-12-21 18:02:44 -05:00
|
|
|
return jsonmessage.DisplayJSONMessagesStream(responseBody, out, cli.outFd, cli.isTerminalOut, nil)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type cidFile struct {
|
|
|
|
path string
|
|
|
|
file *os.File
|
|
|
|
written bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCIDFile(path string) (*cidFile, error) {
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
return nil, fmt.Errorf("Container ID file found, make sure the other container isn't running or delete %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to create the container ID file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cidFile{path: path, file: f}, nil
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:18:34 -05:00
|
|
|
func (cli *DockerCli) createContainer(config *container.Config, hostConfig *container.HostConfig, networkingConfig *networktypes.NetworkingConfig, cidfile, name string) (*types.ContainerCreateResponse, error) {
|
2015-03-24 23:57:23 -04:00
|
|
|
var containerIDFile *cidFile
|
|
|
|
if cidfile != "" {
|
|
|
|
var err error
|
|
|
|
if containerIDFile, err = newCIDFile(cidfile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer containerIDFile.Close()
|
|
|
|
}
|
|
|
|
|
2016-03-07 13:56:24 -05:00
|
|
|
var trustedRef reference.Canonical
|
|
|
|
_, ref, err := reference.ParseIDOrReference(config.Image)
|
2015-11-18 17:20:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-07-15 16:42:45 -04:00
|
|
|
}
|
2016-03-07 13:56:24 -05:00
|
|
|
if ref != nil {
|
|
|
|
ref = reference.WithDefaultTag(ref)
|
2015-11-18 17:20:54 -05:00
|
|
|
|
2016-03-07 13:56:24 -05:00
|
|
|
if ref, ok := ref.(reference.NamedTagged); ok && isTrusted() {
|
|
|
|
var err error
|
|
|
|
trustedRef, err = cli.trustedReference(ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Image = trustedRef.String()
|
2015-07-15 16:42:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
//create the container
|
2016-03-16 15:19:22 -04:00
|
|
|
response, err := cli.client.ContainerCreate(context.Background(), config, hostConfig, networkingConfig, name)
|
2016-01-07 19:18:34 -05:00
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
//if image not found try to pull it
|
2015-12-03 14:14:36 -05:00
|
|
|
if err != nil {
|
2016-03-07 13:56:24 -05:00
|
|
|
if client.IsErrImageNotFound(err) && ref != nil {
|
2015-12-03 14:14:36 -05:00
|
|
|
fmt.Fprintf(cli.err, "Unable to find image '%s' locally\n", ref.String())
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-03 14:14:36 -05:00
|
|
|
// we don't want to write to stdout anything apart from container.ID
|
2016-03-25 12:41:19 -04:00
|
|
|
if err = cli.pullImage(config.Image, cli.err); err != nil {
|
2015-07-15 16:42:45 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-10 14:01:34 -05:00
|
|
|
if ref, ok := ref.(reference.NamedTagged); ok && trustedRef != nil {
|
|
|
|
if err := cli.tagTrusted(trustedRef, ref); err != nil {
|
2015-12-03 14:14:36 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Retry
|
|
|
|
var retryErr error
|
2016-03-16 15:19:22 -04:00
|
|
|
response, retryErr = cli.client.ContainerCreate(context.Background(), config, hostConfig, networkingConfig, name)
|
2015-12-03 14:14:36 -05:00
|
|
|
if retryErr != nil {
|
|
|
|
return nil, retryErr
|
|
|
|
}
|
|
|
|
} else {
|
2015-03-24 23:57:23 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, warning := range response.Warnings {
|
|
|
|
fmt.Fprintf(cli.err, "WARNING: %s\n", warning)
|
|
|
|
}
|
|
|
|
if containerIDFile != nil {
|
|
|
|
if err = containerIDFile.Write(response.ID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &response, nil
|
|
|
|
}
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdCreate creates a new container from a given image.
|
|
|
|
//
|
|
|
|
// Usage: docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdCreate(args ...string) error {
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd := Cli.Subcmd("create", []string{"IMAGE [COMMAND] [ARG...]"}, Cli.DockerCommands["create"].Description, true)
|
2015-07-15 16:42:45 -04:00
|
|
|
addTrustedFlags(cmd, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
// These are flags not stored in Config/HostConfig
|
|
|
|
var (
|
|
|
|
flName = cmd.String([]string{"-name"}, "", "Assign a name to the container")
|
|
|
|
)
|
|
|
|
|
2016-01-07 19:18:34 -05:00
|
|
|
config, hostConfig, networkingConfig, cmd, err := runconfigopts.Parse(cmd, args)
|
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ReportError(err.Error(), true)
|
2015-03-31 03:11:03 -04:00
|
|
|
os.Exit(1)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
if config.Image == "" {
|
|
|
|
cmd.Usage()
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-07 19:18:34 -05:00
|
|
|
response, err := cli.createContainer(config, hostConfig, networkingConfig, hostConfig.ContainerIDFile, *flName)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintf(cli.out, "%s\n", response.ID)
|
|
|
|
return nil
|
|
|
|
}
|