func (m *UpdateServiceSnapshotByContainer) Process() error { _, imageName := m.info.GetName() cached, err := utils.IsImageCached(imageName) if err != nil { fmt.Println(err) return err } if !cached { //TODO: remove this image to save server disk err := utils.PullImage(imageName) if err != nil { return err } } var config container.Config config.Image = imageName config.Cmd = []string{snapshotProcess, m.info.CallbackID, m.info.Host, m.info.DataProto} var hostConfig container.HostConfig hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s:%s", m.info.DataURL, snapshotMountDir)) containerName := "scan-" + m.info.CallbackID err = utils.StartContainer(config, hostConfig, containerName) return err }
func TestUtilsContainer(t *testing.T) { //TODO: dockyard dev team should provide small testing containers. imageName := "google/nodejs" containerName := "" cached, err := utils.IsImageCached(imageName) if err == utils.ErrorsNoDockerClient { fmt.Println("Please start a docker daemon to continue the container operation test") return } assert.Nil(t, err, "Fail to load Image") if !cached { err := utils.PullImage(imageName) assert.Nil(t, err, "Fail to pull image") } tmpFile, err := ioutil.TempFile("/tmp", "dockyard-test-container-oper") assert.Nil(t, err, "System err, fail to create temp file") defer os.Remove(tmpFile.Name()) var config container.Config config.Image = imageName config.Cmd = []string{"touch", tmpFile.Name()} var hostConfig container.HostConfig hostConfig.Binds = append(hostConfig.Binds, "/tmp:/tmp") utils.StartContainer(config, hostConfig, containerName) //TODO: stop, remove the container process assert.Equal(t, true, utils.IsFileExist(tmpFile.Name()), "Fail to touch file by using StartContainer") }
func (cli *DockerCli) createContainer(config *container.Config, hostConfig *container.HostConfig, networkingConfig *networktypes.NetworkingConfig, cidfile, name string) (*types.ContainerCreateResponse, error) { var containerIDFile *cidFile if cidfile != "" { var err error if containerIDFile, err = newCIDFile(cidfile); err != nil { return nil, err } defer containerIDFile.Close() } ref, err := reference.ParseNamed(config.Image) if err != nil { return nil, err } ref = reference.WithDefaultTag(ref) var trustedRef reference.Canonical 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() } //create the container response, err := cli.client.ContainerCreate(config, hostConfig, networkingConfig, name) //if image not found try to pull it if err != nil { if client.IsErrImageNotFound(err) { fmt.Fprintf(cli.err, "Unable to find image '%s' locally\n", ref.String()) // 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 } if ref, ok := ref.(reference.NamedTagged); ok && trustedRef != nil { if err := cli.tagTrusted(trustedRef, ref); err != nil { return nil, err } } // Retry var retryErr error response, retryErr = cli.client.ContainerCreate(config, hostConfig, networkingConfig, name) if retryErr != nil { return nil, retryErr } } else { 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 }