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