【Docker学习】重启容器的docker restart

命令:

docker container restart

描述:

重启一个或多个容器

用法:

docker container restart [OPTIONS] CONTAINER [CONTAINER...]

别名:

docker restart(docker的一些命令可以简写,docker restart就等同于docker container restart)

选项:

选项 描述
-s, --signal 给容器发送信号
-t, --time 在容器启动后,等待多少秒之后,如果容器没有按预期运行,则自动终止(kill)容器

实战:

如上图所示,我通过docker ps -a显示所有停止的容器,通过docker restart nginx重启nginx容器,最后docker ps显示nginx的状态为Up。

--signal,--time:

经过多次测试,没有迹象表明这个选项起了作用。网上查询,没有发现有对restart的这个-s选项的任何实战说明。为了一探究竟,只能查看docker源码了。

docker分为客户端和服务器两部分,其中客户端有关restart部分关键源码如下所示:

func runRestart(ctx context.Context, dockerCli command.Cli, opts *restartOptions) error {
	var errs []string
	var timeout *int
	if opts.timeoutChanged {
		timeout = &opts.timeout
	}
	for _, name := range opts.containers {
		err := dockerCli.Client().ContainerRestart(ctx, name, container.StopOptions{
			Signal:  opts.signal,
			Timeout: timeout,
		})
		if err != nil {
			errs = append(errs, err.Error())
			continue
		}
		_, _ = fmt.Fprintln(dockerCli.Out(), name)
	}
	if len(errs) > 0 {
		return errors.New(strings.Join(errs, "\n"))
	}
	return nil
}

可以看到Signal和Timeout都是处于StopOptions结构体内。

// StopOptions holds the options to stop or restart a container.
type StopOptions struct {
	// Signal (optional) is the signal to send to the container to (gracefully)
	// stop it before forcibly terminating the container with SIGKILL after the
	// timeout expires. If not value is set, the default (SIGTERM) is used.
	Signal string `json:",omitempty"`

	// Timeout (optional) is the timeout (in seconds) to wait for the container
	// to stop gracefully before forcibly terminating it with SIGKILL.
	//
	// - Use nil to use the default timeout (10 seconds).
	// - Use '-1' to wait indefinitely.
	// - Use '0' to not wait for the container to exit gracefully, and
	//   immediately proceeds to forcibly terminating the container.
	// - Other positive values are used as timeout (in seconds).
	Timeout *int `json:",omitempty"`
}

从结构体成员Signal上面的的注释可以看到,只有重启失败,经过timeout时间后,signal才会起作用,优雅地停止容器,然后再用 SIGKILL 强制终止容器。如果没有设置值,则使用默认值(SIGTERM)。

若是自己的容器项目经常因为一些原因而停止,这时可以使用这两个选项控制停止时长,以更快地进行后面的调试(这是我能想到的使用这个选项的一种情境)。

关于--signal和--time的使用,在我上一篇【Docker学习】docker stop深入研究有更为深入的使用,这里就不再演示了。

相关推荐

  1. docker容器故障

    2024-05-15 15:28:02       14 阅读
  2. docker 如何在容器 php

    2024-05-15 15:28:02       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-15 15:28:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-15 15:28:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-15 15:28:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-15 15:28:02       20 阅读

热门阅读

  1. 桥接模式

    2024-05-15 15:28:02       10 阅读
  2. 5.14号模拟前端面试10问

    2024-05-15 15:28:02       13 阅读
  3. Python实战

    2024-05-15 15:28:02       15 阅读
  4. Hive内部表(管理表)和外部表的区别

    2024-05-15 15:28:02       9 阅读
  5. Intel RealSense D455深度相机驱动安装与运行

    2024-05-15 15:28:02       13 阅读
  6. 配置多个SSH密钥以访问不同平台(GitHub、Gitee)

    2024-05-15 15:28:02       14 阅读
  7. 用wordpress建外贸独立站的是主流的外贸建站方式

    2024-05-15 15:28:02       14 阅读
  8. HIVE调优

    2024-05-15 15:28:02       11 阅读
  9. AFM 433

    2024-05-15 15:28:02       14 阅读
  10. C语言-STM32:介绍PWM,并使用PWM实现呼吸灯

    2024-05-15 15:28:02       11 阅读
  11. 你眼中的IT行业现状与未来趋势

    2024-05-15 15:28:02       11 阅读
  12. Element-ui el-table组件单选/多选/跨页勾选讲解

    2024-05-15 15:28:02       18 阅读
  13. js判断是否PC端

    2024-05-15 15:28:02       13 阅读