command

package
v0.0.0-...-d7ba063 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2025 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommitCommand = &cli.Command{
	Name:  "commit",
	Usage: "commit a container into images",
	Action: func(context *cli.Context) error {
		if context.NArg() < 2 {
			return fmt.Errorf("missing container name and image name")
		}

		containerArg := context.Args().Get(0)
		imageName := context.Args().Get(1)

		err := cmdExec.CommitContainer(containerArg, imageName)
		if err != nil {
			log.Errorf("commit container err: %v", err)
			return err
		}
		return nil
	},
}
View Source
var ExecCommand = &cli.Command{
	Name:  "exec",
	Usage: "Run a command in a running container",
	Action: func(context *cli.Context) error {

		if os.Getenv("dockergsh_pid") != "" {

			log.Infof("pid callback pid %s", os.Getgid())
			return nil
		}
		if context.NArg() < 2 {
			return fmt.Errorf("missing container name or command")
		}
		containerArg := context.Args().Get(0)
		var commandArr []string
		for _, arg := range context.Args().Tail() {
			commandArr = append(commandArr, arg)
		}

		err := cmdExec.ExecInContainer(containerArg, commandArr)
		if err != nil {
			log.Errorf("Exec Container failed %v", err)
			return err
		}
		return nil
	},
}
View Source
var InitCommand = &cli.Command{
	Name:  "init",
	Usage: "Init container process run user's process in container. Do not call it outside",

	Action: func(context *cli.Context) error {
		log.Infof("init come on")
		cmd := context.Args().Get(0)
		log.Infof("Command %s", cmd)
		err := container.RunContainerInitProcess()
		return err
	},
}

定义了 InitCommand 的具体操作,此操作为内部方法,禁止外部调用 其实就是 container 内有一个 init 进程 init 命令的作用:fork出子进程启动容器时,执行 /proc/self/exe 来执行 dockergsh init 命令来启动容器

View Source
var ListCommand = &cli.Command{
	Name:  "ps",
	Usage: "list all the containers",
	Action: func(context *cli.Context) error {
		cmdExec.ListContainers()
		return nil
	},
}
View Source
var LogsCommand = &cli.Command{
	Name:  "logs",
	Usage: "Fetch the logs of a container",
	Action: func(context *cli.Context) error {
		if context.NArg() < 1 {
			return fmt.Errorf("Please input your container name")
		}
		containerName := context.Args().Get(0)
		err := cmdExec.LogContainer(containerName)
		if err != nil {
			logrus.Errorf("Log container error %v", err)
			return err
		}
		return nil
	},
}
View Source
var NetworkCommand = &cli.Command{
	Name:  "network",
	Usage: "Manage networks",
	Subcommands: []*cli.Command{
		{
			Name:  "create",
			Usage: "Create a network",
			Flags: []cli.Flag{
				&cli.StringFlag{
					Name:  "driver",
					Usage: "Driver to manage the Network (default \"bridge\")",
				},
				&cli.StringFlag{
					Name:  "subnet",
					Usage: "Subnet in CIDR format that represents a network segment",
				},
			},
			Action: func(context *cli.Context) error {
				if context.NArg() < 1 {
					return fmt.Errorf("missing network name")
				}
				if err := network.Init(); err != nil {
					return err
				}

				err := network.CreateNetwork(context.String("driver"), context.String("subnet"), context.Args().Get(0))
				if err != nil {
					log.Errorf("commit container err: %v", err)
					return err
				}
				return nil
			},
		},
		{
			Name:  "list",
			Usage: "List networks",
			Action: func(context *cli.Context) error {
				if err := network.Init(); err != nil {
					return err
				}
				if err := network.ListNetwork(); err != nil {
					return err
				}
				return nil
			},
		},
		{
			Name:  "remove",
			Usage: "Remove one or more networks",
			Action: func(context *cli.Context) error {
				if context.NArg() < 1 {
					return fmt.Errorf("Missing image name")
				}

				err := network.Init()
				if err != nil {
					return err
				}

				if err = network.DeleteNetwork(context.Args().Get(0)); err != nil {
					return fmt.Errorf("remove network error: %+v", err)
				}

				return nil
			},
		},
	},
}
View Source
var RemoveCommand = &cli.Command{
	Name:  "rm",
	Usage: "Remove one or more containers",
	Action: func(context *cli.Context) error {
		if context.NArg() < 1 {
			return fmt.Errorf("Missing container name")
		}
		containerArg := context.Args().Get(0)
		err := cmdExec.RemoveContainer(containerArg)
		if err != nil {
			log.Errorf("Remove Container failed %v", err)
			return err
		}
		return nil
	},
}
View Source
var RunCommand = &cli.Command{
	Name: "run",
	Usage: `Create a container with namespace and cgroup limit
			mydocker run -it [command]`,
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:  "it",
			Usage: "enable tty",
		},
		&cli.BoolFlag{
			Name:  "d",
			Usage: "detach container",
		},
		&cli.StringFlag{
			Name:  "m",
			Usage: "memory limit",
		},
		&cli.StringFlag{
			Name:  "cpu",
			Usage: "cpu limit",
		},
		&cli.StringFlag{
			Name:  "cpuset",
			Usage: "cpuset limit",
		},
		&cli.StringFlag{
			Name:  "v",
			Usage: "Volume",
		},
		&cli.StringFlag{
			Name:  "name",
			Usage: "container name",
		},
		&cli.StringSliceFlag{
			Name:  "e",
			Usage: "set environments",
		},
		&cli.StringFlag{
			Name:  "net",
			Usage: "container network",
		},
	},

	Action: func(context *cli.Context) error {
		if context.NArg() < 1 {
			return fmt.Errorf("Missing container command")
		}

		// 要执行的 命令
		var cmdArray []string
		for i := 0; i < context.NArg(); i++ {
			cmdArray = append(cmdArray, context.Args().Get(i))
		}

		imageName := cmdArray[0]
		cmdArray = cmdArray[1:]

		volume := context.String("v")

		network := context.String("net")

		containerName := context.String("name")

		envSlice := context.StringSlice("e")

		tty := context.Bool("it")
		detach := context.Bool("d")

		if tty && detach {
			return fmt.Errorf("-it and -d paramter can not both provided")
		}

		resConf := &subsystem.ResourceConfig{
			MemoryLimit: context.String("m"),
			CpuShare:    context.String("cpu"),
			CpuSet:      context.String("cpuset"),
		}

		cmdExec.Run(tty, cmdArray, resConf, imageName, containerName, volume, envSlice, network)

		return nil
	},
}

定义了 docker run 命令的 RunCommand 的所有 Flags,也就是用 -- 来指定的选项

View Source
var StopCommand = &cli.Command{
	Name:  "stop",
	Usage: "Stop one or more running containers",
	Action: func(context *cli.Context) error {

		if context.NArg() < 1 {
			return fmt.Errorf("missing container name")
		}
		containerArg := context.Args().Get(0)
		err := cmdExec.StopContainer(containerArg)
		if err != nil {
			log.Errorf("Stop Container failed %v", err)
			return err
		}
		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL