github.com/jen20/docker@v1.13.1/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 apierrors "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, checkpoint string, checkpointDir string) error { 23 if checkpoint != "" && !daemon.HasExperimental() { 24 return apierrors.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 apierrors.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); 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 hostConfig != nil { 82 if err := daemon.adaptContainerSettings(container.HostConfig, false); err != nil { 83 return err 84 } 85 } 86 87 return daemon.containerStart(container, checkpoint, checkpointDir, true) 88 } 89 90 // Start starts a container 91 func (daemon *Daemon) Start(container *container.Container) error { 92 return daemon.containerStart(container, "", "", true) 93 } 94 95 // containerStart prepares the container to run by setting up everything the 96 // container needs, such as storage and networking, as well as links 97 // between containers. The container is left waiting for a signal to 98 // begin running. 99 func (daemon *Daemon) containerStart(container *container.Container, checkpoint string, checkpointDir string, resetRestartManager bool) (err error) { 100 start := time.Now() 101 container.Lock() 102 defer container.Unlock() 103 104 if resetRestartManager && container.Running { // skip this check if already in restarting step and resetRestartManager==false 105 return nil 106 } 107 108 if container.RemovalInProgress || container.Dead { 109 return fmt.Errorf("Container is marked for removal and cannot be started.") 110 } 111 112 // if we encounter an error during start we need to ensure that any other 113 // setup has been cleaned up properly 114 defer func() { 115 if err != nil { 116 container.SetError(err) 117 // if no one else has set it, make sure we don't leave it at zero 118 if container.ExitCode() == 0 { 119 container.SetExitCode(128) 120 } 121 container.ToDisk() 122 123 container.Reset(false) 124 125 daemon.Cleanup(container) 126 // if containers AutoRemove flag is set, remove it after clean up 127 if container.HostConfig.AutoRemove { 128 container.Unlock() 129 if err := daemon.ContainerRm(container.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil { 130 logrus.Errorf("can't remove container %s: %v", container.ID, err) 131 } 132 container.Lock() 133 } 134 } 135 }() 136 137 if err := daemon.conditionalMountOnStart(container); err != nil { 138 return err 139 } 140 141 // Make sure NetworkMode has an acceptable value. We do this to ensure 142 // backwards API compatibility. 143 container.HostConfig = runconfig.SetDefaultNetModeIfBlank(container.HostConfig) 144 145 if err := daemon.initializeNetworking(container); err != nil { 146 return err 147 } 148 149 spec, err := daemon.createSpec(container) 150 if err != nil { 151 return err 152 } 153 154 createOptions, err := daemon.getLibcontainerdCreateOptions(container) 155 if err != nil { 156 return err 157 } 158 159 if resetRestartManager { 160 container.ResetRestartManager(true) 161 } 162 163 if checkpointDir == "" { 164 checkpointDir = container.CheckpointDir() 165 } 166 167 if err := daemon.containerd.Create(container.ID, checkpoint, checkpointDir, *spec, container.InitializeStdio, createOptions...); err != nil { 168 errDesc := grpc.ErrorDesc(err) 169 contains := func(s1, s2 string) bool { 170 return strings.Contains(strings.ToLower(s1), s2) 171 } 172 logrus.Errorf("Create container failed with error: %s", errDesc) 173 // if we receive an internal error from the initial start of a container then lets 174 // return it instead of entering the restart loop 175 // set to 127 for container cmd not found/does not exist) 176 if contains(errDesc, container.Path) && 177 (contains(errDesc, "executable file not found") || 178 contains(errDesc, "no such file or directory") || 179 contains(errDesc, "system cannot find the file specified")) { 180 container.SetExitCode(127) 181 } 182 // set to 126 for container cmd can't be invoked errors 183 if contains(errDesc, syscall.EACCES.Error()) { 184 container.SetExitCode(126) 185 } 186 187 // attempted to mount a file onto a directory, or a directory onto a file, maybe from user specified bind mounts 188 if contains(errDesc, syscall.ENOTDIR.Error()) { 189 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" 190 container.SetExitCode(127) 191 } 192 193 return fmt.Errorf("%s", errDesc) 194 } 195 196 containerActions.WithValues("start").UpdateSince(start) 197 198 return nil 199 } 200 201 // Cleanup releases any network resources allocated to the container along with any rules 202 // around how containers are linked together. It also unmounts the container's root filesystem. 203 func (daemon *Daemon) Cleanup(container *container.Container) { 204 daemon.releaseNetwork(container) 205 206 container.UnmountIpcMounts(detachMounted) 207 208 if err := daemon.conditionalUnmountOnCleanup(container); err != nil { 209 // FIXME: remove once reference counting for graphdrivers has been refactored 210 // Ensure that all the mounts are gone 211 if mountid, err := daemon.layerStore.GetMountID(container.ID); err == nil { 212 daemon.cleanupMountsByID(mountid) 213 } 214 } 215 216 if err := container.UnmountSecrets(); err != nil { 217 logrus.Warnf("%s cleanup: failed to unmount secrets: %s", container.ID, err) 218 } 219 220 for _, eConfig := range container.ExecCommands.Commands() { 221 daemon.unregisterExecCommand(container, eConfig) 222 } 223 224 if container.BaseFS != "" { 225 if err := container.UnmountVolumes(daemon.LogVolumeEvent); err != nil { 226 logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err) 227 } 228 } 229 container.CancelAttachContext() 230 }