Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of info Command request flow in docker

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the analysis of info command request flow in docker, which is very detailed and has certain reference value. Friends who are interested must finish it!

First, the previous process diagram

Only for yourself to sort out and understand the latest code flow, some details will not be dug in depth.

1. Enter the client to receive the code block, and the content is returned by the runInfo method

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. The request is forwarded to docker daemon for processing

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. Monitor the route of docker daemon and go to SystemInfo for processing.

Github.com/docker/docker/api/server/router/system/system.go

/ / NewRouter initializes a new system routerfunc 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. After entering the systemInfo processing method, we can know that the container status information is already in memory.

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. Find the data method to set the running status of 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. Container status data source

6.1. follow the setting method to find that a piece of data will be set when the container is created, which is the initialized data

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. Container operation (pause, start, resume)

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.After docker restarts, get the container status from the directory

Github.com/docker/docker/daemon/daemon.go

Func (daemon * Daemon) restore () error {for _, c: = range containers {/ / get the container status daemon.setStateCounter (c) / if the status in the file is running or paused, check again And reset the status if c.IsRunning () | | c.IsPaused () {default: / / running c.Lock () c.Paused = false daemon.setStateCounter (c)

7. Event change resets container state (windows)

7.1. information method of container status change

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 event monitoring

Github.com/docker/docker/daemon/monitor.go

/ / ProcessEvent is called by libcontainerd whenever an event occursfunc (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. Enable the debug mode when starting docker, and obtain file description, goroute and other information

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 (), "SystemTime:", info.SystemTime) fmt.Fprintln (dockerCli.Out (), "EventsListeners:", info.NEventsListener)} all the contents of the article "info Command request flow Analysis in docker" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report