這篇文章主要介紹docker中info命令請(qǐng)求流程分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
夏津網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
僅供自己梳理了解最新代碼流程,有些細(xì)節(jié)并不會(huì)展開深挖
1、進(jìn)入客戶端接收代碼塊,由runInfo方法返回內(nèi)容
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、 請(qǐng)求轉(zhuǎn)發(fā)給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的監(jiān)聽路由,進(jìn)入到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、經(jīng)過進(jìn)入systemInfo處理方法,可知container狀態(tài)信息是已經(jīng)在內(nèi)存中數(shù)據(jù)
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、找到設(shè)置container運(yùn)行狀態(tài)的數(shù)據(jù)方法
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、容器狀態(tài)數(shù)據(jù)來源
6.1、順著設(shè)置方法找到在創(chuàng)建container時(shí)會(huì)設(shè)置一條數(shù)據(jù),這是初始化的數(shù)據(jù)
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、容器操作(暫停、啟動(dòng)、恢復(fù))
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重啟后,從目錄中獲取容器狀態(tài)
github.com/docker/docker/daemon/daemon.go
func (daemon *Daemon) restore() error { for _, c := range containers { //從文件config.v2.json中獲取容器狀態(tài) daemon.setStateCounter(c) //如文件中的狀態(tài)是運(yùn)行或暫停,再進(jìn)行檢查,并重置狀態(tài) if c.IsRunning() || c.IsPaused() { default: // running c.Lock() c.Paused = false daemon.setStateCounter(c)
7、事件變更重置容器狀態(tài)(windows)
7.1、container狀態(tài)變更的信息方法
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事件監(jiān)聽
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、開啟啟動(dòng)docker時(shí)的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命令請(qǐng)求流程分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!