2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2014-07-31 17:12:12 -04:00
|
|
|
|
|
|
|
import (
|
2015-09-03 20:51:04 -04:00
|
|
|
"fmt"
|
2014-07-31 17:12:12 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
2015-09-03 20:51:04 -04:00
|
|
|
"strings"
|
2016-07-20 19:11:28 -04:00
|
|
|
"time"
|
2014-07-31 17:12:12 -04:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2017-02-14 13:35:20 -05:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2020-12-14 05:46:58 -05:00
|
|
|
"github.com/opencontainers/selinux/go-selinux"
|
2017-03-01 08:36:09 -05:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2014-07-31 17:12:12 -04:00
|
|
|
)
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// ContainerRm removes the container id from the filesystem. An error
|
|
|
|
// is returned if the container is not found, or if the remove
|
|
|
|
// fails. If the remove succeeds, the container name is released, and
|
|
|
|
// network links are removed.
|
2015-12-04 15:34:43 -05:00
|
|
|
func (daemon *Daemon) ContainerRm(name string, config *types.ContainerRmConfig) error {
|
2016-07-20 19:11:28 -04:00
|
|
|
start := time.Now()
|
2015-12-11 12:39:28 -05:00
|
|
|
container, err := daemon.GetContainer(name)
|
2014-12-16 18:06:35 -05:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-28 10:18:08 -04:00
|
|
|
}
|
|
|
|
|
2015-12-01 12:54:26 -05:00
|
|
|
// Container state RemovalInProgress should be used to avoid races.
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
if inProgress := container.SetRemovalInProgress(); inProgress {
|
2016-08-27 09:39:34 -04:00
|
|
|
err := fmt.Errorf("removal of container %s is already in progress", name)
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.Conflict(err)
|
2015-12-01 12:54:26 -05:00
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
defer container.ResetRemovalInProgress()
|
2015-12-01 12:54:26 -05:00
|
|
|
|
|
|
|
// check if container wasn't deregistered by previous rm since Get
|
|
|
|
if c := daemon.containers.Get(container.ID); c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 17:38:53 -04:00
|
|
|
if config.RemoveLink {
|
2015-09-03 20:51:04 -04:00
|
|
|
return daemon.rmLink(container, name)
|
2014-07-31 17:12:12 -04:00
|
|
|
}
|
|
|
|
|
2016-07-20 19:11:28 -04:00
|
|
|
err = daemon.cleanupContainer(container, config.ForceRemove, config.RemoveVolume)
|
|
|
|
containerActions.WithValues("delete").UpdateSince(start)
|
|
|
|
|
|
|
|
return err
|
2014-07-31 17:12:12 -04:00
|
|
|
}
|
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
func (daemon *Daemon) rmLink(container *container.Container, name string) error {
|
|
|
|
if name[0] != '/' {
|
|
|
|
name = "/" + name
|
2015-12-02 12:18:34 -05:00
|
|
|
}
|
|
|
|
parent, n := path.Split(name)
|
|
|
|
if parent == "/" {
|
2019-04-25 12:57:56 -04:00
|
|
|
return fmt.Errorf("Conflict, cannot remove the default link name of the container")
|
2015-12-02 12:18:34 -05:00
|
|
|
}
|
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
parent = strings.TrimSuffix(parent, "/")
|
2017-06-29 21:56:22 -04:00
|
|
|
pe, err := daemon.containersReplica.Snapshot().GetID(parent)
|
2015-09-03 20:51:04 -04:00
|
|
|
if err != nil {
|
2019-04-25 12:57:56 -04:00
|
|
|
return fmt.Errorf("Cannot get parent %s for link name %s", parent, name)
|
2015-12-02 12:18:34 -05:00
|
|
|
}
|
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
daemon.releaseName(name)
|
|
|
|
parentContainer, _ := daemon.GetContainer(pe)
|
2015-12-02 12:18:34 -05:00
|
|
|
if parentContainer != nil {
|
2015-09-03 20:51:04 -04:00
|
|
|
daemon.linkIndex.unlink(name, container, parentContainer)
|
2015-12-02 12:18:34 -05:00
|
|
|
if err := daemon.updateNetwork(parentContainer); err != nil {
|
|
|
|
logrus.Debugf("Could not update network to remove link %s: %v", n, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanupContainer unregisters a container from the daemon, stops stats
|
|
|
|
// collection and cleanly removes contents and metadata from the filesystem.
|
2016-08-16 23:02:16 -04:00
|
|
|
func (daemon *Daemon) cleanupContainer(container *container.Container, forceRemove, removeVolume bool) (err error) {
|
2015-05-26 15:38:52 -04:00
|
|
|
if container.IsRunning() {
|
|
|
|
if !forceRemove {
|
2017-02-09 05:48:33 -05:00
|
|
|
state := container.StateString()
|
|
|
|
procedure := "Stop the container before attempting removal or force remove"
|
|
|
|
if state == "paused" {
|
|
|
|
procedure = "Unpause and then " + strings.ToLower(procedure)
|
|
|
|
}
|
|
|
|
err := fmt.Errorf("You cannot remove a %s container %s. %s", state, container.ID, procedure)
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.Conflict(err)
|
2015-05-26 15:38:52 -04:00
|
|
|
}
|
2015-11-02 18:25:26 -05:00
|
|
|
if err := daemon.Kill(container); err != nil {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return fmt.Errorf("Could not kill running container %s, cannot remove - %v", container.ID, err)
|
2015-05-26 15:38:52 -04:00
|
|
|
}
|
2014-07-31 17:12:12 -04:00
|
|
|
}
|
2017-11-20 11:33:20 -05:00
|
|
|
if !system.IsOSSupported(container.OS) {
|
|
|
|
return fmt.Errorf("cannot remove %s: %s ", container.ID, system.ErrNotSupportedOperatingSystem)
|
|
|
|
}
|
2014-07-31 17:12:12 -04:00
|
|
|
|
2015-10-20 16:36:09 -04:00
|
|
|
// stop collection of stats for the container regardless
|
|
|
|
// if stats are currently getting collected.
|
2017-01-04 12:01:59 -05:00
|
|
|
daemon.statsCollector.StopCollection(container)
|
2015-10-20 16:36:09 -04:00
|
|
|
|
2015-11-02 18:25:26 -05:00
|
|
|
if err = daemon.containerStop(container, 3); err != nil {
|
2014-07-31 17:12:12 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-12 15:26:17 -04:00
|
|
|
// Mark container dead. We don't want anybody to be restarting it.
|
2017-02-22 17:02:20 -05:00
|
|
|
container.Lock()
|
|
|
|
container.Dead = true
|
2015-03-12 15:26:17 -04:00
|
|
|
|
|
|
|
// Save container state to disk. So that if error happens before
|
|
|
|
// container meta file got removed from disk, then a restart of
|
|
|
|
// docker should not make a dead container alive.
|
2017-03-27 13:18:53 -04:00
|
|
|
if err := container.CheckpointTo(daemon.containersReplica); err != nil && !os.IsNotExist(err) {
|
2015-07-02 06:24:35 -04:00
|
|
|
logrus.Errorf("Error saving dying container to disk: %v", err)
|
|
|
|
}
|
2017-02-22 17:02:20 -05:00
|
|
|
container.Unlock()
|
2015-03-12 15:26:17 -04:00
|
|
|
|
2016-04-04 21:30:05 -04:00
|
|
|
// When container creation fails and `RWLayer` has not been created yet, we
|
|
|
|
// do not call `ReleaseRWLayer`
|
|
|
|
if container.RWLayer != nil {
|
2018-02-07 15:52:47 -05:00
|
|
|
err := daemon.imageService.ReleaseLayer(container.RWLayer, container.OS)
|
2018-02-02 17:18:46 -05:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrapf(err, "container %s", container.ID)
|
|
|
|
container.SetRemovalError(err)
|
|
|
|
return err
|
2016-04-04 21:30:05 -04:00
|
|
|
}
|
2018-01-30 18:01:45 -05:00
|
|
|
container.RWLayer = nil
|
2014-07-31 17:12:12 -04:00
|
|
|
}
|
|
|
|
|
2017-02-14 13:35:20 -05:00
|
|
|
if err := system.EnsureRemoveAll(container.Root); err != nil {
|
ContainerWait on remove: don't stuck on rm fail
Currently, if a container removal has failed for some reason,
any client waiting for removal (e.g. `docker run --rm`) is
stuck, waiting for removal to succeed while it has failed already.
For more details and the reproducer, please check
https://github.com/moby/moby/issues/34945
This commit addresses that by allowing `ContainerWait()` with
`container.WaitCondition == "removed"` argument to return an
error in case of removal failure. The `ContainerWaitOKBody`
stucture returned to a client is amended with a pointer to `struct Error`,
containing an error message string, and the `Client.ContainerWait()`
is modified to return the error, if any, to the client.
Note that this feature is only available for API version >= 1.34.
In order for the old clients to be unstuck, we just close the connection
without writing anything -- this causes client's error.
Now, docker-cli would need a separate commit to bump the API to 1.34
and to show an error returned, if any.
[v2: recreate the waitRemove channel after closing]
[v3: document; keep legacy behavior for older clients]
[v4: convert Error from string to pointer to a struct]
[v5: don't emulate old behavior, send empty response in error case]
[v6: rename legacy* vars to include version suffix]
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2017-09-27 14:49:22 -04:00
|
|
|
e := errors.Wrapf(err, "unable to remove filesystem for %s", container.ID)
|
|
|
|
container.SetRemovalError(e)
|
|
|
|
return e
|
2017-02-14 13:35:20 -05:00
|
|
|
}
|
|
|
|
|
2017-07-14 05:25:09 -04:00
|
|
|
linkNames := daemon.linkIndex.delete(container)
|
2020-12-14 05:46:58 -05:00
|
|
|
selinux.ReleaseLabel(container.ProcessLabel)
|
2017-02-14 13:35:20 -05:00
|
|
|
daemon.idIndex.Delete(container.ID)
|
|
|
|
daemon.containers.Delete(container.ID)
|
2017-03-27 13:18:53 -04:00
|
|
|
daemon.containersReplica.Delete(container)
|
2017-02-14 13:35:20 -05:00
|
|
|
if e := daemon.removeMountPoints(container, removeVolume); e != nil {
|
|
|
|
logrus.Error(e)
|
|
|
|
}
|
2017-07-14 05:25:09 -04:00
|
|
|
for _, name := range linkNames {
|
|
|
|
daemon.releaseName(name)
|
|
|
|
}
|
2017-03-30 16:52:40 -04:00
|
|
|
container.SetRemoved()
|
2017-02-14 13:35:20 -05:00
|
|
|
stateCtr.del(container.ID)
|
2017-09-22 09:52:41 -04:00
|
|
|
|
2017-02-14 13:35:20 -05:00
|
|
|
daemon.LogContainerEvent(container, "destroy")
|
2014-07-31 17:12:12 -04:00
|
|
|
return nil
|
|
|
|
}
|