2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2015-12-03 11:14:07 -05:00
|
|
|
"github.com/docker/docker/api/client/lib"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-12-18 13:36:17 -05:00
|
|
|
"github.com/docker/docker/api/types/container"
|
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"
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (cli *DockerCli) pullImage(image string) error {
|
|
|
|
return cli.pullImageCustomOut(image, cli.out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) pullImageCustomOut(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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve the Auth config relevant for this server
|
2015-12-06 15:17:34 -05:00
|
|
|
encodedAuth, err := cli.encodeRegistryAuth(repoInfo.Index)
|
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
|
|
|
|
2015-12-04 17:02:06 -05:00
|
|
|
responseBody, err := cli.client.ImageCreate(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()
|
|
|
|
|
|
|
|
return jsonmessage.DisplayJSONMessagesStream(responseBody, out, cli.outFd, cli.isTerminalOut)
|
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
|
|
|
|
}
|
|
|
|
|
2015-12-18 13:36:17 -05:00
|
|
|
func (cli *DockerCli) createContainer(config *container.Config, hostConfig *container.HostConfig, 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()
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
ref, err := reference.ParseNamed(config.Image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-07-15 16:42:45 -04:00
|
|
|
}
|
2015-12-10 14:01:34 -05:00
|
|
|
ref = reference.WithDefaultTag(ref)
|
2015-07-15 16:42:45 -04:00
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
var trustedRef reference.Canonical
|
|
|
|
|
2015-12-10 14:01:34 -05:00
|
|
|
if ref, ok := ref.(reference.NamedTagged); ok && isTrusted() {
|
2015-07-15 16:42:45 -04:00
|
|
|
var err error
|
2015-12-10 14:01:34 -05:00
|
|
|
trustedRef, err = cli.trustedReference(ref)
|
2015-07-15 16:42:45 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
config.Image = trustedRef.String()
|
2015-07-15 16:42:45 -04:00
|
|
|
}
|
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
//create the container
|
2015-12-18 13:36:17 -05:00
|
|
|
response, err := cli.client.ContainerCreate(config, hostConfig, name)
|
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 {
|
|
|
|
if lib.IsErrImageNotFound(err) {
|
|
|
|
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
|
|
|
|
if err = cli.pullImageCustomOut(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
|
2015-12-18 13:36:17 -05:00
|
|
|
response, retryErr = cli.client.ContainerCreate(config, hostConfig, 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")
|
|
|
|
)
|
|
|
|
|
|
|
|
config, hostConfig, cmd, err := runconfig.Parse(cmd, args)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
response, err := cli.createContainer(config, hostConfig, hostConfig.ContainerIDFile, *flName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintf(cli.out, "%s\n", response.ID)
|
|
|
|
return nil
|
|
|
|
}
|