扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章主要介绍docker中info命令请求流程分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联主要从事网站设计制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务渠县,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220仅供自己梳理了解最新代码流程,有些细节并不会展开深挖
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) }
以上是“docker中info命令请求流程分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流