2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2015-03-27 21:07:20 -04:00
|
|
|
"strings"
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-05-11 13:23:32 -04:00
|
|
|
"github.com/docker/docker/graph/tags"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
|
|
|
"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 {
|
|
|
|
v := url.Values{}
|
|
|
|
repos, tag := parsers.ParseRepositoryTag(image)
|
|
|
|
// pull only the image tagged 'latest' if no tag was specified
|
|
|
|
if tag == "" {
|
2015-07-29 19:45:47 -04:00
|
|
|
tag = tags.DefaultTag
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
v.Set("fromImage", repos)
|
|
|
|
v.Set("tag", tag)
|
|
|
|
|
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(repos)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve the Auth config relevant for this server
|
2015-04-22 08:06:58 -04:00
|
|
|
authConfig := registry.ResolveAuthConfig(cli.configFile, repoInfo.Index)
|
2015-03-24 23:57:23 -04:00
|
|
|
buf, err := json.Marshal(authConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
registryAuthHeader := []string{
|
|
|
|
base64.URLEncoding.EncodeToString(buf),
|
|
|
|
}
|
2015-05-01 14:23:44 -04:00
|
|
|
sopts := &streamOpts{
|
|
|
|
rawTerminal: true,
|
|
|
|
out: out,
|
|
|
|
headers: map[string][]string{"X-Registry-Auth": registryAuthHeader},
|
|
|
|
}
|
2015-06-04 09:30:14 -04:00
|
|
|
if _, err := cli.stream("POST", "/images/create?"+v.Encode(), sopts); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) createContainer(config *runconfig.Config, hostConfig *runconfig.HostConfig, cidfile, name string) (*types.ContainerCreateResponse, error) {
|
|
|
|
containerValues := url.Values{}
|
|
|
|
if name != "" {
|
|
|
|
containerValues.Set("name", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
mergedConfig := runconfig.MergeConfigs(config, hostConfig)
|
|
|
|
|
|
|
|
var containerIDFile *cidFile
|
|
|
|
if cidfile != "" {
|
|
|
|
var err error
|
|
|
|
if containerIDFile, err = newCIDFile(cidfile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer containerIDFile.Close()
|
|
|
|
}
|
|
|
|
|
2015-07-15 16:42:45 -04:00
|
|
|
repo, tag := parsers.ParseRepositoryTag(config.Image)
|
|
|
|
if tag == "" {
|
2015-07-29 19:45:47 -04:00
|
|
|
tag = tags.DefaultTag
|
2015-07-15 16:42:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ref := registry.ParseReference(tag)
|
|
|
|
var trustedRef registry.Reference
|
|
|
|
|
|
|
|
if isTrusted() && !ref.HasDigest() {
|
|
|
|
var err error
|
|
|
|
trustedRef, err = cli.trustedReference(repo, ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Image = trustedRef.ImageName(repo)
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
//create the container
|
2015-07-09 22:05:50 -04:00
|
|
|
serverResp, err := cli.call("POST", "/containers/create?"+containerValues.Encode(), mergedConfig, nil)
|
2015-03-24 23:57:23 -04:00
|
|
|
//if image not found try to pull it
|
2015-07-09 22:05:50 -04:00
|
|
|
if serverResp.statusCode == 404 && strings.Contains(err.Error(), config.Image) {
|
2015-07-15 16:42:45 -04:00
|
|
|
fmt.Fprintf(cli.err, "Unable to find image '%s' locally\n", ref.ImageName(repo))
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
// we don't want to write to stdout anything apart from container.ID
|
|
|
|
if err = cli.pullImageCustomOut(config.Image, cli.err); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-15 16:42:45 -04:00
|
|
|
if trustedRef != nil && !ref.HasDigest() {
|
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := cli.tagTrusted(repoInfo, trustedRef, ref); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
// Retry
|
2015-07-09 22:05:50 -04:00
|
|
|
if serverResp, err = cli.call("POST", "/containers/create?"+containerValues.Encode(), mergedConfig, nil); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-09 22:05:50 -04:00
|
|
|
defer serverResp.body.Close()
|
2015-07-03 02:19:23 -04:00
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
var response types.ContainerCreateResponse
|
2015-07-09 22:05:50 -04:00
|
|
|
if err := json.NewDecoder(serverResp.body).Decode(&response); err != nil {
|
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-05-05 00:18:28 -04:00
|
|
|
cmd := Cli.Subcmd("create", []string{"IMAGE [COMMAND] [ARG...]"}, "Create a new container", 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
|
|
|
|
}
|