...

Source file src/github.com/opencontainers/runc/delete.go

Documentation: github.com/opencontainers/runc

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"time"
     9  
    10  	"github.com/opencontainers/runc/libcontainer"
    11  	"github.com/urfave/cli"
    12  
    13  	"golang.org/x/sys/unix"
    14  )
    15  
    16  func killContainer(container libcontainer.Container) error {
    17  	_ = container.Signal(unix.SIGKILL, false)
    18  	for i := 0; i < 100; i++ {
    19  		time.Sleep(100 * time.Millisecond)
    20  		if err := container.Signal(unix.Signal(0), false); err != nil {
    21  			destroy(container)
    22  			return nil
    23  		}
    24  	}
    25  	return errors.New("container init still running")
    26  }
    27  
    28  var deleteCommand = cli.Command{
    29  	Name:  "delete",
    30  	Usage: "delete any resources held by the container often used with detached container",
    31  	ArgsUsage: `<container-id>
    32  
    33  Where "<container-id>" is the name for the instance of the container.
    34  
    35  EXAMPLE:
    36  For example, if the container id is "ubuntu01" and runc list currently shows the
    37  status of "ubuntu01" as "stopped" the following will delete resources held for
    38  "ubuntu01" removing "ubuntu01" from the runc list of containers:
    39  
    40         # runc delete ubuntu01`,
    41  	Flags: []cli.Flag{
    42  		cli.BoolFlag{
    43  			Name:  "force, f",
    44  			Usage: "Forcibly deletes the container if it is still running (uses SIGKILL)",
    45  		},
    46  	},
    47  	Action: func(context *cli.Context) error {
    48  		if err := checkArgs(context, 1, exactArgs); err != nil {
    49  			return err
    50  		}
    51  
    52  		id := context.Args().First()
    53  		force := context.Bool("force")
    54  		container, err := getContainer(context)
    55  		if err != nil {
    56  			if errors.Is(err, libcontainer.ErrNotExist) {
    57  				// if there was an aborted start or something of the sort then the container's directory could exist but
    58  				// libcontainer does not see it because the state.json file inside that directory was never created.
    59  				path := filepath.Join(context.GlobalString("root"), id)
    60  				if e := os.RemoveAll(path); e != nil {
    61  					fmt.Fprintf(os.Stderr, "remove %s: %v\n", path, e)
    62  				}
    63  				if force {
    64  					return nil
    65  				}
    66  			}
    67  			return err
    68  		}
    69  		s, err := container.Status()
    70  		if err != nil {
    71  			return err
    72  		}
    73  		switch s {
    74  		case libcontainer.Stopped:
    75  			destroy(container)
    76  		case libcontainer.Created:
    77  			return killContainer(container)
    78  		default:
    79  			if force {
    80  				return killContainer(container)
    81  			}
    82  			return fmt.Errorf("cannot delete container %s that is not stopped: %s", id, s)
    83  		}
    84  
    85  		return nil
    86  	},
    87  }
    88  

View as plain text