github.com/portworx/docker@v1.12.1/daemon/start.go (about) 1 package daemon 2 3 import ( 4 "fmt" 5 "net/http" 6 "runtime" 7 "strings" 8 "syscall" 9 10 "google.golang.org/grpc" 11 12 "github.com/Sirupsen/logrus" 13 "github.com/docker/docker/container" 14 "github.com/docker/docker/errors" 15 "github.com/docker/docker/libcontainerd" 16 "github.com/docker/docker/runconfig" 17 containertypes "github.com/docker/engine-api/types/container" 18 ) 19 20 // ContainerStart starts a container. 21 func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.HostConfig, validateHostname bool) error { 22 container, err := daemon.GetContainer(name) 23 if err != nil { 24 return err 25 } 26 27 if container.IsPaused() { 28 return fmt.Errorf("Cannot start a paused container, try unpause instead.") 29 } 30 31 if container.IsRunning() { 32 err := fmt.Errorf("Container already started") 33 return errors.NewErrorWithStatusCode(err, http.StatusNotModified) 34 } 35 36 // Windows does not have the backwards compatibility issue here. 37 if runtime.GOOS != "windows" { 38 // This is kept for backward compatibility - hostconfig should be passed when 39 // creating a container, not during start. 40 if hostConfig != nil { 41 logrus.Warn("DEPRECATED: Setting host configuration options when the container starts is deprecated and will be removed in Docker 1.12") 42 oldNetworkMode := container.HostConfig.NetworkMode 43 if err := daemon.setSecurityOptions(container, hostConfig); err != nil { 44 return err 45 } 46 if err := daemon.mergeAndVerifyLogConfig(&hostConfig.LogConfig); err != nil { 47 return err 48 } 49 if err := daemon.setHostConfig(container, hostConfig); err != nil { 50 return err 51 } 52 newNetworkMode := container.HostConfig.NetworkMode 53 if string(oldNetworkMode) != string(newNetworkMode) { 54 // if user has change the network mode on starting, clean up the 55 // old networks. It is a deprecated feature and will be removed in Docker 1.12 56 container.NetworkSettings.Networks = nil 57 if err := container.ToDisk(); err != nil { 58 return err 59 } 60 } 61 container.InitDNSHostConfig() 62 } 63 } else { 64 if hostConfig != nil { 65 return fmt.Errorf("Supplying a hostconfig on start is not supported. It should be supplied on create") 66 } 67 } 68 69 // check if hostConfig is in line with the current system settings. 70 // It may happen cgroups are umounted or the like. 71 if _, err = daemon.verifyContainerSettings(container.HostConfig, nil, false, validateHostname); err != nil { 72 return err 73 } 74 // Adapt for old containers in case we have updates in this function and 75 // old containers never have chance to call the new function in create stage. 76 if err := daemon.adaptContainerSettings(container.HostConfig, false); err != nil { 77 return err 78 } 79 80 return daemon.containerStart(container) 81 } 82 83 // Start starts a container 84 func (daemon *Daemon) Start(container *container.Container) error { 85 return daemon.containerStart(container) 86 } 87 88 // containerStart prepares the container to run by setting up everything the 89 // container needs, such as storage and networking, as well as links 90 // between containers. The container is left waiting for a signal to 91 // begin running. 92 func (daemon *Daemon) containerStart(container *container.Container) (err error) { 93 container.Lock() 94 defer container.Unlock() 95 96 if container.Running { 97 return nil 98 } 99 100 if container.RemovalInProgress || container.Dead { 101 return fmt.Errorf("Container is marked for removal and cannot be started.") 102 } 103 104 // if we encounter an error during start we need to ensure that any other 105 // setup has been cleaned up properly 106 defer func() { 107 if err != nil { 108 container.SetError(err) 109 // if no one else has set it, make sure we don't leave it at zero 110 if container.ExitCode() == 0 { 111 container.SetExitCode(128) 112 } 113 container.ToDisk() 114 daemon.Cleanup(container) 115 } 116 }() 117 118 if err := daemon.conditionalMountOnStart(container); err != nil { 119 return err 120 } 121 122 // Make sure NetworkMode has an acceptable value. We do this to ensure 123 // backwards API compatibility. 124 container.HostConfig = runconfig.SetDefaultNetModeIfBlank(container.HostConfig) 125 126 if err := daemon.initializeNetworking(container); err != nil { 127 return err 128 } 129 130 spec, err := daemon.createSpec(container) 131 if err != nil { 132 return err 133 } 134 135 createOptions := []libcontainerd.CreateOption{libcontainerd.WithRestartManager(container.RestartManager(true))} 136 copts, err := daemon.getLibcontainerdCreateOptions(container) 137 if err != nil { 138 return err 139 } 140 if copts != nil { 141 createOptions = append(createOptions, *copts...) 142 } 143 144 if err := daemon.containerd.Create(container.ID, *spec, createOptions...); err != nil { 145 errDesc := grpc.ErrorDesc(err) 146 logrus.Errorf("Create container failed with error: %s", errDesc) 147 // if we receive an internal error from the initial start of a container then lets 148 // return it instead of entering the restart loop 149 // set to 127 for container cmd not found/does not exist) 150 if strings.Contains(errDesc, container.Path) && 151 (strings.Contains(errDesc, "executable file not found") || 152 strings.Contains(errDesc, "no such file or directory") || 153 strings.Contains(errDesc, "system cannot find the file specified")) { 154 container.SetExitCode(127) 155 } 156 // set to 126 for container cmd can't be invoked errors 157 if strings.Contains(errDesc, syscall.EACCES.Error()) { 158 container.SetExitCode(126) 159 } 160 161 container.Reset(false) 162 163 return fmt.Errorf("%s", errDesc) 164 } 165 166 return nil 167 } 168 169 // Cleanup releases any network resources allocated to the container along with any rules 170 // around how containers are linked together. It also unmounts the container's root filesystem. 171 func (daemon *Daemon) Cleanup(container *container.Container) { 172 daemon.releaseNetwork(container) 173 174 container.UnmountIpcMounts(detachMounted) 175 176 if err := daemon.conditionalUnmountOnCleanup(container); err != nil { 177 // FIXME: remove once reference counting for graphdrivers has been refactored 178 // Ensure that all the mounts are gone 179 if mountid, err := daemon.layerStore.GetMountID(container.ID); err == nil { 180 daemon.cleanupMountsByID(mountid) 181 } 182 } 183 184 for _, eConfig := range container.ExecCommands.Commands() { 185 daemon.unregisterExecCommand(container, eConfig) 186 } 187 188 if container.BaseFS != "" { 189 if err := container.UnmountVolumes(false, daemon.LogVolumeEvent); err != nil { 190 logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err) 191 } 192 } 193 container.CancelAttachContext() 194 }