github.com/rawahars/moby@v24.0.4+incompatible/cmd/dockerd/daemon_unix.go (about) 1 //go:build !windows 2 // +build !windows 3 4 package main 5 6 import ( 7 "context" 8 "net" 9 "os" 10 "os/signal" 11 "path/filepath" 12 "strconv" 13 "time" 14 15 "github.com/docker/docker/daemon" 16 "github.com/docker/docker/daemon/config" 17 "github.com/docker/docker/libcontainerd/supervisor" 18 "github.com/docker/docker/libnetwork/portallocator" 19 "github.com/docker/docker/pkg/homedir" 20 "github.com/pkg/errors" 21 "github.com/sirupsen/logrus" 22 "golang.org/x/sys/unix" 23 ) 24 25 func getDefaultDaemonConfigDir() (string, error) { 26 if !honorXDG { 27 return "/etc/docker", nil 28 } 29 // NOTE: CLI uses ~/.docker while the daemon uses ~/.config/docker, because 30 // ~/.docker was not designed to store daemon configurations. 31 // In future, the daemon directory may be renamed to ~/.config/moby-engine (?). 32 configHome, err := homedir.GetConfigHome() 33 if err != nil { 34 return "", nil 35 } 36 return filepath.Join(configHome, "docker"), nil 37 } 38 39 func getDefaultDaemonConfigFile() (string, error) { 40 dir, err := getDefaultDaemonConfigDir() 41 if err != nil { 42 return "", err 43 } 44 return filepath.Join(dir, "daemon.json"), nil 45 } 46 47 // setDefaultUmask sets the umask to 0022 to avoid problems 48 // caused by custom umask 49 func setDefaultUmask() error { 50 desiredUmask := 0022 51 unix.Umask(desiredUmask) 52 if umask := unix.Umask(desiredUmask); umask != desiredUmask { 53 return errors.Errorf("failed to set umask: expected %#o, got %#o", desiredUmask, umask) 54 } 55 56 return nil 57 } 58 59 func (cli *DaemonCli) getPlatformContainerdDaemonOpts() ([]supervisor.DaemonOpt, error) { 60 opts := []supervisor.DaemonOpt{ 61 // TODO(thaJeztah) change this to use /proc/self/oom_score_adj instead, 62 // which would allow us to set the correct score even if dockerd's score 63 // was set through other means (such as systemd or "manually"). 64 supervisor.WithOOMScore(cli.Config.OOMScoreAdjust), //nolint:staticcheck // ignore SA1019 (WithOOMScore is deprecated); will be removed in the next release. 65 } 66 if cli.Config.OOMScoreAdjust != 0 { 67 logrus.Warn(`DEPRECATED: The "oom-score-adjust" config parameter and the dockerd "--oom-score-adjust" option will be removed in the next release.`) 68 } 69 return opts, nil 70 } 71 72 // setupConfigReloadTrap configures the SIGHUP signal to reload the configuration. 73 func (cli *DaemonCli) setupConfigReloadTrap() { 74 c := make(chan os.Signal, 1) 75 signal.Notify(c, unix.SIGHUP) 76 go func() { 77 for range c { 78 cli.reloadConfig() 79 } 80 }() 81 } 82 83 // getSwarmRunRoot gets the root directory for swarm to store runtime state 84 // For example, the control socket 85 func (cli *DaemonCli) getSwarmRunRoot() string { 86 return filepath.Join(cli.Config.ExecRoot, "swarm") 87 } 88 89 // allocateDaemonPort ensures that there are no containers 90 // that try to use any port allocated for the docker server. 91 func allocateDaemonPort(addr string) error { 92 host, port, err := net.SplitHostPort(addr) 93 if err != nil { 94 return errors.Wrap(err, "error parsing tcp address") 95 } 96 97 intPort, err := strconv.Atoi(port) 98 if err != nil { 99 return errors.Wrap(err, "error parsing tcp address") 100 } 101 102 var hostIPs []net.IP 103 if parsedIP := net.ParseIP(host); parsedIP != nil { 104 hostIPs = append(hostIPs, parsedIP) 105 } else if hostIPs, err = net.LookupIP(host); err != nil { 106 return errors.Errorf("failed to lookup %s address in host specification", host) 107 } 108 109 pa := portallocator.Get() 110 for _, hostIP := range hostIPs { 111 if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil { 112 return errors.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err) 113 } 114 } 115 return nil 116 } 117 118 func newCgroupParent(config *config.Config) string { 119 cgroupParent := "docker" 120 useSystemd := daemon.UsingSystemd(config) 121 if useSystemd { 122 cgroupParent = "system.slice" 123 } 124 if config.CgroupParent != "" { 125 cgroupParent = config.CgroupParent 126 } 127 if useSystemd { 128 cgroupParent = cgroupParent + ":" + "docker" + ":" 129 } 130 return cgroupParent 131 } 132 133 func (cli *DaemonCli) initContainerd(ctx context.Context) (func(time.Duration) error, error) { 134 if cli.ContainerdAddr != "" { 135 // use system containerd at the given address. 136 return nil, nil 137 } 138 139 systemContainerdAddr, ok, err := systemContainerdRunning(honorXDG) 140 if err != nil { 141 return nil, errors.Wrap(err, "could not determine whether the system containerd is running") 142 } 143 if ok { 144 // detected a system containerd at the given address. 145 cli.ContainerdAddr = systemContainerdAddr 146 return nil, nil 147 } 148 149 logrus.Info("containerd not running, starting managed containerd") 150 opts, err := cli.getContainerdDaemonOpts() 151 if err != nil { 152 return nil, errors.Wrap(err, "failed to generate containerd options") 153 } 154 155 r, err := supervisor.Start(ctx, filepath.Join(cli.Root, "containerd"), filepath.Join(cli.ExecRoot, "containerd"), opts...) 156 if err != nil { 157 return nil, errors.Wrap(err, "failed to start containerd") 158 } 159 cli.ContainerdAddr = r.Address() 160 161 // Try to wait for containerd to shutdown 162 return r.WaitTimeout, nil 163 }