docker中info命令请求流程分析
admin
2023-04-06 06:21:26
0

先上一个流程图示

docker中info命令请求流程分析

仅供自己梳理了解最新代码流程,有些细节并不会展开深挖
1、进入客户端接收代码块,由runInfo方法返回内容
github.com/docker/cli/cli/command/system/info.go

// NewInfoCommand creates a new cobra.Command for `docker info`
func NewInfoCommand(dockerCli command.Cli) *cobra.Command {
    var opts infoOptions

    cmd := &cobra.Command{
        Use:   "info [OPTIONS]",
        Short: "Display system-wide information",
        Args:  cli.NoArgs,
        RunE: func(cmd *cobra.Command, args []string) error {
            return runInfo(dockerCli, &opts)
        },
    }

func runInfo(dockerCli command.Cli, opts *infoOptions) error {
    ctx := context.Background()
    info, err := dockerCli.Client().Info(ctx)

2、    请求转发给docker daemon处理
github.com/docker/cli/vendor/github.com/docker/docker/client/info.go

// Info returns information about the docker server.
func (cli *Client) Info(ctx context.Context) (types.Info, error) {
    var info types.Info
    serverResp, err := cli.get(ctx, "/info", url.Values{}, nil)

3、docker daemon的监听路由,进入到SystemInfo处理  
github.com/docker/docker/api/server/router/system/system.go

// NewRouter initializes a new system router
func NewRouter(b Backend, c ClusterBackend, fscache *fscache.FSCache, builder *buildkit.Builder, features *map[string]bool) router.Router {
   router.NewGetRoute("/info", r.getInfo),

github.com/docker/docker/api/server/router/system/system_routes.go

func (s *systemRouter) getInfo(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    info, err := s.backend.SystemInfo()

4、经过进入systemInfo处理方法,可知container状态信息是已经在内存中数据  
github.com/docker/docker/daemon/info.go

// SystemInfo returns information about the host server the daemon is running on.
func (daemon *Daemon) SystemInfo() (*types.Info, error) {
    sysInfo := sysinfo.New(true)
    cRunning, cPaused, cStopped := stateCtr.get()

    v := &types.Info{
        ID:                 daemon.ID,
        Containers:         cRunning + cPaused + cStopped,
        ContainersRunning:  cRunning,
        ContainersPaused:   cPaused,
        ContainersStopped:  cStopped,

5、找到设置container运行状态的数据方法      
github.com/docker/docker/daemon/metrics.go

func (ctr *stateCounter) set(id, label string) {
    ctr.mu.Lock()
    ctr.states[id] = label
    ctr.mu.Unlock()
}

6、容器状态数据来源
6.1、顺着设置方法找到在创建container时会设置一条数据,这是初始化的数据
github.com/docker/docker/daemon/create.go

// Create creates a new container from the given configuration with a given name.
func (daemon *Daemon) create(params types.ContainerCreateConfig, managed bool) (retC *container.Container, retErr error) {
   stateCtr.set(container.ID, "stopped")

6.2、容器操作(暂停、启动、恢复)
github.com/docker/docker/daemon/pause.go

// containerPause pauses the container execution without stopping the process.
// The execution can be resumed by calling containerUnpause.
func (daemon *Daemon) containerPause(container *container.Container) error {
    container.Paused = true
    daemon.setStateCounter(container)

github.com/docker/docker/daemon/start.go

// containerStart prepares the container to run by setting up everything the
// container needs, such as storage and networking, as well as links
// between containers. The container is left waiting for a signal to
// begin running.
func (daemon *Daemon) containerStart(container *container.Container, checkpoint string, checkpointDir string, resetRestartManager bool) (err error) {
    container.SetRunning(pid, true)
    container.HasBeenManuallyStopped = false
    container.HasBeenStartedBefore = true
    daemon.setStateCounter(container)

github.com/docker/docker/daemon/unpause.go

// containerUnpause resumes the container execution after the container is paused.
func (daemon *Daemon) containerUnpause(container *container.Container) error {
    container.Paused = false
    daemon.setStateCounter(container)

6.3、docker重启后,从目录中获取容器状态
github.com/docker/docker/daemon/daemon.go

func (daemon *Daemon) restore() error {
   for _, c := range containers {
     //从文件config.v2.json中获取容器状态
     daemon.setStateCounter(c)

     //如文件中的状态是运行或暂停,再进行检查,并重置状态
     if c.IsRunning() || c.IsPaused() {
     default:
                            // running
                            c.Lock()
                            c.Paused = false
                            daemon.setStateCounter(c)

7、事件变更重置容器状态(windows)
7.1、container状态变更的信息方法  
github.com/docker/docker/daemon/monitor.go

func (daemon *Daemon) setStateCounter(c *container.Container) {
    switch c.StateString() {
    case "paused":
        stateCtr.set(c.ID, "paused")
    case "running":
        stateCtr.set(c.ID, "running")
    default:
        stateCtr.set(c.ID, "stopped")
    }
}

7.2、windows事件监听
github.com/docker/docker/daemon/monitor.go

// ProcessEvent is called by libcontainerd whenever an event occurs
func (daemon *Daemon) ProcessEvent(id string, e libcontainerd.EventType, ei libcontainerd.EventInfo) error {
    case libcontainerd.EventExit:
       daemon.setStateCounter(c)
    case libcontainerd.EventStart:

        // This is here to handle start not generated by docker
        if !c.Running {
            c.SetRunning(int(ei.Pid), false)
            c.HasBeenManuallyStopped = false
            c.HasBeenStartedBefore = true
            daemon.setStateCounter(c)

    case libcontainerd.EventPaused:

        if !c.Paused {
            c.Paused = true
            daemon.setStateCounter(c)

    case libcontainerd.EventResumed:
        if c.Paused {
            c.Paused = false
            daemon.setStateCounter(c)

8、开启启动docker时的debug模式,获取文件描述、goroute等信息
dockerd --debug
github.com/docker/cli/cli/command/system/info.go

if info.Debug {
        fmt.Fprintln(dockerCli.Out(), " File Descriptors:", info.NFd)
        fmt.Fprintln(dockerCli.Out(), " Goroutines:", info.NGoroutines)
        fmt.Fprintln(dockerCli.Out(), " System Time:", info.SystemTime)
        fmt.Fprintln(dockerCli.Out(), " EventsListeners:", info.NEventsListener)
    }

相关内容

热门资讯

空调26度不凉是什么原因 从空调内部元器件来说,不凉的原因有可能是温度传感器出现故障了,也有可能是空调压缩机或者铜管出现问题,...
鲸鸿动能携手香港国际机场、鸿蒙...   2026 年 7 月 25 日,十余位鸿蒙车主组成的车队从珠海出发,经港珠澳大桥通关抵达香港国际...
外墙涂料多少钱一平方 刷外墙涂... 外墙涂料多少钱一平方外墙涂料的价格可以分为两个部分,一个是材料价格,另外一类是施工价格。1、材料价格...
挡土墙施工的工艺流程有哪些 摘要:首先要进行测量和放样,再进行垃圾的处理,再进行模板的安装,最后只要通过检查就行了。测量放样:利...
电视用遥控器打不开怎么办 步骤一,用户可检查遥控器的电池电源电量是否充足,如若不充足更换电池后方可正常使用。步骤二,检测遥控器...
可以回答下新买的橡木浴室柜开裂... 如果橡木浴室柜刚买就发生开裂的问题,那就说明这个浴室柜的质量肯定是不合格的。遇到这种情况,一定要及时...
青海海南州再发生5.8级地震 中国地震台网正式测定:07月28日11时34分在青海海南州兴海县(北纬35.35度,东经99.58度...
风声丨支持率一夜狂跌,高市早苗... 作者丨郁风媒体评论员日本首相高市早苗,近日民调支持率突然迎来崩盘式下跌,从原本居高不下的六成左右下跌...
50年后再访唐山:经历过地震那... 澎湃新闻记者 高丹1976年7月28日,凌晨3时42分56秒,大地颠簸。一座百万人口的工业城市成为一...
上半年全国挽回税款1806亿元 国家税务总局副局长王道树7月28日在国新办新闻发布会上表示,今年上半年,全国依法查处各类涉税违法行为...