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)
    }

相关内容

热门资讯

青海海南州再发生5.8级地震 中国地震台网正式测定:07月28日11时34分在青海海南州兴海县(北纬35.35度,东经99.58度...
风声丨支持率一夜狂跌,高市早苗... 作者丨郁风媒体评论员日本首相高市早苗,近日民调支持率突然迎来崩盘式下跌,从原本居高不下的六成左右下跌...
50年后再访唐山:经历过地震那... 澎湃新闻记者 高丹1976年7月28日,凌晨3时42分56秒,大地颠簸。一座百万人口的工业城市成为一...
上半年全国挽回税款1806亿元 国家税务总局副局长王道树7月28日在国新办新闻发布会上表示,今年上半年,全国依法查处各类涉税违法行为...
青海兴海县发生5.7级地震,震... 中国地震台网正式测定:07月28日11时16分在青海海南州兴海县(北纬35.35度,东经99.57度...
中国海警紧急救援越南籍遇险船舶 7月25日19时37分,中国海警接海南省海上搜救中心通报,1艘越南籍船舶在永暑礁以北38海里附近海域...
黄仁勋、马斯克同日发声,事关中... 据澎湃新闻7月26日报道,7月25日,马斯克在《经济学人》访谈中表示,未来某个时刻中国极有可能成为A...
森马,被成人装拖累? “人这一辈子真的不知道谁会旺自己。”近日,好莱坞明星、蜘蛛侠扮演者汤姆·赫兰德(中国粉丝常称“荷兰弟...
伊菲丹 × 成都尼依格罗酒店|... 今夏,法国奢护品牌伊菲丹EviDenS de Beauté携手成都尼依格罗酒店,于七夕...
特朗普下令不打了,美国的“余粮... 可以说戛然而止,快得让很多人都无法适应。前两周,每一天美国都要打击伊朗一通。就在7月24日白宫会议前...