github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/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/api/errors" 14 "github.com/docker/docker/api/types" 15 containertypes "github.com/docker/docker/api/types/container" 16 "github.com/docker/docker/container" 17 "github.com/docker/docker/runconfig" 18 ) 19 20 // ContainerStart starts a container. 21 func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.HostConfig, validateHostname bool, checkpoint string) 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 has been 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 has been 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, checkpoint, true) 81 } 82 83 // Start starts a container 84 func (daemon *Daemon) Start(container *container.Container) error { 85 return daemon.containerStart(container, "", true) 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, checkpoint string, resetRestartManager bool) (err error) { 93 container.Lock() 94 defer container.Unlock() 95 96 if resetRestartManager && container.Running { // skip this check if already in restarting step and resetRestartManager==false 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 // if containers AutoRemove flag is set, remove it after clean up 116 if container.HostConfig.AutoRemove { 117 container.Unlock() 118 if err := daemon.ContainerRm(container.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil { 119 logrus.Errorf("can't remove container %s: %v", container.ID, err) 120 } 121 container.Lock() 122 } 123 } 124 }() 125 126 if err := daemon.conditionalMountOnStart(container); err != nil { 127 return err 128 } 129 130 // Make sure NetworkMode has an acceptable value. We do this to ensure 131 // backwards API compatibility. 132 container.HostConfig = runconfig.SetDefaultNetModeIfBlank(container.HostConfig) 133 134 if err := daemon.initializeNetworking(container); err != nil { 135 return err 136 } 137 138 spec, err := daemon.createSpec(container) 139 if err != nil { 140 return err 141 } 142 143 createOptions, err := daemon.getLibcontainerdCreateOptions(container) 144 if err != nil { 145 return err 146 } 147 148 if resetRestartManager { 149 container.ResetRestartManager(true) 150 } 151 152 if err := daemon.containerd.Create(container.ID, checkpoint, container.CheckpointDir(), *spec, container.InitializeStdio, createOptions...); err != nil { 153 errDesc := grpc.ErrorDesc(err) 154 logrus.Errorf("Create container failed with error: %s", errDesc) 155 // if we receive an internal error from the initial start of a container then lets 156 // return it instead of entering the restart loop 157 // set to 127 for container cmd not found/does not exist) 158 if strings.Contains(errDesc, container.Path) && 159 (strings.Contains(errDesc, "executable file not found") || 160 strings.Contains(errDesc, "no such file or directory") || 161 strings.Contains(errDesc, "system cannot find the file specified")) { 162 container.SetExitCode(127) 163 } 164 // set to 126 for container cmd can't be invoked errors 165 if strings.Contains(errDesc, syscall.EACCES.Error()) { 166 container.SetExitCode(126) 167 } 168 169 // attempted to mount a file onto a directory, or a directory onto a file, maybe from user specified bind mounts 170 if strings.Contains(errDesc, syscall.ENOTDIR.Error()) { 171 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" 172 container.SetExitCode(127) 173 } 174 175 container.Reset(false) 176 177 return fmt.Errorf("%s", errDesc) 178 } 179 180 return nil 181 } 182 183 // Cleanup releases any network resources allocated to the container along with any rules 184 // around how containers are linked together. It also unmounts the container's root filesystem. 185 func (daemon *Daemon) Cleanup(container *container.Container) { 186 daemon.releaseNetwork(container) 187 188 container.UnmountIpcMounts(detachMounted) 189 190 if err := daemon.conditionalUnmountOnCleanup(container); err != nil { 191 // FIXME: remove once reference counting for graphdrivers has been refactored 192 // Ensure that all the mounts are gone 193 if mountid, err := daemon.layerStore.GetMountID(container.ID); err == nil { 194 daemon.cleanupMountsByID(mountid) 195 } 196 } 197 198 for _, eConfig := range container.ExecCommands.Commands() { 199 daemon.unregisterExecCommand(container, eConfig) 200 } 201 202 if container.BaseFS != "" { 203 if err := container.UnmountVolumes(false, daemon.LogVolumeEvent); err != nil { 204 logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err) 205 } 206 } 207 container.CancelAttachContext() 208 }