github.com/titanous/docker@v1.4.1/daemon/start.go (about)

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/docker/docker/engine"
     9  	"github.com/docker/docker/runconfig"
    10  )
    11  
    12  func (daemon *Daemon) ContainerStart(job *engine.Job) engine.Status {
    13  	if len(job.Args) < 1 {
    14  		return job.Errorf("Usage: %s container_id", job.Name)
    15  	}
    16  	var (
    17  		name      = job.Args[0]
    18  		container = daemon.Get(name)
    19  	)
    20  
    21  	if container == nil {
    22  		return job.Errorf("No such container: %s", name)
    23  	}
    24  
    25  	if container.IsRunning() {
    26  		return job.Errorf("Container already started")
    27  	}
    28  
    29  	// If no environment was set, then no hostconfig was passed.
    30  	// This is kept for backward compatibility - hostconfig should be passed when
    31  	// creating a container, not during start.
    32  	if len(job.Environ()) > 0 {
    33  		hostConfig := runconfig.ContainerHostConfigFromJob(job)
    34  		if err := daemon.setHostConfig(container, hostConfig); err != nil {
    35  			return job.Error(err)
    36  		}
    37  	}
    38  	if err := container.Start(); err != nil {
    39  		container.LogEvent("die")
    40  		return job.Errorf("Cannot start container %s: %s", name, err)
    41  	}
    42  
    43  	return engine.StatusOK
    44  }
    45  
    46  func (daemon *Daemon) setHostConfig(container *Container, hostConfig *runconfig.HostConfig) error {
    47  	if err := parseSecurityOpt(container, hostConfig); err != nil {
    48  		return err
    49  	}
    50  	// Validate the HostConfig binds. Make sure that:
    51  	// the source exists
    52  	for _, bind := range hostConfig.Binds {
    53  		splitBind := strings.Split(bind, ":")
    54  		source := splitBind[0]
    55  
    56  		// ensure the source exists on the host
    57  		_, err := os.Stat(source)
    58  		if err != nil && os.IsNotExist(err) {
    59  			err = os.MkdirAll(source, 0755)
    60  			if err != nil {
    61  				return fmt.Errorf("Could not create local directory '%s' for bind mount: %s!", source, err.Error())
    62  			}
    63  		}
    64  	}
    65  	// Register any links from the host config before starting the container
    66  	if err := daemon.RegisterLinks(container, hostConfig); err != nil {
    67  		return err
    68  	}
    69  	container.SetHostConfig(hostConfig)
    70  	container.ToDisk()
    71  
    72  	return nil
    73  }