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