create.go 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "bytes"
  16. "context"
  17. "encoding/json"
  18. "errors"
  19. "fmt"
  20. "io/fs"
  21. "os"
  22. "path/filepath"
  23. "sort"
  24. "strconv"
  25. "strings"
  26. "github.com/compose-spec/compose-go/v2/types"
  27. "github.com/docker/compose/v2/internal/desktop"
  28. pathutil "github.com/docker/compose/v2/internal/paths"
  29. "github.com/docker/compose/v2/pkg/api"
  30. "github.com/docker/compose/v2/pkg/progress"
  31. "github.com/docker/compose/v2/pkg/prompt"
  32. "github.com/docker/compose/v2/pkg/utils"
  33. "github.com/docker/docker/api/types/blkiodev"
  34. "github.com/docker/docker/api/types/container"
  35. "github.com/docker/docker/api/types/filters"
  36. "github.com/docker/docker/api/types/image"
  37. "github.com/docker/docker/api/types/mount"
  38. "github.com/docker/docker/api/types/network"
  39. "github.com/docker/docker/api/types/strslice"
  40. "github.com/docker/docker/api/types/versions"
  41. volumetypes "github.com/docker/docker/api/types/volume"
  42. "github.com/docker/docker/errdefs"
  43. "github.com/docker/go-connections/nat"
  44. "github.com/sirupsen/logrus"
  45. cdi "tags.cncf.io/container-device-interface/pkg/parser"
  46. )
  47. type createOptions struct {
  48. AutoRemove bool
  49. AttachStdin bool
  50. UseNetworkAliases bool
  51. Labels types.Labels
  52. }
  53. type createConfigs struct {
  54. Container *container.Config
  55. Host *container.HostConfig
  56. Network *network.NetworkingConfig
  57. Links []string
  58. }
  59. func (s *composeService) Create(ctx context.Context, project *types.Project, createOpts api.CreateOptions) error {
  60. return progress.RunWithTitle(ctx, func(ctx context.Context) error {
  61. return s.create(ctx, project, createOpts)
  62. }, s.stdinfo(), "Creating")
  63. }
  64. func (s *composeService) create(ctx context.Context, project *types.Project, options api.CreateOptions) error {
  65. if len(options.Services) == 0 {
  66. options.Services = project.ServiceNames()
  67. }
  68. err := project.CheckContainerNameUnicity()
  69. if err != nil {
  70. return err
  71. }
  72. err = s.ensureImagesExists(ctx, project, options.Build, options.QuietPull)
  73. if err != nil {
  74. return err
  75. }
  76. prepareNetworks(project)
  77. networks, err := s.ensureNetworks(ctx, project)
  78. if err != nil {
  79. return err
  80. }
  81. volumes, err := s.ensureProjectVolumes(ctx, project, options.AssumeYes)
  82. if err != nil {
  83. return err
  84. }
  85. var observedState Containers
  86. observedState, err = s.getContainers(ctx, project.Name, oneOffInclude, true)
  87. if err != nil {
  88. return err
  89. }
  90. orphans := observedState.filter(isOrphaned(project))
  91. if len(orphans) > 0 && !options.IgnoreOrphans {
  92. if options.RemoveOrphans {
  93. err := s.removeContainers(ctx, orphans, nil, nil, false)
  94. if err != nil {
  95. return err
  96. }
  97. } else {
  98. logrus.Warnf("Found orphan containers (%s) for this project. If "+
  99. "you removed or renamed this service in your compose "+
  100. "file, you can run this command with the "+
  101. "--remove-orphans flag to clean it up.", orphans.names())
  102. }
  103. }
  104. return newConvergence(options.Services, observedState, networks, volumes, s).apply(ctx, project, options)
  105. }
  106. func prepareNetworks(project *types.Project) {
  107. for k, nw := range project.Networks {
  108. nw.CustomLabels = nw.CustomLabels.
  109. Add(api.NetworkLabel, k).
  110. Add(api.ProjectLabel, project.Name).
  111. Add(api.VersionLabel, api.ComposeVersion)
  112. project.Networks[k] = nw
  113. }
  114. }
  115. func (s *composeService) ensureNetworks(ctx context.Context, project *types.Project) (map[string]string, error) {
  116. networks := map[string]string{}
  117. for name, nw := range project.Networks {
  118. id, err := s.ensureNetwork(ctx, project, name, &nw)
  119. if err != nil {
  120. return nil, err
  121. }
  122. networks[name] = id
  123. project.Networks[name] = nw
  124. }
  125. return networks, nil
  126. }
  127. func (s *composeService) ensureProjectVolumes(ctx context.Context, project *types.Project, assumeYes bool) (map[string]string, error) {
  128. ids := map[string]string{}
  129. for k, volume := range project.Volumes {
  130. volume.CustomLabels = volume.CustomLabels.Add(api.VolumeLabel, k)
  131. volume.CustomLabels = volume.CustomLabels.Add(api.ProjectLabel, project.Name)
  132. volume.CustomLabels = volume.CustomLabels.Add(api.VersionLabel, api.ComposeVersion)
  133. id, err := s.ensureVolume(ctx, k, volume, project, assumeYes)
  134. if err != nil {
  135. return nil, err
  136. }
  137. ids[k] = id
  138. }
  139. err := func() error {
  140. if s.manageDesktopFileSharesEnabled(ctx) {
  141. // collect all the bind mount paths and try to set up file shares in
  142. // Docker Desktop for them
  143. var paths []string
  144. for _, svcName := range project.ServiceNames() {
  145. svc := project.Services[svcName]
  146. for _, vol := range svc.Volumes {
  147. if vol.Type != string(mount.TypeBind) {
  148. continue
  149. }
  150. p := filepath.Clean(vol.Source)
  151. if !filepath.IsAbs(p) {
  152. return fmt.Errorf("file share path is not absolute: %s", p)
  153. }
  154. if fi, err := os.Stat(p); errors.Is(err, fs.ErrNotExist) {
  155. // actual directory will be implicitly created when the
  156. // file share is initialized if it doesn't exist, so
  157. // need to filter out any that should not be auto-created
  158. if vol.Bind != nil && !vol.Bind.CreateHostPath {
  159. logrus.Debugf("Skipping creating file share for %q: does not exist and `create_host_path` is false", p)
  160. continue
  161. }
  162. } else if err != nil {
  163. // if we can't read the path, we won't be able to make
  164. // a file share for it
  165. logrus.Debugf("Skipping creating file share for %q: %v", p, err)
  166. continue
  167. } else if !fi.IsDir() {
  168. // ignore files & special types (e.g. Unix sockets)
  169. logrus.Debugf("Skipping creating file share for %q: not a directory", p)
  170. continue
  171. }
  172. paths = append(paths, p)
  173. }
  174. }
  175. // remove duplicate/unnecessary child paths and sort them for predictability
  176. paths = pathutil.EncompassingPaths(paths)
  177. sort.Strings(paths)
  178. fileShareManager := desktop.NewFileShareManager(s.desktopCli, project.Name, paths)
  179. if err := fileShareManager.EnsureExists(ctx); err != nil {
  180. return fmt.Errorf("initializing file shares: %w", err)
  181. }
  182. }
  183. return nil
  184. }()
  185. if err != nil {
  186. progress.ContextWriter(ctx).TailMsgf("Failed to prepare Synchronized file shares: %v", err)
  187. }
  188. return ids, nil
  189. }
  190. func (s *composeService) getCreateConfigs(ctx context.Context,
  191. p *types.Project,
  192. service types.ServiceConfig,
  193. number int,
  194. inherit *container.Summary,
  195. opts createOptions,
  196. ) (createConfigs, error) {
  197. labels, err := s.prepareLabels(opts.Labels, service, number)
  198. if err != nil {
  199. return createConfigs{}, err
  200. }
  201. var (
  202. runCmd strslice.StrSlice
  203. entrypoint strslice.StrSlice
  204. )
  205. if service.Command != nil {
  206. runCmd = strslice.StrSlice(service.Command)
  207. }
  208. if service.Entrypoint != nil {
  209. entrypoint = strslice.StrSlice(service.Entrypoint)
  210. }
  211. var (
  212. tty = service.Tty
  213. stdinOpen = service.StdinOpen
  214. )
  215. proxyConfig := types.MappingWithEquals(s.configFile().ParseProxyConfig(s.apiClient().DaemonHost(), nil))
  216. env := proxyConfig.OverrideBy(service.Environment)
  217. var mainNwName string
  218. var mainNw *types.ServiceNetworkConfig
  219. if len(service.Networks) > 0 {
  220. mainNwName = service.NetworksByPriority()[0]
  221. mainNw = service.Networks[mainNwName]
  222. }
  223. macAddress, err := s.prepareContainerMACAddress(ctx, service, mainNw, mainNwName)
  224. if err != nil {
  225. return createConfigs{}, err
  226. }
  227. healthcheck, err := s.ToMobyHealthCheck(ctx, service.HealthCheck)
  228. if err != nil {
  229. return createConfigs{}, err
  230. }
  231. containerConfig := container.Config{
  232. Hostname: service.Hostname,
  233. Domainname: service.DomainName,
  234. User: service.User,
  235. ExposedPorts: buildContainerPorts(service),
  236. Tty: tty,
  237. OpenStdin: stdinOpen,
  238. StdinOnce: opts.AttachStdin && stdinOpen,
  239. AttachStdin: opts.AttachStdin,
  240. AttachStderr: true,
  241. AttachStdout: true,
  242. Cmd: runCmd,
  243. Image: api.GetImageNameOrDefault(service, p.Name),
  244. WorkingDir: service.WorkingDir,
  245. Entrypoint: entrypoint,
  246. NetworkDisabled: service.NetworkMode == "disabled",
  247. MacAddress: macAddress, // Field is deprecated since API v1.44, but kept for compatibility with older API versions.
  248. Labels: labels,
  249. StopSignal: service.StopSignal,
  250. Env: ToMobyEnv(env),
  251. Healthcheck: healthcheck,
  252. StopTimeout: ToSeconds(service.StopGracePeriod),
  253. } // VOLUMES/MOUNTS/FILESYSTEMS
  254. tmpfs := map[string]string{}
  255. for _, t := range service.Tmpfs {
  256. if arr := strings.SplitN(t, ":", 2); len(arr) > 1 {
  257. tmpfs[arr[0]] = arr[1]
  258. } else {
  259. tmpfs[arr[0]] = ""
  260. }
  261. }
  262. binds, mounts, err := s.buildContainerVolumes(ctx, *p, service, inherit)
  263. if err != nil {
  264. return createConfigs{}, err
  265. }
  266. // NETWORKING
  267. links, err := s.getLinks(ctx, p.Name, service, number)
  268. if err != nil {
  269. return createConfigs{}, err
  270. }
  271. apiVersion, err := s.RuntimeVersion(ctx)
  272. if err != nil {
  273. return createConfigs{}, err
  274. }
  275. networkMode, networkingConfig := defaultNetworkSettings(p, service, number, links, opts.UseNetworkAliases, apiVersion)
  276. portBindings := buildContainerPortBindingOptions(service)
  277. // MISC
  278. resources := getDeployResources(service)
  279. var logConfig container.LogConfig
  280. if service.Logging != nil {
  281. logConfig = container.LogConfig{
  282. Type: service.Logging.Driver,
  283. Config: service.Logging.Options,
  284. }
  285. }
  286. securityOpts, unconfined, err := parseSecurityOpts(p, service.SecurityOpt)
  287. if err != nil {
  288. return createConfigs{}, err
  289. }
  290. hostConfig := container.HostConfig{
  291. AutoRemove: opts.AutoRemove,
  292. Annotations: service.Annotations,
  293. Binds: binds,
  294. Mounts: mounts,
  295. CapAdd: strslice.StrSlice(service.CapAdd),
  296. CapDrop: strslice.StrSlice(service.CapDrop),
  297. NetworkMode: networkMode,
  298. Init: service.Init,
  299. IpcMode: container.IpcMode(service.Ipc),
  300. CgroupnsMode: container.CgroupnsMode(service.Cgroup),
  301. ReadonlyRootfs: service.ReadOnly,
  302. RestartPolicy: getRestartPolicy(service),
  303. ShmSize: int64(service.ShmSize),
  304. Sysctls: service.Sysctls,
  305. PortBindings: portBindings,
  306. Resources: resources,
  307. VolumeDriver: service.VolumeDriver,
  308. VolumesFrom: service.VolumesFrom,
  309. DNS: service.DNS,
  310. DNSSearch: service.DNSSearch,
  311. DNSOptions: service.DNSOpts,
  312. ExtraHosts: service.ExtraHosts.AsList(":"),
  313. SecurityOpt: securityOpts,
  314. StorageOpt: service.StorageOpt,
  315. UsernsMode: container.UsernsMode(service.UserNSMode),
  316. UTSMode: container.UTSMode(service.Uts),
  317. Privileged: service.Privileged,
  318. PidMode: container.PidMode(service.Pid),
  319. Tmpfs: tmpfs,
  320. Isolation: container.Isolation(service.Isolation),
  321. Runtime: service.Runtime,
  322. LogConfig: logConfig,
  323. GroupAdd: service.GroupAdd,
  324. Links: links,
  325. OomScoreAdj: int(service.OomScoreAdj),
  326. }
  327. if unconfined {
  328. hostConfig.MaskedPaths = []string{}
  329. hostConfig.ReadonlyPaths = []string{}
  330. }
  331. cfgs := createConfigs{
  332. Container: &containerConfig,
  333. Host: &hostConfig,
  334. Network: networkingConfig,
  335. Links: links,
  336. }
  337. return cfgs, nil
  338. }
  339. // prepareContainerMACAddress handles the service-level mac_address field and the newer mac_address field added to service
  340. // network config. This newer field is only compatible with the Engine API v1.44 (and onwards), and this API version
  341. // also deprecates the container-wide mac_address field. Thus, this method will validate service config and mutate the
  342. // passed mainNw to provide backward-compatibility whenever possible.
  343. //
  344. // It returns the container-wide MAC address, but this value will be kept empty for newer API versions.
  345. func (s *composeService) prepareContainerMACAddress(ctx context.Context, service types.ServiceConfig, mainNw *types.ServiceNetworkConfig, nwName string) (string, error) {
  346. version, err := s.RuntimeVersion(ctx)
  347. if err != nil {
  348. return "", err
  349. }
  350. // Engine API 1.44 added support for endpoint-specific MAC address and now returns a warning when a MAC address is
  351. // set in container.Config. Thus, we have to jump through a number of hoops:
  352. //
  353. // 1. Top-level mac_address and main endpoint's MAC address should be the same ;
  354. // 2. If supported by the API, top-level mac_address should be migrated to the main endpoint and container.Config
  355. // should be kept empty ;
  356. // 3. Otherwise, the endpoint mac_address should be set in container.Config and no other endpoint-specific
  357. // mac_address can be specified. If that's the case, use top-level mac_address ;
  358. //
  359. // After that, if an endpoint mac_address is set, it's either user-defined or migrated by the code below, so
  360. // there's no need to check for API version in defaultNetworkSettings.
  361. macAddress := service.MacAddress
  362. if macAddress != "" && mainNw != nil && mainNw.MacAddress != "" && mainNw.MacAddress != macAddress {
  363. return "", fmt.Errorf("the service-level mac_address should have the same value as network %s", nwName)
  364. }
  365. if versions.GreaterThanOrEqualTo(version, "1.44") {
  366. if mainNw != nil && mainNw.MacAddress == "" {
  367. mainNw.MacAddress = macAddress
  368. }
  369. macAddress = ""
  370. } else if len(service.Networks) > 0 {
  371. var withMacAddress []string
  372. for nwName, nw := range service.Networks {
  373. if nw != nil && nw.MacAddress != "" {
  374. withMacAddress = append(withMacAddress, nwName)
  375. }
  376. }
  377. if len(withMacAddress) > 1 {
  378. return "", fmt.Errorf("a MAC address is specified for multiple networks (%s), but this feature requires Docker Engine 1.44 or later (currently: %s)", strings.Join(withMacAddress, ", "), version)
  379. }
  380. if mainNw != nil && mainNw.MacAddress != "" {
  381. macAddress = mainNw.MacAddress
  382. }
  383. }
  384. return macAddress, nil
  385. }
  386. func getAliases(project *types.Project, service types.ServiceConfig, serviceIndex int, cfg *types.ServiceNetworkConfig, useNetworkAliases bool) []string {
  387. aliases := []string{getContainerName(project.Name, service, serviceIndex)}
  388. if useNetworkAliases {
  389. aliases = append(aliases, service.Name)
  390. if cfg != nil {
  391. aliases = append(aliases, cfg.Aliases...)
  392. }
  393. }
  394. return aliases
  395. }
  396. func createEndpointSettings(p *types.Project, service types.ServiceConfig, serviceIndex int, networkKey string, links []string, useNetworkAliases bool) *network.EndpointSettings {
  397. config := service.Networks[networkKey]
  398. var ipam *network.EndpointIPAMConfig
  399. var (
  400. ipv4Address string
  401. ipv6Address string
  402. macAddress string
  403. driverOpts types.Options
  404. gwPriority int
  405. )
  406. if config != nil {
  407. ipv4Address = config.Ipv4Address
  408. ipv6Address = config.Ipv6Address
  409. ipam = &network.EndpointIPAMConfig{
  410. IPv4Address: ipv4Address,
  411. IPv6Address: ipv6Address,
  412. LinkLocalIPs: config.LinkLocalIPs,
  413. }
  414. macAddress = config.MacAddress
  415. driverOpts = config.DriverOpts
  416. gwPriority = config.GatewayPriority
  417. }
  418. return &network.EndpointSettings{
  419. Aliases: getAliases(p, service, serviceIndex, config, useNetworkAliases),
  420. Links: links,
  421. IPAddress: ipv4Address,
  422. IPv6Gateway: ipv6Address,
  423. IPAMConfig: ipam,
  424. MacAddress: macAddress,
  425. DriverOpts: driverOpts,
  426. GwPriority: gwPriority,
  427. }
  428. }
  429. // copy/pasted from https://github.com/docker/cli/blob/9de1b162f/cli/command/container/opts.go#L673-L697 + RelativePath
  430. // TODO find so way to share this code with docker/cli
  431. func parseSecurityOpts(p *types.Project, securityOpts []string) ([]string, bool, error) {
  432. var (
  433. unconfined bool
  434. parsed []string
  435. )
  436. for _, opt := range securityOpts {
  437. if opt == "systempaths=unconfined" {
  438. unconfined = true
  439. continue
  440. }
  441. con := strings.SplitN(opt, "=", 2)
  442. if len(con) == 1 && con[0] != "no-new-privileges" {
  443. if strings.Contains(opt, ":") {
  444. con = strings.SplitN(opt, ":", 2)
  445. } else {
  446. return securityOpts, false, fmt.Errorf("invalid security-opt: %q", opt)
  447. }
  448. }
  449. if con[0] == "seccomp" && con[1] != "unconfined" && con[1] != "builtin" {
  450. f, err := os.ReadFile(p.RelativePath(con[1]))
  451. if err != nil {
  452. return securityOpts, false, fmt.Errorf("opening seccomp profile (%s) failed: %w", con[1], err)
  453. }
  454. b := bytes.NewBuffer(nil)
  455. if err := json.Compact(b, f); err != nil {
  456. return securityOpts, false, fmt.Errorf("compacting json for seccomp profile (%s) failed: %w", con[1], err)
  457. }
  458. parsed = append(parsed, fmt.Sprintf("seccomp=%s", b.Bytes()))
  459. } else {
  460. parsed = append(parsed, opt)
  461. }
  462. }
  463. return parsed, unconfined, nil
  464. }
  465. func (s *composeService) prepareLabels(labels types.Labels, service types.ServiceConfig, number int) (map[string]string, error) {
  466. hash, err := ServiceHash(service)
  467. if err != nil {
  468. return nil, err
  469. }
  470. labels[api.ConfigHashLabel] = hash
  471. if number > 0 {
  472. // One-off containers are not indexed
  473. labels[api.ContainerNumberLabel] = strconv.Itoa(number)
  474. }
  475. var dependencies []string
  476. for s, d := range service.DependsOn {
  477. dependencies = append(dependencies, fmt.Sprintf("%s:%s:%t", s, d.Condition, d.Restart))
  478. }
  479. labels[api.DependenciesLabel] = strings.Join(dependencies, ",")
  480. return labels, nil
  481. }
  482. // defaultNetworkSettings determines the container.NetworkMode and corresponding network.NetworkingConfig (nil if not applicable).
  483. func defaultNetworkSettings(
  484. project *types.Project,
  485. service types.ServiceConfig,
  486. serviceIndex int,
  487. links []string,
  488. useNetworkAliases bool,
  489. version string,
  490. ) (container.NetworkMode, *network.NetworkingConfig) {
  491. if service.NetworkMode != "" {
  492. return container.NetworkMode(service.NetworkMode), nil
  493. }
  494. if len(project.Networks) == 0 {
  495. return "none", nil
  496. }
  497. var primaryNetworkKey string
  498. if len(service.Networks) > 0 {
  499. primaryNetworkKey = service.NetworksByPriority()[0]
  500. } else {
  501. primaryNetworkKey = "default"
  502. }
  503. primaryNetworkMobyNetworkName := project.Networks[primaryNetworkKey].Name
  504. primaryNetworkEndpoint := createEndpointSettings(project, service, serviceIndex, primaryNetworkKey, links, useNetworkAliases)
  505. endpointsConfig := map[string]*network.EndpointSettings{}
  506. // Starting from API version 1.44, the Engine will take several EndpointsConfigs
  507. // so we can pass all the extra networks we want the container to be connected to
  508. // in the network configuration instead of connecting the container to each extra
  509. // network individually after creation.
  510. if versions.GreaterThanOrEqualTo(version, "1.44") {
  511. if len(service.Networks) > 1 {
  512. serviceNetworks := service.NetworksByPriority()
  513. for _, networkKey := range serviceNetworks[1:] {
  514. mobyNetworkName := project.Networks[networkKey].Name
  515. epSettings := createEndpointSettings(project, service, serviceIndex, networkKey, links, useNetworkAliases)
  516. endpointsConfig[mobyNetworkName] = epSettings
  517. }
  518. }
  519. if primaryNetworkEndpoint.MacAddress == "" {
  520. primaryNetworkEndpoint.MacAddress = service.MacAddress
  521. }
  522. }
  523. endpointsConfig[primaryNetworkMobyNetworkName] = primaryNetworkEndpoint
  524. networkConfig := &network.NetworkingConfig{
  525. EndpointsConfig: endpointsConfig,
  526. }
  527. // From the Engine API docs:
  528. // > Supported standard values are: bridge, host, none, and container:<name|id>.
  529. // > Any other value is taken as a custom network's name to which this container should connect to.
  530. return container.NetworkMode(primaryNetworkMobyNetworkName), networkConfig
  531. }
  532. func getRestartPolicy(service types.ServiceConfig) container.RestartPolicy {
  533. var restart container.RestartPolicy
  534. if service.Restart != "" {
  535. split := strings.Split(service.Restart, ":")
  536. var attempts int
  537. if len(split) > 1 {
  538. attempts, _ = strconv.Atoi(split[1])
  539. }
  540. restart = container.RestartPolicy{
  541. Name: mapRestartPolicyCondition(split[0]),
  542. MaximumRetryCount: attempts,
  543. }
  544. }
  545. if service.Deploy != nil && service.Deploy.RestartPolicy != nil {
  546. policy := *service.Deploy.RestartPolicy
  547. var attempts int
  548. if policy.MaxAttempts != nil {
  549. attempts = int(*policy.MaxAttempts)
  550. }
  551. restart = container.RestartPolicy{
  552. Name: mapRestartPolicyCondition(policy.Condition),
  553. MaximumRetryCount: attempts,
  554. }
  555. }
  556. return restart
  557. }
  558. func mapRestartPolicyCondition(condition string) container.RestartPolicyMode {
  559. // map definitions of deploy.restart_policy to engine definitions
  560. switch condition {
  561. case "none", "no":
  562. return container.RestartPolicyDisabled
  563. case "on-failure":
  564. return container.RestartPolicyOnFailure
  565. case "unless-stopped":
  566. return container.RestartPolicyUnlessStopped
  567. case "any", "always":
  568. return container.RestartPolicyAlways
  569. default:
  570. return container.RestartPolicyMode(condition)
  571. }
  572. }
  573. func getDeployResources(s types.ServiceConfig) container.Resources {
  574. var swappiness *int64
  575. if s.MemSwappiness != 0 {
  576. val := int64(s.MemSwappiness)
  577. swappiness = &val
  578. }
  579. resources := container.Resources{
  580. CgroupParent: s.CgroupParent,
  581. Memory: int64(s.MemLimit),
  582. MemorySwap: int64(s.MemSwapLimit),
  583. MemorySwappiness: swappiness,
  584. MemoryReservation: int64(s.MemReservation),
  585. OomKillDisable: &s.OomKillDisable,
  586. CPUCount: s.CPUCount,
  587. CPUPeriod: s.CPUPeriod,
  588. CPUQuota: s.CPUQuota,
  589. CPURealtimePeriod: s.CPURTPeriod,
  590. CPURealtimeRuntime: s.CPURTRuntime,
  591. CPUShares: s.CPUShares,
  592. NanoCPUs: int64(s.CPUS * 1e9),
  593. CPUPercent: int64(s.CPUPercent * 100),
  594. CpusetCpus: s.CPUSet,
  595. DeviceCgroupRules: s.DeviceCgroupRules,
  596. }
  597. if s.PidsLimit != 0 {
  598. resources.PidsLimit = &s.PidsLimit
  599. }
  600. setBlkio(s.BlkioConfig, &resources)
  601. if s.Deploy != nil {
  602. setLimits(s.Deploy.Resources.Limits, &resources)
  603. setReservations(s.Deploy.Resources.Reservations, &resources)
  604. }
  605. var cdiDeviceNames []string
  606. for _, device := range s.Devices {
  607. if device.Source == device.Target && cdi.IsQualifiedName(device.Source) {
  608. cdiDeviceNames = append(cdiDeviceNames, device.Source)
  609. continue
  610. }
  611. resources.Devices = append(resources.Devices, container.DeviceMapping{
  612. PathOnHost: device.Source,
  613. PathInContainer: device.Target,
  614. CgroupPermissions: device.Permissions,
  615. })
  616. }
  617. if len(cdiDeviceNames) > 0 {
  618. resources.DeviceRequests = append(resources.DeviceRequests, container.DeviceRequest{
  619. Driver: "cdi",
  620. DeviceIDs: cdiDeviceNames,
  621. })
  622. }
  623. for _, gpus := range s.Gpus {
  624. resources.DeviceRequests = append(resources.DeviceRequests, container.DeviceRequest{
  625. Driver: gpus.Driver,
  626. Count: int(gpus.Count),
  627. DeviceIDs: gpus.IDs,
  628. Capabilities: [][]string{append(gpus.Capabilities, "gpu")},
  629. Options: gpus.Options,
  630. })
  631. }
  632. ulimits := toUlimits(s.Ulimits)
  633. resources.Ulimits = ulimits
  634. return resources
  635. }
  636. func toUlimits(m map[string]*types.UlimitsConfig) []*container.Ulimit {
  637. var ulimits []*container.Ulimit
  638. for name, u := range m {
  639. soft := u.Single
  640. if u.Soft != 0 {
  641. soft = u.Soft
  642. }
  643. hard := u.Single
  644. if u.Hard != 0 {
  645. hard = u.Hard
  646. }
  647. ulimits = append(ulimits, &container.Ulimit{
  648. Name: name,
  649. Hard: int64(hard),
  650. Soft: int64(soft),
  651. })
  652. }
  653. return ulimits
  654. }
  655. func setReservations(reservations *types.Resource, resources *container.Resources) {
  656. if reservations == nil {
  657. return
  658. }
  659. // Cpu reservation is a swarm option and PIDs is only a limit
  660. // So we only need to map memory reservation and devices
  661. if reservations.MemoryBytes != 0 {
  662. resources.MemoryReservation = int64(reservations.MemoryBytes)
  663. }
  664. for _, device := range reservations.Devices {
  665. resources.DeviceRequests = append(resources.DeviceRequests, container.DeviceRequest{
  666. Capabilities: [][]string{device.Capabilities},
  667. Count: int(device.Count),
  668. DeviceIDs: device.IDs,
  669. Driver: device.Driver,
  670. Options: device.Options,
  671. })
  672. }
  673. }
  674. func setLimits(limits *types.Resource, resources *container.Resources) {
  675. if limits == nil {
  676. return
  677. }
  678. if limits.MemoryBytes != 0 {
  679. resources.Memory = int64(limits.MemoryBytes)
  680. }
  681. if limits.NanoCPUs != 0 {
  682. resources.NanoCPUs = int64(limits.NanoCPUs * 1e9)
  683. }
  684. if limits.Pids > 0 {
  685. resources.PidsLimit = &limits.Pids
  686. }
  687. }
  688. func setBlkio(blkio *types.BlkioConfig, resources *container.Resources) {
  689. if blkio == nil {
  690. return
  691. }
  692. resources.BlkioWeight = blkio.Weight
  693. for _, b := range blkio.WeightDevice {
  694. resources.BlkioWeightDevice = append(resources.BlkioWeightDevice, &blkiodev.WeightDevice{
  695. Path: b.Path,
  696. Weight: b.Weight,
  697. })
  698. }
  699. for _, b := range blkio.DeviceReadBps {
  700. resources.BlkioDeviceReadBps = append(resources.BlkioDeviceReadBps, &blkiodev.ThrottleDevice{
  701. Path: b.Path,
  702. Rate: uint64(b.Rate),
  703. })
  704. }
  705. for _, b := range blkio.DeviceReadIOps {
  706. resources.BlkioDeviceReadIOps = append(resources.BlkioDeviceReadIOps, &blkiodev.ThrottleDevice{
  707. Path: b.Path,
  708. Rate: uint64(b.Rate),
  709. })
  710. }
  711. for _, b := range blkio.DeviceWriteBps {
  712. resources.BlkioDeviceWriteBps = append(resources.BlkioDeviceWriteBps, &blkiodev.ThrottleDevice{
  713. Path: b.Path,
  714. Rate: uint64(b.Rate),
  715. })
  716. }
  717. for _, b := range blkio.DeviceWriteIOps {
  718. resources.BlkioDeviceWriteIOps = append(resources.BlkioDeviceWriteIOps, &blkiodev.ThrottleDevice{
  719. Path: b.Path,
  720. Rate: uint64(b.Rate),
  721. })
  722. }
  723. }
  724. func buildContainerPorts(s types.ServiceConfig) nat.PortSet {
  725. ports := nat.PortSet{}
  726. for _, s := range s.Expose {
  727. p := nat.Port(s)
  728. ports[p] = struct{}{}
  729. }
  730. for _, p := range s.Ports {
  731. p := nat.Port(fmt.Sprintf("%d/%s", p.Target, p.Protocol))
  732. ports[p] = struct{}{}
  733. }
  734. return ports
  735. }
  736. func buildContainerPortBindingOptions(s types.ServiceConfig) nat.PortMap {
  737. bindings := nat.PortMap{}
  738. for _, port := range s.Ports {
  739. p := nat.Port(fmt.Sprintf("%d/%s", port.Target, port.Protocol))
  740. binding := nat.PortBinding{
  741. HostIP: port.HostIP,
  742. HostPort: port.Published,
  743. }
  744. bindings[p] = append(bindings[p], binding)
  745. }
  746. return bindings
  747. }
  748. func getDependentServiceFromMode(mode string) string {
  749. if strings.HasPrefix(
  750. mode,
  751. types.NetworkModeServicePrefix,
  752. ) {
  753. return mode[len(types.NetworkModeServicePrefix):]
  754. }
  755. return ""
  756. }
  757. func (s *composeService) buildContainerVolumes(
  758. ctx context.Context,
  759. p types.Project,
  760. service types.ServiceConfig,
  761. inherit *container.Summary,
  762. ) ([]string, []mount.Mount, error) {
  763. var mounts []mount.Mount
  764. var binds []string
  765. img := api.GetImageNameOrDefault(service, p.Name)
  766. imgInspect, err := s.apiClient().ImageInspect(ctx, img)
  767. if err != nil {
  768. return nil, nil, err
  769. }
  770. mountOptions, err := buildContainerMountOptions(p, service, imgInspect, inherit)
  771. if err != nil {
  772. return nil, nil, err
  773. }
  774. MOUNTS:
  775. for _, m := range mountOptions {
  776. if m.Type == mount.TypeNamedPipe {
  777. mounts = append(mounts, m)
  778. continue
  779. }
  780. if m.Type == mount.TypeBind {
  781. // `Mount` is preferred but does not offer option to created host path if missing
  782. // so `Bind` API is used here with raw volume string
  783. // see https://github.com/moby/moby/issues/43483
  784. for _, v := range service.Volumes {
  785. if v.Target == m.Target {
  786. switch {
  787. case string(m.Type) != v.Type:
  788. v.Source = m.Source
  789. fallthrough
  790. case !requireMountAPI(v.Bind):
  791. binds = append(binds, v.String())
  792. continue MOUNTS
  793. }
  794. }
  795. }
  796. }
  797. if m.Type == mount.TypeImage {
  798. version, err := s.RuntimeVersion(ctx)
  799. if err != nil {
  800. return nil, nil, err
  801. }
  802. if versions.LessThan(version, "1.48") {
  803. return nil, nil, fmt.Errorf("volume with type=image require Docker Engine v28 or later")
  804. }
  805. }
  806. mounts = append(mounts, m)
  807. }
  808. return binds, mounts, nil
  809. }
  810. // requireMountAPI check if Bind declaration can be implemented by the plain old Bind API or uses any of the advanced
  811. // options which require use of Mount API
  812. func requireMountAPI(bind *types.ServiceVolumeBind) bool {
  813. switch {
  814. case bind == nil:
  815. return false
  816. case !bind.CreateHostPath:
  817. return true
  818. case bind.Propagation != "":
  819. return true
  820. case bind.Recursive != "":
  821. return true
  822. default:
  823. return false
  824. }
  825. }
  826. func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img image.InspectResponse, inherit *container.Summary) ([]mount.Mount, error) {
  827. mounts := map[string]mount.Mount{}
  828. if inherit != nil {
  829. for _, m := range inherit.Mounts {
  830. if m.Type == "tmpfs" {
  831. continue
  832. }
  833. src := m.Source
  834. if m.Type == "volume" {
  835. src = m.Name
  836. }
  837. if img.Config != nil {
  838. if _, ok := img.Config.Volumes[m.Destination]; ok {
  839. // inherit previous container's anonymous volume
  840. mounts[m.Destination] = mount.Mount{
  841. Type: m.Type,
  842. Source: src,
  843. Target: m.Destination,
  844. ReadOnly: !m.RW,
  845. }
  846. }
  847. }
  848. volumes := []types.ServiceVolumeConfig{}
  849. for _, v := range s.Volumes {
  850. if v.Target != m.Destination || v.Source != "" {
  851. volumes = append(volumes, v)
  852. continue
  853. }
  854. // inherit previous container's anonymous volume
  855. mounts[m.Destination] = mount.Mount{
  856. Type: m.Type,
  857. Source: src,
  858. Target: m.Destination,
  859. ReadOnly: !m.RW,
  860. }
  861. }
  862. s.Volumes = volumes
  863. }
  864. }
  865. mounts, err := fillBindMounts(p, s, mounts)
  866. if err != nil {
  867. return nil, err
  868. }
  869. values := make([]mount.Mount, 0, len(mounts))
  870. for _, v := range mounts {
  871. values = append(values, v)
  872. }
  873. return values, nil
  874. }
  875. func fillBindMounts(p types.Project, s types.ServiceConfig, m map[string]mount.Mount) (map[string]mount.Mount, error) {
  876. for _, v := range s.Volumes {
  877. bindMount, err := buildMount(p, v)
  878. if err != nil {
  879. return nil, err
  880. }
  881. m[bindMount.Target] = bindMount
  882. }
  883. secrets, err := buildContainerSecretMounts(p, s)
  884. if err != nil {
  885. return nil, err
  886. }
  887. for _, s := range secrets {
  888. if _, found := m[s.Target]; found {
  889. continue
  890. }
  891. m[s.Target] = s
  892. }
  893. configs, err := buildContainerConfigMounts(p, s)
  894. if err != nil {
  895. return nil, err
  896. }
  897. for _, c := range configs {
  898. if _, found := m[c.Target]; found {
  899. continue
  900. }
  901. m[c.Target] = c
  902. }
  903. return m, nil
  904. }
  905. func buildContainerConfigMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
  906. mounts := map[string]mount.Mount{}
  907. configsBaseDir := "/"
  908. for _, config := range s.Configs {
  909. target := config.Target
  910. if config.Target == "" {
  911. target = configsBaseDir + config.Source
  912. } else if !isAbsTarget(config.Target) {
  913. target = configsBaseDir + config.Target
  914. }
  915. definedConfig := p.Configs[config.Source]
  916. if definedConfig.External {
  917. return nil, fmt.Errorf("unsupported external config %s", definedConfig.Name)
  918. }
  919. if definedConfig.Driver != "" {
  920. return nil, errors.New("Docker Compose does not support configs.*.driver") //nolint:staticcheck
  921. }
  922. if definedConfig.TemplateDriver != "" {
  923. return nil, errors.New("Docker Compose does not support configs.*.template_driver") //nolint:staticcheck
  924. }
  925. if definedConfig.Environment != "" || definedConfig.Content != "" {
  926. continue
  927. }
  928. if config.UID != "" || config.GID != "" || config.Mode != nil {
  929. logrus.Warn("config `uid`, `gid` and `mode` are not supported, they will be ignored")
  930. }
  931. bindMount, err := buildMount(p, types.ServiceVolumeConfig{
  932. Type: types.VolumeTypeBind,
  933. Source: definedConfig.File,
  934. Target: target,
  935. ReadOnly: true,
  936. })
  937. if err != nil {
  938. return nil, err
  939. }
  940. mounts[target] = bindMount
  941. }
  942. values := make([]mount.Mount, 0, len(mounts))
  943. for _, v := range mounts {
  944. values = append(values, v)
  945. }
  946. return values, nil
  947. }
  948. func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
  949. mounts := map[string]mount.Mount{}
  950. secretsDir := "/run/secrets/"
  951. for _, secret := range s.Secrets {
  952. target := secret.Target
  953. if secret.Target == "" {
  954. target = secretsDir + secret.Source
  955. } else if !isAbsTarget(secret.Target) {
  956. target = secretsDir + secret.Target
  957. }
  958. definedSecret := p.Secrets[secret.Source]
  959. if definedSecret.External {
  960. return nil, fmt.Errorf("unsupported external secret %s", definedSecret.Name)
  961. }
  962. if definedSecret.Driver != "" {
  963. return nil, errors.New("Docker Compose does not support secrets.*.driver") //nolint:staticcheck
  964. }
  965. if definedSecret.TemplateDriver != "" {
  966. return nil, errors.New("Docker Compose does not support secrets.*.template_driver") //nolint:staticcheck
  967. }
  968. if definedSecret.Environment != "" {
  969. continue
  970. }
  971. if secret.UID != "" || secret.GID != "" || secret.Mode != nil {
  972. logrus.Warn("secrets `uid`, `gid` and `mode` are not supported, they will be ignored")
  973. }
  974. if _, err := os.Stat(definedSecret.File); os.IsNotExist(err) {
  975. logrus.Warnf("secret file %s does not exist", definedSecret.Name)
  976. }
  977. mnt, err := buildMount(p, types.ServiceVolumeConfig{
  978. Type: types.VolumeTypeBind,
  979. Source: definedSecret.File,
  980. Target: target,
  981. ReadOnly: true,
  982. Bind: &types.ServiceVolumeBind{
  983. CreateHostPath: false,
  984. },
  985. })
  986. if err != nil {
  987. return nil, err
  988. }
  989. mounts[target] = mnt
  990. }
  991. values := make([]mount.Mount, 0, len(mounts))
  992. for _, v := range mounts {
  993. values = append(values, v)
  994. }
  995. return values, nil
  996. }
  997. func isAbsTarget(p string) bool {
  998. return isUnixAbs(p) || isWindowsAbs(p)
  999. }
  1000. func isUnixAbs(p string) bool {
  1001. return strings.HasPrefix(p, "/")
  1002. }
  1003. func isWindowsAbs(p string) bool {
  1004. if strings.HasPrefix(p, "\\\\") {
  1005. return true
  1006. }
  1007. if len(p) > 2 && p[1] == ':' {
  1008. return p[2] == '\\'
  1009. }
  1010. return false
  1011. }
  1012. func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {
  1013. source := volume.Source
  1014. // on windows, filepath.IsAbs(source) is false for unix style abs path like /var/run/docker.sock.
  1015. // do not replace these with filepath.Abs(source) that will include a default drive.
  1016. if volume.Type == types.VolumeTypeBind && !filepath.IsAbs(source) && !strings.HasPrefix(source, "/") {
  1017. // volume source has already been prefixed with workdir if required, by compose-go project loader
  1018. var err error
  1019. source, err = filepath.Abs(source)
  1020. if err != nil {
  1021. return mount.Mount{}, err
  1022. }
  1023. }
  1024. if volume.Type == types.VolumeTypeVolume {
  1025. if volume.Source != "" {
  1026. pVolume, ok := project.Volumes[volume.Source]
  1027. if ok {
  1028. source = pVolume.Name
  1029. }
  1030. }
  1031. }
  1032. bind, vol, tmpfs, img := buildMountOptions(volume)
  1033. if bind != nil {
  1034. volume.Type = types.VolumeTypeBind
  1035. }
  1036. return mount.Mount{
  1037. Type: mount.Type(volume.Type),
  1038. Source: source,
  1039. Target: volume.Target,
  1040. ReadOnly: volume.ReadOnly,
  1041. Consistency: mount.Consistency(volume.Consistency),
  1042. BindOptions: bind,
  1043. VolumeOptions: vol,
  1044. TmpfsOptions: tmpfs,
  1045. ImageOptions: img,
  1046. }, nil
  1047. }
  1048. func buildMountOptions(volume types.ServiceVolumeConfig) (*mount.BindOptions, *mount.VolumeOptions, *mount.TmpfsOptions, *mount.ImageOptions) {
  1049. if volume.Type != types.VolumeTypeBind && volume.Bind != nil {
  1050. logrus.Warnf("mount of type `%s` should not define `bind` option", volume.Type)
  1051. }
  1052. if volume.Type != types.VolumeTypeVolume && volume.Volume != nil {
  1053. logrus.Warnf("mount of type `%s` should not define `volume` option", volume.Type)
  1054. }
  1055. if volume.Type != types.VolumeTypeTmpfs && volume.Tmpfs != nil {
  1056. logrus.Warnf("mount of type `%s` should not define `tmpfs` option", volume.Type)
  1057. }
  1058. if volume.Type != types.VolumeTypeImage && volume.Image != nil {
  1059. logrus.Warnf("mount of type `%s` should not define `image` option", volume.Type)
  1060. }
  1061. switch volume.Type {
  1062. case "bind":
  1063. return buildBindOption(volume.Bind), nil, nil, nil
  1064. case "volume":
  1065. return nil, buildVolumeOptions(volume.Volume), nil, nil
  1066. case "tmpfs":
  1067. return nil, nil, buildTmpfsOptions(volume.Tmpfs), nil
  1068. case "image":
  1069. return nil, nil, nil, buildImageOptions(volume.Image)
  1070. }
  1071. return nil, nil, nil, nil
  1072. }
  1073. func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions {
  1074. if bind == nil {
  1075. return nil
  1076. }
  1077. opts := &mount.BindOptions{
  1078. Propagation: mount.Propagation(bind.Propagation),
  1079. CreateMountpoint: bind.CreateHostPath,
  1080. }
  1081. switch bind.Recursive {
  1082. case "disabled":
  1083. opts.NonRecursive = true
  1084. case "writable":
  1085. opts.ReadOnlyNonRecursive = true
  1086. case "readonly":
  1087. opts.ReadOnlyForceRecursive = true
  1088. }
  1089. return opts
  1090. }
  1091. func buildVolumeOptions(vol *types.ServiceVolumeVolume) *mount.VolumeOptions {
  1092. if vol == nil {
  1093. return nil
  1094. }
  1095. return &mount.VolumeOptions{
  1096. NoCopy: vol.NoCopy,
  1097. Subpath: vol.Subpath,
  1098. Labels: vol.Labels,
  1099. // DriverConfig: , // FIXME missing from model ?
  1100. }
  1101. }
  1102. func buildTmpfsOptions(tmpfs *types.ServiceVolumeTmpfs) *mount.TmpfsOptions {
  1103. if tmpfs == nil {
  1104. return nil
  1105. }
  1106. return &mount.TmpfsOptions{
  1107. SizeBytes: int64(tmpfs.Size),
  1108. Mode: os.FileMode(tmpfs.Mode),
  1109. }
  1110. }
  1111. func buildImageOptions(image *types.ServiceVolumeImage) *mount.ImageOptions {
  1112. if image == nil {
  1113. return nil
  1114. }
  1115. return &mount.ImageOptions{
  1116. Subpath: image.SubPath,
  1117. }
  1118. }
  1119. func (s *composeService) ensureNetwork(ctx context.Context, project *types.Project, name string, n *types.NetworkConfig) (string, error) {
  1120. if n.External {
  1121. return s.resolveExternalNetwork(ctx, n)
  1122. }
  1123. id, err := s.resolveOrCreateNetwork(ctx, project, name, n)
  1124. if errdefs.IsConflict(err) {
  1125. // Maybe another execution of `docker compose up|run` created same network
  1126. // let's retry once
  1127. return s.resolveOrCreateNetwork(ctx, project, name, n)
  1128. }
  1129. return id, err
  1130. }
  1131. func (s *composeService) resolveOrCreateNetwork(ctx context.Context, project *types.Project, name string, n *types.NetworkConfig) (string, error) { //nolint:gocyclo
  1132. // First, try to find a unique network matching by name or ID
  1133. inspect, err := s.apiClient().NetworkInspect(ctx, n.Name, network.InspectOptions{})
  1134. if err == nil {
  1135. // NetworkInspect will match on ID prefix, so double check we get the expected one
  1136. // as looking for network named `db` we could erroneously match network ID `db9086999caf`
  1137. if inspect.Name == n.Name || inspect.ID == n.Name {
  1138. p, ok := inspect.Labels[api.ProjectLabel]
  1139. if !ok {
  1140. logrus.Warnf("a network with name %s exists but was not created by compose.\n"+
  1141. "Set `external: true` to use an existing network", n.Name)
  1142. } else if p != project.Name {
  1143. logrus.Warnf("a network with name %s exists but was not created for project %q.\n"+
  1144. "Set `external: true` to use an existing network", n.Name, project.Name)
  1145. }
  1146. if inspect.Labels[api.NetworkLabel] != name {
  1147. return "", fmt.Errorf(
  1148. "network %s was found but has incorrect label %s set to %q (expected: %q)",
  1149. n.Name,
  1150. api.NetworkLabel,
  1151. inspect.Labels[api.NetworkLabel],
  1152. name,
  1153. )
  1154. }
  1155. hash := inspect.Labels[api.ConfigHashLabel]
  1156. expected, err := NetworkHash(n)
  1157. if err != nil {
  1158. return "", err
  1159. }
  1160. if hash == "" || hash == expected {
  1161. return inspect.ID, nil
  1162. }
  1163. err = s.removeDivergedNetwork(ctx, project, name, n)
  1164. if err != nil {
  1165. return "", err
  1166. }
  1167. }
  1168. }
  1169. // ignore other errors. Typically, an ambiguous request by name results in some generic `invalidParameter` error
  1170. // Either not found, or name is ambiguous - use NetworkList to list by name
  1171. networks, err := s.apiClient().NetworkList(ctx, network.ListOptions{
  1172. Filters: filters.NewArgs(filters.Arg("name", n.Name)),
  1173. })
  1174. if err != nil {
  1175. return "", err
  1176. }
  1177. // NetworkList Matches all or part of a network name, so we have to filter for a strict match
  1178. networks = utils.Filter(networks, func(net network.Summary) bool {
  1179. return net.Name == n.Name
  1180. })
  1181. for _, net := range networks {
  1182. if net.Labels[api.ProjectLabel] == project.Name &&
  1183. net.Labels[api.NetworkLabel] == name {
  1184. return net.ID, nil
  1185. }
  1186. }
  1187. // we could have set NetworkList with a projectFilter and networkFilter but not doing so allows to catch this
  1188. // scenario were a network with same name exists but doesn't have label, and use of `CheckDuplicate: true`
  1189. // prevents to create another one.
  1190. if len(networks) > 0 {
  1191. logrus.Warnf("a network with name %s exists but was not created by compose.\n"+
  1192. "Set `external: true` to use an existing network", n.Name)
  1193. return networks[0].ID, nil
  1194. }
  1195. var ipam *network.IPAM
  1196. if n.Ipam.Config != nil {
  1197. var config []network.IPAMConfig
  1198. for _, pool := range n.Ipam.Config {
  1199. config = append(config, network.IPAMConfig{
  1200. Subnet: pool.Subnet,
  1201. IPRange: pool.IPRange,
  1202. Gateway: pool.Gateway,
  1203. AuxAddress: pool.AuxiliaryAddresses,
  1204. })
  1205. }
  1206. ipam = &network.IPAM{
  1207. Driver: n.Ipam.Driver,
  1208. Config: config,
  1209. }
  1210. }
  1211. hash, err := NetworkHash(n)
  1212. if err != nil {
  1213. return "", err
  1214. }
  1215. n.CustomLabels = n.CustomLabels.Add(api.ConfigHashLabel, hash)
  1216. createOpts := network.CreateOptions{
  1217. Labels: mergeLabels(n.Labels, n.CustomLabels),
  1218. Driver: n.Driver,
  1219. Options: n.DriverOpts,
  1220. Internal: n.Internal,
  1221. Attachable: n.Attachable,
  1222. IPAM: ipam,
  1223. EnableIPv6: n.EnableIPv6,
  1224. EnableIPv4: n.EnableIPv4,
  1225. }
  1226. if n.Ipam.Driver != "" || len(n.Ipam.Config) > 0 {
  1227. createOpts.IPAM = &network.IPAM{}
  1228. }
  1229. if n.Ipam.Driver != "" {
  1230. createOpts.IPAM.Driver = n.Ipam.Driver
  1231. }
  1232. for _, ipamConfig := range n.Ipam.Config {
  1233. config := network.IPAMConfig{
  1234. Subnet: ipamConfig.Subnet,
  1235. IPRange: ipamConfig.IPRange,
  1236. Gateway: ipamConfig.Gateway,
  1237. AuxAddress: ipamConfig.AuxiliaryAddresses,
  1238. }
  1239. createOpts.IPAM.Config = append(createOpts.IPAM.Config, config)
  1240. }
  1241. networkEventName := fmt.Sprintf("Network %s", n.Name)
  1242. w := progress.ContextWriter(ctx)
  1243. w.Event(progress.CreatingEvent(networkEventName))
  1244. resp, err := s.apiClient().NetworkCreate(ctx, n.Name, createOpts)
  1245. if err != nil {
  1246. w.Event(progress.ErrorEvent(networkEventName))
  1247. return "", fmt.Errorf("failed to create network %s: %w", n.Name, err)
  1248. }
  1249. w.Event(progress.CreatedEvent(networkEventName))
  1250. return resp.ID, nil
  1251. }
  1252. func (s *composeService) removeDivergedNetwork(ctx context.Context, project *types.Project, name string, n *types.NetworkConfig) error {
  1253. // Remove services attached to this network to force recreation
  1254. var services []string
  1255. for _, service := range project.Services.Filter(func(config types.ServiceConfig) bool {
  1256. _, ok := config.Networks[name]
  1257. return ok
  1258. }) {
  1259. services = append(services, service.Name)
  1260. }
  1261. // Stop containers so we can remove network
  1262. // They will be restarted (actually: recreated) with the updated network
  1263. err := s.stop(ctx, project.Name, api.StopOptions{
  1264. Services: services,
  1265. Project: project,
  1266. })
  1267. if err != nil {
  1268. return err
  1269. }
  1270. err = s.apiClient().NetworkRemove(ctx, n.Name)
  1271. eventName := fmt.Sprintf("Network %s", n.Name)
  1272. progress.ContextWriter(ctx).Event(progress.RemovedEvent(eventName))
  1273. return err
  1274. }
  1275. func (s *composeService) resolveExternalNetwork(ctx context.Context, n *types.NetworkConfig) (string, error) {
  1276. // NetworkInspect will match on ID prefix, so NetworkList with a name
  1277. // filter is used to look for an exact match to prevent e.g. a network
  1278. // named `db` from getting erroneously matched to a network with an ID
  1279. // like `db9086999caf`
  1280. networks, err := s.apiClient().NetworkList(ctx, network.ListOptions{
  1281. Filters: filters.NewArgs(filters.Arg("name", n.Name)),
  1282. })
  1283. if err != nil {
  1284. return "", err
  1285. }
  1286. if len(networks) == 0 {
  1287. // in this instance, n.Name is really an ID
  1288. sn, err := s.apiClient().NetworkInspect(ctx, n.Name, network.InspectOptions{})
  1289. if err != nil && !errdefs.IsNotFound(err) {
  1290. return "", err
  1291. }
  1292. networks = append(networks, sn)
  1293. }
  1294. // NetworkList API doesn't return the exact name match, so we can retrieve more than one network with a request
  1295. networks = utils.Filter(networks, func(net network.Inspect) bool {
  1296. // later in this function, the name is changed the to ID.
  1297. // this function is called during the rebuild stage of `compose watch`.
  1298. // we still require just one network back, but we need to run the search on the ID
  1299. return net.Name == n.Name || net.ID == n.Name
  1300. })
  1301. switch len(networks) {
  1302. case 1:
  1303. return networks[0].ID, nil
  1304. case 0:
  1305. enabled, err := s.isSWarmEnabled(ctx)
  1306. if err != nil {
  1307. return "", err
  1308. }
  1309. if enabled {
  1310. // Swarm nodes do not register overlay networks that were
  1311. // created on a different node unless they're in use.
  1312. // So we can't preemptively check network exists, but
  1313. // networkAttach will later fail anyway if network actually doesn't exist
  1314. return "swarm", nil
  1315. }
  1316. return "", fmt.Errorf("network %s declared as external, but could not be found", n.Name)
  1317. default:
  1318. return "", fmt.Errorf("multiple networks with name %q were found. Use network ID as `name` to avoid ambiguity", n.Name)
  1319. }
  1320. }
  1321. func (s *composeService) ensureVolume(ctx context.Context, name string, volume types.VolumeConfig, project *types.Project, assumeYes bool) (string, error) {
  1322. inspected, err := s.apiClient().VolumeInspect(ctx, volume.Name)
  1323. if err != nil {
  1324. if !errdefs.IsNotFound(err) {
  1325. return "", err
  1326. }
  1327. if volume.External {
  1328. return "", fmt.Errorf("external volume %q not found", volume.Name)
  1329. }
  1330. err = s.createVolume(ctx, volume)
  1331. return volume.Name, err
  1332. }
  1333. if volume.External {
  1334. return volume.Name, nil
  1335. }
  1336. // Volume exists with name, but let's double-check this is the expected one
  1337. p, ok := inspected.Labels[api.ProjectLabel]
  1338. if !ok {
  1339. logrus.Warnf("volume %q already exists but was not created by Docker Compose. Use `external: true` to use an existing volume", volume.Name)
  1340. }
  1341. if ok && p != project.Name {
  1342. logrus.Warnf("volume %q already exists but was created for project %q (expected %q). Use `external: true` to use an existing volume", volume.Name, p, project.Name)
  1343. }
  1344. expected, err := VolumeHash(volume)
  1345. if err != nil {
  1346. return "", err
  1347. }
  1348. actual, ok := inspected.Labels[api.ConfigHashLabel]
  1349. if ok && actual != expected {
  1350. confirm := assumeYes
  1351. if !assumeYes {
  1352. msg := fmt.Sprintf("Volume %q exists but doesn't match configuration in compose file. Recreate (data will be lost)?", volume.Name)
  1353. confirm, err = prompt.NewPrompt(s.stdin(), s.stdout()).Confirm(msg, false)
  1354. if err != nil {
  1355. return "", err
  1356. }
  1357. }
  1358. if confirm {
  1359. err = s.removeDivergedVolume(ctx, name, volume, project)
  1360. if err != nil {
  1361. return "", err
  1362. }
  1363. return volume.Name, s.createVolume(ctx, volume)
  1364. }
  1365. }
  1366. return inspected.Name, nil
  1367. }
  1368. func (s *composeService) removeDivergedVolume(ctx context.Context, name string, volume types.VolumeConfig, project *types.Project) error {
  1369. // Remove services mounting divergent volume
  1370. var services []string
  1371. for _, service := range project.Services.Filter(func(config types.ServiceConfig) bool {
  1372. for _, cfg := range config.Volumes {
  1373. if cfg.Source == name {
  1374. return true
  1375. }
  1376. }
  1377. return false
  1378. }) {
  1379. services = append(services, service.Name)
  1380. }
  1381. err := s.stop(ctx, project.Name, api.StopOptions{
  1382. Services: services,
  1383. Project: project,
  1384. })
  1385. if err != nil {
  1386. return err
  1387. }
  1388. containers, err := s.getContainers(ctx, project.Name, oneOffExclude, true, services...)
  1389. if err != nil {
  1390. return err
  1391. }
  1392. // FIXME (ndeloof) we have to remove container so we can recreate volume
  1393. // but doing so we can't inherit anonymous volumes from previous instance
  1394. err = s.remove(ctx, containers, api.RemoveOptions{
  1395. Services: services,
  1396. Project: project,
  1397. })
  1398. if err != nil {
  1399. return err
  1400. }
  1401. return s.apiClient().VolumeRemove(ctx, volume.Name, true)
  1402. }
  1403. func (s *composeService) createVolume(ctx context.Context, volume types.VolumeConfig) error {
  1404. eventName := fmt.Sprintf("Volume %q", volume.Name)
  1405. w := progress.ContextWriter(ctx)
  1406. w.Event(progress.CreatingEvent(eventName))
  1407. hash, err := VolumeHash(volume)
  1408. if err != nil {
  1409. return err
  1410. }
  1411. volume.CustomLabels.Add(api.ConfigHashLabel, hash)
  1412. _, err = s.apiClient().VolumeCreate(ctx, volumetypes.CreateOptions{
  1413. Labels: mergeLabels(volume.Labels, volume.CustomLabels),
  1414. Name: volume.Name,
  1415. Driver: volume.Driver,
  1416. DriverOpts: volume.DriverOpts,
  1417. })
  1418. if err != nil {
  1419. w.Event(progress.ErrorEvent(eventName))
  1420. return err
  1421. }
  1422. w.Event(progress.CreatedEvent(eventName))
  1423. return nil
  1424. }