Esempio n. 1
0
func (c *Container) createContainer(imageName, oldContainer string, configOverride *config.ServiceConfig) (*types.Container, error) {
	serviceConfig := c.service.serviceConfig
	if configOverride != nil {
		serviceConfig.Command = configOverride.Command
		serviceConfig.Tty = configOverride.Tty
		serviceConfig.StdinOpen = configOverride.StdinOpen
	}
	configWrapper, err := ConvertToAPI(c.service)
	if err != nil {
		return nil, err
	}

	configWrapper.Config.Image = imageName

	if configWrapper.Config.Labels == nil {
		configWrapper.Config.Labels = map[string]string{}
	}

	configWrapper.Config.Labels[SERVICE.Str()] = c.service.name
	configWrapper.Config.Labels[PROJECT.Str()] = c.service.context.Project.Name
	configWrapper.Config.Labels[HASH.Str()] = c.getHash()
	// libcompose run command not yet supported, so always "False"
	configWrapper.Config.Labels[ONEOFF.Str()] = "False"
	configWrapper.Config.Labels[NUMBER.Str()] = getContainerNumber(c)
	configWrapper.Config.Labels[VERSION.Str()] = ComposeVersion

	err = c.populateAdditionalHostConfig(configWrapper.HostConfig)
	if err != nil {
		return nil, err
	}

	if oldContainer != "" {
		info, err := c.client.ContainerInspect(context.Background(), oldContainer)
		if err != nil {
			return nil, err
		}
		configWrapper.HostConfig.Binds = util.Merge(configWrapper.HostConfig.Binds, volumeBinds(configWrapper.Config.Volumes, &info))
	}

	logrus.Debugf("Creating container %s %#v", c.name, configWrapper)

	container, err := c.client.ContainerCreate(context.Background(), configWrapper.Config, configWrapper.HostConfig, configWrapper.NetworkingConfig, c.name)
	if err != nil {
		if client.IsErrImageNotFound(err) {
			logrus.Debugf("Not Found, pulling image %s", configWrapper.Config.Image)
			if err = c.pull(configWrapper.Config.Image); err != nil {
				return nil, err
			}
			if container, err = c.client.ContainerCreate(context.Background(), configWrapper.Config, configWrapper.HostConfig, configWrapper.NetworkingConfig, c.name); err != nil {
				return nil, err
			}
		} else {
			logrus.Debugf("Failed to create container %s: %v", c.name, err)
			return nil, err
		}
	}

	return GetContainerByID(c.client, container.ID)
}
Esempio n. 2
0
func (c *Container) createContainer(imageName, oldContainer string) (*dockerclient.APIContainers, error) {
	createOpts, err := ConvertToAPI(c.service, c.name)
	if err != nil {
		return nil, err
	}

	createOpts.Config.Image = imageName

	if createOpts.Config.Labels == nil {
		createOpts.Config.Labels = map[string]string{}
	}

	createOpts.Config.Labels[NAME.Str()] = c.name
	createOpts.Config.Labels[SERVICE.Str()] = c.service.name
	createOpts.Config.Labels[PROJECT.Str()] = c.service.context.Project.Name
	createOpts.Config.Labels[HASH.Str()] = c.getHash()

	err = c.populateAdditionalHostConfig(createOpts.HostConfig)
	if err != nil {
		return nil, err
	}

	if oldContainer != "" {
		info, err := c.client.InspectContainer(oldContainer)
		if err != nil {
			return nil, err
		}
		createOpts.HostConfig.Binds = util.Merge(createOpts.HostConfig.Binds, volumeBinds(createOpts.Config.Volumes, info))
	}

	logrus.Debugf("Creating container %s %#v", c.name, createOpts)

	container, err := c.client.CreateContainer(*createOpts)
	if err != nil && err == dockerclient.ErrNoSuchImage {
		logrus.Debugf("Not Found, pulling image %s", createOpts.Config.Image)
		if err = c.pull(createOpts.Config.Image); err != nil {
			return nil, err
		}
		if container, err = c.client.CreateContainer(*createOpts); err != nil {
			return nil, err
		}
	}

	if err != nil {
		logrus.Debugf("Failed to create container %s: %v", c.name, err)
		return nil, err
	}

	return GetContainerByID(c.client, container.ID)
}
Esempio n. 3
0
func (c *Container) createContainer(imageName, oldContainer string) (*types.Container, error) {
	configWrapper, err := ConvertToAPI(c.service)
	if err != nil {
		return nil, err
	}

	configWrapper.Config.Image = imageName

	if configWrapper.Config.Labels == nil {
		configWrapper.Config.Labels = map[string]string{}
	}

	configWrapper.Config.Labels[NAME.Str()] = c.name
	configWrapper.Config.Labels[SERVICE.Str()] = c.service.name
	configWrapper.Config.Labels[PROJECT.Str()] = c.service.context.Project.Name
	configWrapper.Config.Labels[HASH.Str()] = c.getHash()

	err = c.populateAdditionalHostConfig(configWrapper.HostConfig)
	if err != nil {
		return nil, err
	}

	if oldContainer != "" {
		info, err := c.client.ContainerInspect(context.Background(), oldContainer)
		if err != nil {
			return nil, err
		}
		configWrapper.HostConfig.Binds = util.Merge(configWrapper.HostConfig.Binds, volumeBinds(configWrapper.Config.Volumes, &info))
	}

	logrus.Debugf("Creating container %s %#v", c.name, configWrapper)

	container, err := c.client.ContainerCreate(context.Background(), configWrapper.Config, configWrapper.HostConfig, configWrapper.NetworkingConfig, c.name)
	if err != nil {
		if client.IsErrImageNotFound(err) {
			logrus.Debugf("Not Found, pulling image %s", configWrapper.Config.Image)
			if err = c.pull(configWrapper.Config.Image); err != nil {
				return nil, err
			}
			if container, err = c.client.ContainerCreate(context.Background(), configWrapper.Config, configWrapper.HostConfig, configWrapper.NetworkingConfig, c.name); err != nil {
				return nil, err
			}
		} else {
			logrus.Debugf("Failed to create container %s: %v", c.name, err)
			return nil, err
		}
	}

	return GetContainerByID(c.client, container.ID)
}
Esempio n. 4
0
func (s *Service) createContainer(ctx context.Context, namer Namer, oldContainer string, configOverride *config.ServiceConfig, oneOff bool) (*composecontainer.Container, error) {
	serviceConfig := s.serviceConfig
	if configOverride != nil {
		serviceConfig.Command = configOverride.Command
		serviceConfig.Tty = configOverride.Tty
		serviceConfig.StdinOpen = configOverride.StdinOpen
	}
	configWrapper, err := ConvertToAPI(serviceConfig, s.context.Context, s.clientFactory)
	if err != nil {
		return nil, err
	}
	configWrapper.Config.Image = s.imageName()

	containerName, containerNumber := namer.Next()

	configWrapper.Config.Labels[labels.SERVICE.Str()] = s.name
	configWrapper.Config.Labels[labels.PROJECT.Str()] = s.project.Name
	configWrapper.Config.Labels[labels.HASH.Str()] = config.GetServiceHash(s.name, serviceConfig)
	configWrapper.Config.Labels[labels.ONEOFF.Str()] = strings.Title(strconv.FormatBool(oneOff))
	configWrapper.Config.Labels[labels.NUMBER.Str()] = fmt.Sprintf("%d", containerNumber)
	configWrapper.Config.Labels[labels.VERSION.Str()] = project.ComposeVersion

	err = s.populateAdditionalHostConfig(configWrapper.HostConfig)
	if err != nil {
		return nil, err
	}

	// FIXME(vdemeester): oldContainer should be a Container instead of a string
	client := s.clientFactory.Create(s)
	if oldContainer != "" {
		info, err := client.ContainerInspect(ctx, oldContainer)
		if err != nil {
			return nil, err
		}
		configWrapper.HostConfig.Binds = util.Merge(configWrapper.HostConfig.Binds, volumeBinds(configWrapper.Config.Volumes, &info))
	}

	logrus.Debugf("Creating container %s %#v", containerName, configWrapper)
	// FIXME(vdemeester): long-term will be container.Create(…)
	container, err := composecontainer.Create(ctx, client, containerName, configWrapper.Config, configWrapper.HostConfig, configWrapper.NetworkingConfig)
	if err != nil {
		return nil, err
	}
	s.project.Notify(events.ContainerCreated, s.name, map[string]string{
		"name": containerName,
	})
	return container, nil
}