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