create.go 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  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. //nolint:gocyclo
  758. func (s *composeService) buildContainerVolumes(
  759. ctx context.Context,
  760. p types.Project,
  761. service types.ServiceConfig,
  762. inherit *container.Summary,
  763. ) ([]string, []mount.Mount, error) {
  764. var mounts []mount.Mount
  765. var binds []string
  766. img := api.GetImageNameOrDefault(service, p.Name)
  767. imgInspect, err := s.apiClient().ImageInspect(ctx, img)
  768. if err != nil {
  769. return nil, nil, err
  770. }
  771. mountOptions, err := buildContainerMountOptions(p, service, imgInspect, inherit)
  772. if err != nil {
  773. return nil, nil, err
  774. }
  775. for _, m := range mountOptions {
  776. switch m.Type {
  777. case mount.TypeBind:
  778. // `Mount` is preferred but does not offer option to created host path if missing
  779. // so `Bind` API is used here with raw volume string
  780. // see https://github.com/moby/moby/issues/43483
  781. v := findVolumeByTarget(service.Volumes, m.Target)
  782. if v != nil {
  783. switch {
  784. case v.Type != types.VolumeTypeBind:
  785. v.Source = m.Source
  786. fallthrough
  787. case !requireMountAPI(v.Bind):
  788. source := m.Source
  789. if vol := findVolumeByName(p.Volumes, m.Source); vol != nil {
  790. source = m.Source
  791. }
  792. binds = append(binds, toBindString(source, v))
  793. continue
  794. }
  795. }
  796. case mount.TypeVolume:
  797. v := findVolumeByTarget(service.Volumes, m.Target)
  798. vol := findVolumeByName(p.Volumes, m.Source)
  799. if v != nil && vol != nil {
  800. if _, ok := vol.DriverOpts["device"]; ok && vol.Driver == "local" && vol.DriverOpts["o"] == "bind" {
  801. // Looks like a volume, but actually a bind mount which requires the bind API
  802. binds = append(binds, toBindString(vol.Name, v))
  803. continue
  804. }
  805. }
  806. case mount.TypeImage:
  807. version, err := s.RuntimeVersion(ctx)
  808. if err != nil {
  809. return nil, nil, err
  810. }
  811. if versions.LessThan(version, "1.48") {
  812. return nil, nil, fmt.Errorf("volume with type=image require Docker Engine v28 or later")
  813. }
  814. }
  815. mounts = append(mounts, m)
  816. }
  817. return binds, mounts, nil
  818. }
  819. func toBindString(name string, v *types.ServiceVolumeConfig) string {
  820. access := "rw"
  821. if v.ReadOnly {
  822. access = "ro"
  823. }
  824. options := []string{access}
  825. if v.Bind != nil && v.Bind.SELinux != "" {
  826. options = append(options, v.Bind.SELinux)
  827. }
  828. if v.Bind != nil && v.Bind.Propagation != "" {
  829. options = append(options, v.Bind.Propagation)
  830. }
  831. if v.Volume != nil && v.Volume.NoCopy {
  832. options = append(options, "nocopy")
  833. }
  834. return fmt.Sprintf("%s:%s:%s", name, v.Target, strings.Join(options, ","))
  835. }
  836. func findVolumeByName(volumes types.Volumes, name string) *types.VolumeConfig {
  837. for _, vol := range volumes {
  838. if vol.Name == name {
  839. return &vol
  840. }
  841. }
  842. return nil
  843. }
  844. func findVolumeByTarget(volumes []types.ServiceVolumeConfig, target string) *types.ServiceVolumeConfig {
  845. for _, v := range volumes {
  846. if v.Target == target {
  847. return &v
  848. }
  849. }
  850. return nil
  851. }
  852. // requireMountAPI check if Bind declaration can be implemented by the plain old Bind API or uses any of the advanced
  853. // options which require use of Mount API
  854. func requireMountAPI(bind *types.ServiceVolumeBind) bool {
  855. switch {
  856. case bind == nil:
  857. return false
  858. case !bind.CreateHostPath:
  859. return true
  860. case bind.Propagation != "":
  861. return true
  862. case bind.Recursive != "":
  863. return true
  864. default:
  865. return false
  866. }
  867. }
  868. func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img image.InspectResponse, inherit *container.Summary) ([]mount.Mount, error) {
  869. mounts := map[string]mount.Mount{}
  870. if inherit != nil {
  871. for _, m := range inherit.Mounts {
  872. if m.Type == "tmpfs" {
  873. continue
  874. }
  875. src := m.Source
  876. if m.Type == "volume" {
  877. src = m.Name
  878. }
  879. if img.Config != nil {
  880. if _, ok := img.Config.Volumes[m.Destination]; ok {
  881. // inherit previous container's anonymous volume
  882. mounts[m.Destination] = mount.Mount{
  883. Type: m.Type,
  884. Source: src,
  885. Target: m.Destination,
  886. ReadOnly: !m.RW,
  887. }
  888. }
  889. }
  890. volumes := []types.ServiceVolumeConfig{}
  891. for _, v := range s.Volumes {
  892. if v.Target != m.Destination || v.Source != "" {
  893. volumes = append(volumes, v)
  894. continue
  895. }
  896. // inherit previous container's anonymous volume
  897. mounts[m.Destination] = mount.Mount{
  898. Type: m.Type,
  899. Source: src,
  900. Target: m.Destination,
  901. ReadOnly: !m.RW,
  902. }
  903. }
  904. s.Volumes = volumes
  905. }
  906. }
  907. mounts, err := fillBindMounts(p, s, mounts)
  908. if err != nil {
  909. return nil, err
  910. }
  911. values := make([]mount.Mount, 0, len(mounts))
  912. for _, v := range mounts {
  913. values = append(values, v)
  914. }
  915. return values, nil
  916. }
  917. func fillBindMounts(p types.Project, s types.ServiceConfig, m map[string]mount.Mount) (map[string]mount.Mount, error) {
  918. for _, v := range s.Volumes {
  919. bindMount, err := buildMount(p, v)
  920. if err != nil {
  921. return nil, err
  922. }
  923. m[bindMount.Target] = bindMount
  924. }
  925. secrets, err := buildContainerSecretMounts(p, s)
  926. if err != nil {
  927. return nil, err
  928. }
  929. for _, s := range secrets {
  930. if _, found := m[s.Target]; found {
  931. continue
  932. }
  933. m[s.Target] = s
  934. }
  935. configs, err := buildContainerConfigMounts(p, s)
  936. if err != nil {
  937. return nil, err
  938. }
  939. for _, c := range configs {
  940. if _, found := m[c.Target]; found {
  941. continue
  942. }
  943. m[c.Target] = c
  944. }
  945. return m, nil
  946. }
  947. func buildContainerConfigMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
  948. mounts := map[string]mount.Mount{}
  949. configsBaseDir := "/"
  950. for _, config := range s.Configs {
  951. target := config.Target
  952. if config.Target == "" {
  953. target = configsBaseDir + config.Source
  954. } else if !isAbsTarget(config.Target) {
  955. target = configsBaseDir + config.Target
  956. }
  957. definedConfig := p.Configs[config.Source]
  958. if definedConfig.External {
  959. return nil, fmt.Errorf("unsupported external config %s", definedConfig.Name)
  960. }
  961. if definedConfig.Driver != "" {
  962. return nil, errors.New("Docker Compose does not support configs.*.driver") //nolint:staticcheck
  963. }
  964. if definedConfig.TemplateDriver != "" {
  965. return nil, errors.New("Docker Compose does not support configs.*.template_driver") //nolint:staticcheck
  966. }
  967. if definedConfig.Environment != "" || definedConfig.Content != "" {
  968. continue
  969. }
  970. if config.UID != "" || config.GID != "" || config.Mode != nil {
  971. logrus.Warn("config `uid`, `gid` and `mode` are not supported, they will be ignored")
  972. }
  973. bindMount, err := buildMount(p, types.ServiceVolumeConfig{
  974. Type: types.VolumeTypeBind,
  975. Source: definedConfig.File,
  976. Target: target,
  977. ReadOnly: true,
  978. })
  979. if err != nil {
  980. return nil, err
  981. }
  982. mounts[target] = bindMount
  983. }
  984. values := make([]mount.Mount, 0, len(mounts))
  985. for _, v := range mounts {
  986. values = append(values, v)
  987. }
  988. return values, nil
  989. }
  990. func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
  991. mounts := map[string]mount.Mount{}
  992. secretsDir := "/run/secrets/"
  993. for _, secret := range s.Secrets {
  994. target := secret.Target
  995. if secret.Target == "" {
  996. target = secretsDir + secret.Source
  997. } else if !isAbsTarget(secret.Target) {
  998. target = secretsDir + secret.Target
  999. }
  1000. definedSecret := p.Secrets[secret.Source]
  1001. if definedSecret.External {
  1002. return nil, fmt.Errorf("unsupported external secret %s", definedSecret.Name)
  1003. }
  1004. if definedSecret.Driver != "" {
  1005. return nil, errors.New("Docker Compose does not support secrets.*.driver") //nolint:staticcheck
  1006. }
  1007. if definedSecret.TemplateDriver != "" {
  1008. return nil, errors.New("Docker Compose does not support secrets.*.template_driver") //nolint:staticcheck
  1009. }
  1010. if definedSecret.Environment != "" {
  1011. continue
  1012. }
  1013. if secret.UID != "" || secret.GID != "" || secret.Mode != nil {
  1014. logrus.Warn("secrets `uid`, `gid` and `mode` are not supported, they will be ignored")
  1015. }
  1016. if _, err := os.Stat(definedSecret.File); os.IsNotExist(err) {
  1017. logrus.Warnf("secret file %s does not exist", definedSecret.Name)
  1018. }
  1019. mnt, err := buildMount(p, types.ServiceVolumeConfig{
  1020. Type: types.VolumeTypeBind,
  1021. Source: definedSecret.File,
  1022. Target: target,
  1023. ReadOnly: true,
  1024. Bind: &types.ServiceVolumeBind{
  1025. CreateHostPath: false,
  1026. },
  1027. })
  1028. if err != nil {
  1029. return nil, err
  1030. }
  1031. mounts[target] = mnt
  1032. }
  1033. values := make([]mount.Mount, 0, len(mounts))
  1034. for _, v := range mounts {
  1035. values = append(values, v)
  1036. }
  1037. return values, nil
  1038. }
  1039. func isAbsTarget(p string) bool {
  1040. return isUnixAbs(p) || isWindowsAbs(p)
  1041. }
  1042. func isUnixAbs(p string) bool {
  1043. return strings.HasPrefix(p, "/")
  1044. }
  1045. func isWindowsAbs(p string) bool {
  1046. if strings.HasPrefix(p, "\\\\") {
  1047. return true
  1048. }
  1049. if len(p) > 2 && p[1] == ':' {
  1050. return p[2] == '\\'
  1051. }
  1052. return false
  1053. }
  1054. func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {
  1055. source := volume.Source
  1056. // on windows, filepath.IsAbs(source) is false for unix style abs path like /var/run/docker.sock.
  1057. // do not replace these with filepath.Abs(source) that will include a default drive.
  1058. if volume.Type == types.VolumeTypeBind && !filepath.IsAbs(source) && !strings.HasPrefix(source, "/") {
  1059. // volume source has already been prefixed with workdir if required, by compose-go project loader
  1060. var err error
  1061. source, err = filepath.Abs(source)
  1062. if err != nil {
  1063. return mount.Mount{}, err
  1064. }
  1065. }
  1066. if volume.Type == types.VolumeTypeVolume {
  1067. if volume.Source != "" {
  1068. pVolume, ok := project.Volumes[volume.Source]
  1069. if ok {
  1070. source = pVolume.Name
  1071. }
  1072. }
  1073. }
  1074. bind, vol, tmpfs, img := buildMountOptions(volume)
  1075. if bind != nil {
  1076. volume.Type = types.VolumeTypeBind
  1077. }
  1078. return mount.Mount{
  1079. Type: mount.Type(volume.Type),
  1080. Source: source,
  1081. Target: volume.Target,
  1082. ReadOnly: volume.ReadOnly,
  1083. Consistency: mount.Consistency(volume.Consistency),
  1084. BindOptions: bind,
  1085. VolumeOptions: vol,
  1086. TmpfsOptions: tmpfs,
  1087. ImageOptions: img,
  1088. }, nil
  1089. }
  1090. func buildMountOptions(volume types.ServiceVolumeConfig) (*mount.BindOptions, *mount.VolumeOptions, *mount.TmpfsOptions, *mount.ImageOptions) {
  1091. if volume.Type != types.VolumeTypeBind && volume.Bind != nil {
  1092. logrus.Warnf("mount of type `%s` should not define `bind` option", volume.Type)
  1093. }
  1094. if volume.Type != types.VolumeTypeVolume && volume.Volume != nil {
  1095. logrus.Warnf("mount of type `%s` should not define `volume` option", volume.Type)
  1096. }
  1097. if volume.Type != types.VolumeTypeTmpfs && volume.Tmpfs != nil {
  1098. logrus.Warnf("mount of type `%s` should not define `tmpfs` option", volume.Type)
  1099. }
  1100. if volume.Type != types.VolumeTypeImage && volume.Image != nil {
  1101. logrus.Warnf("mount of type `%s` should not define `image` option", volume.Type)
  1102. }
  1103. switch volume.Type {
  1104. case "bind":
  1105. return buildBindOption(volume.Bind), nil, nil, nil
  1106. case "volume":
  1107. return nil, buildVolumeOptions(volume.Volume), nil, nil
  1108. case "tmpfs":
  1109. return nil, nil, buildTmpfsOptions(volume.Tmpfs), nil
  1110. case "image":
  1111. return nil, nil, nil, buildImageOptions(volume.Image)
  1112. }
  1113. return nil, nil, nil, nil
  1114. }
  1115. func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions {
  1116. if bind == nil {
  1117. return nil
  1118. }
  1119. opts := &mount.BindOptions{
  1120. Propagation: mount.Propagation(bind.Propagation),
  1121. CreateMountpoint: bind.CreateHostPath,
  1122. }
  1123. switch bind.Recursive {
  1124. case "disabled":
  1125. opts.NonRecursive = true
  1126. case "writable":
  1127. opts.ReadOnlyNonRecursive = true
  1128. case "readonly":
  1129. opts.ReadOnlyForceRecursive = true
  1130. }
  1131. return opts
  1132. }
  1133. func buildVolumeOptions(vol *types.ServiceVolumeVolume) *mount.VolumeOptions {
  1134. if vol == nil {
  1135. return nil
  1136. }
  1137. return &mount.VolumeOptions{
  1138. NoCopy: vol.NoCopy,
  1139. Subpath: vol.Subpath,
  1140. Labels: vol.Labels,
  1141. // DriverConfig: , // FIXME missing from model ?
  1142. }
  1143. }
  1144. func buildTmpfsOptions(tmpfs *types.ServiceVolumeTmpfs) *mount.TmpfsOptions {
  1145. if tmpfs == nil {
  1146. return nil
  1147. }
  1148. return &mount.TmpfsOptions{
  1149. SizeBytes: int64(tmpfs.Size),
  1150. Mode: os.FileMode(tmpfs.Mode),
  1151. }
  1152. }
  1153. func buildImageOptions(image *types.ServiceVolumeImage) *mount.ImageOptions {
  1154. if image == nil {
  1155. return nil
  1156. }
  1157. return &mount.ImageOptions{
  1158. Subpath: image.SubPath,
  1159. }
  1160. }
  1161. func (s *composeService) ensureNetwork(ctx context.Context, project *types.Project, name string, n *types.NetworkConfig) (string, error) {
  1162. if n.External {
  1163. return s.resolveExternalNetwork(ctx, n)
  1164. }
  1165. id, err := s.resolveOrCreateNetwork(ctx, project, name, n)
  1166. if errdefs.IsConflict(err) {
  1167. // Maybe another execution of `docker compose up|run` created same network
  1168. // let's retry once
  1169. return s.resolveOrCreateNetwork(ctx, project, name, n)
  1170. }
  1171. return id, err
  1172. }
  1173. func (s *composeService) resolveOrCreateNetwork(ctx context.Context, project *types.Project, name string, n *types.NetworkConfig) (string, error) { //nolint:gocyclo
  1174. // First, try to find a unique network matching by name or ID
  1175. inspect, err := s.apiClient().NetworkInspect(ctx, n.Name, network.InspectOptions{})
  1176. if err == nil {
  1177. // NetworkInspect will match on ID prefix, so double check we get the expected one
  1178. // as looking for network named `db` we could erroneously match network ID `db9086999caf`
  1179. if inspect.Name == n.Name || inspect.ID == n.Name {
  1180. p, ok := inspect.Labels[api.ProjectLabel]
  1181. if !ok {
  1182. logrus.Warnf("a network with name %s exists but was not created by compose.\n"+
  1183. "Set `external: true` to use an existing network", n.Name)
  1184. } else if p != project.Name {
  1185. logrus.Warnf("a network with name %s exists but was not created for project %q.\n"+
  1186. "Set `external: true` to use an existing network", n.Name, project.Name)
  1187. }
  1188. if inspect.Labels[api.NetworkLabel] != name {
  1189. return "", fmt.Errorf(
  1190. "network %s was found but has incorrect label %s set to %q (expected: %q)",
  1191. n.Name,
  1192. api.NetworkLabel,
  1193. inspect.Labels[api.NetworkLabel],
  1194. name,
  1195. )
  1196. }
  1197. hash := inspect.Labels[api.ConfigHashLabel]
  1198. expected, err := NetworkHash(n)
  1199. if err != nil {
  1200. return "", err
  1201. }
  1202. if hash == "" || hash == expected {
  1203. return inspect.ID, nil
  1204. }
  1205. err = s.removeDivergedNetwork(ctx, project, name, n)
  1206. if err != nil {
  1207. return "", err
  1208. }
  1209. }
  1210. }
  1211. // ignore other errors. Typically, an ambiguous request by name results in some generic `invalidParameter` error
  1212. // Either not found, or name is ambiguous - use NetworkList to list by name
  1213. networks, err := s.apiClient().NetworkList(ctx, network.ListOptions{
  1214. Filters: filters.NewArgs(filters.Arg("name", n.Name)),
  1215. })
  1216. if err != nil {
  1217. return "", err
  1218. }
  1219. // NetworkList Matches all or part of a network name, so we have to filter for a strict match
  1220. networks = utils.Filter(networks, func(net network.Summary) bool {
  1221. return net.Name == n.Name
  1222. })
  1223. for _, net := range networks {
  1224. if net.Labels[api.ProjectLabel] == project.Name &&
  1225. net.Labels[api.NetworkLabel] == name {
  1226. return net.ID, nil
  1227. }
  1228. }
  1229. // we could have set NetworkList with a projectFilter and networkFilter but not doing so allows to catch this
  1230. // scenario were a network with same name exists but doesn't have label, and use of `CheckDuplicate: true`
  1231. // prevents to create another one.
  1232. if len(networks) > 0 {
  1233. logrus.Warnf("a network with name %s exists but was not created by compose.\n"+
  1234. "Set `external: true` to use an existing network", n.Name)
  1235. return networks[0].ID, nil
  1236. }
  1237. var ipam *network.IPAM
  1238. if n.Ipam.Config != nil {
  1239. var config []network.IPAMConfig
  1240. for _, pool := range n.Ipam.Config {
  1241. config = append(config, network.IPAMConfig{
  1242. Subnet: pool.Subnet,
  1243. IPRange: pool.IPRange,
  1244. Gateway: pool.Gateway,
  1245. AuxAddress: pool.AuxiliaryAddresses,
  1246. })
  1247. }
  1248. ipam = &network.IPAM{
  1249. Driver: n.Ipam.Driver,
  1250. Config: config,
  1251. }
  1252. }
  1253. hash, err := NetworkHash(n)
  1254. if err != nil {
  1255. return "", err
  1256. }
  1257. n.CustomLabels = n.CustomLabels.Add(api.ConfigHashLabel, hash)
  1258. createOpts := network.CreateOptions{
  1259. Labels: mergeLabels(n.Labels, n.CustomLabels),
  1260. Driver: n.Driver,
  1261. Options: n.DriverOpts,
  1262. Internal: n.Internal,
  1263. Attachable: n.Attachable,
  1264. IPAM: ipam,
  1265. EnableIPv6: n.EnableIPv6,
  1266. EnableIPv4: n.EnableIPv4,
  1267. }
  1268. if n.Ipam.Driver != "" || len(n.Ipam.Config) > 0 {
  1269. createOpts.IPAM = &network.IPAM{}
  1270. }
  1271. if n.Ipam.Driver != "" {
  1272. createOpts.IPAM.Driver = n.Ipam.Driver
  1273. }
  1274. for _, ipamConfig := range n.Ipam.Config {
  1275. config := network.IPAMConfig{
  1276. Subnet: ipamConfig.Subnet,
  1277. IPRange: ipamConfig.IPRange,
  1278. Gateway: ipamConfig.Gateway,
  1279. AuxAddress: ipamConfig.AuxiliaryAddresses,
  1280. }
  1281. createOpts.IPAM.Config = append(createOpts.IPAM.Config, config)
  1282. }
  1283. networkEventName := fmt.Sprintf("Network %s", n.Name)
  1284. w := progress.ContextWriter(ctx)
  1285. w.Event(progress.CreatingEvent(networkEventName))
  1286. resp, err := s.apiClient().NetworkCreate(ctx, n.Name, createOpts)
  1287. if err != nil {
  1288. w.Event(progress.ErrorEvent(networkEventName))
  1289. return "", fmt.Errorf("failed to create network %s: %w", n.Name, err)
  1290. }
  1291. w.Event(progress.CreatedEvent(networkEventName))
  1292. return resp.ID, nil
  1293. }
  1294. func (s *composeService) removeDivergedNetwork(ctx context.Context, project *types.Project, name string, n *types.NetworkConfig) error {
  1295. // Remove services attached to this network to force recreation
  1296. var services []string
  1297. for _, service := range project.Services.Filter(func(config types.ServiceConfig) bool {
  1298. _, ok := config.Networks[name]
  1299. return ok
  1300. }) {
  1301. services = append(services, service.Name)
  1302. }
  1303. // Stop containers so we can remove network
  1304. // They will be restarted (actually: recreated) with the updated network
  1305. err := s.stop(ctx, project.Name, api.StopOptions{
  1306. Services: services,
  1307. Project: project,
  1308. })
  1309. if err != nil {
  1310. return err
  1311. }
  1312. err = s.apiClient().NetworkRemove(ctx, n.Name)
  1313. eventName := fmt.Sprintf("Network %s", n.Name)
  1314. progress.ContextWriter(ctx).Event(progress.RemovedEvent(eventName))
  1315. return err
  1316. }
  1317. func (s *composeService) resolveExternalNetwork(ctx context.Context, n *types.NetworkConfig) (string, error) {
  1318. // NetworkInspect will match on ID prefix, so NetworkList with a name
  1319. // filter is used to look for an exact match to prevent e.g. a network
  1320. // named `db` from getting erroneously matched to a network with an ID
  1321. // like `db9086999caf`
  1322. networks, err := s.apiClient().NetworkList(ctx, network.ListOptions{
  1323. Filters: filters.NewArgs(filters.Arg("name", n.Name)),
  1324. })
  1325. if err != nil {
  1326. return "", err
  1327. }
  1328. if len(networks) == 0 {
  1329. // in this instance, n.Name is really an ID
  1330. sn, err := s.apiClient().NetworkInspect(ctx, n.Name, network.InspectOptions{})
  1331. if err != nil && !errdefs.IsNotFound(err) {
  1332. return "", err
  1333. }
  1334. networks = append(networks, sn)
  1335. }
  1336. // NetworkList API doesn't return the exact name match, so we can retrieve more than one network with a request
  1337. networks = utils.Filter(networks, func(net network.Inspect) bool {
  1338. // later in this function, the name is changed the to ID.
  1339. // this function is called during the rebuild stage of `compose watch`.
  1340. // we still require just one network back, but we need to run the search on the ID
  1341. return net.Name == n.Name || net.ID == n.Name
  1342. })
  1343. switch len(networks) {
  1344. case 1:
  1345. return networks[0].ID, nil
  1346. case 0:
  1347. enabled, err := s.isSWarmEnabled(ctx)
  1348. if err != nil {
  1349. return "", err
  1350. }
  1351. if enabled {
  1352. // Swarm nodes do not register overlay networks that were
  1353. // created on a different node unless they're in use.
  1354. // So we can't preemptively check network exists, but
  1355. // networkAttach will later fail anyway if network actually doesn't exist
  1356. return "swarm", nil
  1357. }
  1358. return "", fmt.Errorf("network %s declared as external, but could not be found", n.Name)
  1359. default:
  1360. return "", fmt.Errorf("multiple networks with name %q were found. Use network ID as `name` to avoid ambiguity", n.Name)
  1361. }
  1362. }
  1363. func (s *composeService) ensureVolume(ctx context.Context, name string, volume types.VolumeConfig, project *types.Project, assumeYes bool) (string, error) {
  1364. inspected, err := s.apiClient().VolumeInspect(ctx, volume.Name)
  1365. if err != nil {
  1366. if !errdefs.IsNotFound(err) {
  1367. return "", err
  1368. }
  1369. if volume.External {
  1370. return "", fmt.Errorf("external volume %q not found", volume.Name)
  1371. }
  1372. err = s.createVolume(ctx, volume)
  1373. return volume.Name, err
  1374. }
  1375. if volume.External {
  1376. return volume.Name, nil
  1377. }
  1378. // Volume exists with name, but let's double-check this is the expected one
  1379. p, ok := inspected.Labels[api.ProjectLabel]
  1380. if !ok {
  1381. logrus.Warnf("volume %q already exists but was not created by Docker Compose. Use `external: true` to use an existing volume", volume.Name)
  1382. }
  1383. if ok && p != project.Name {
  1384. 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)
  1385. }
  1386. expected, err := VolumeHash(volume)
  1387. if err != nil {
  1388. return "", err
  1389. }
  1390. actual, ok := inspected.Labels[api.ConfigHashLabel]
  1391. if ok && actual != expected {
  1392. confirm := assumeYes
  1393. if !assumeYes {
  1394. msg := fmt.Sprintf("Volume %q exists but doesn't match configuration in compose file. Recreate (data will be lost)?", volume.Name)
  1395. confirm, err = prompt.NewPrompt(s.stdin(), s.stdout()).Confirm(msg, false)
  1396. if err != nil {
  1397. return "", err
  1398. }
  1399. }
  1400. if confirm {
  1401. err = s.removeDivergedVolume(ctx, name, volume, project)
  1402. if err != nil {
  1403. return "", err
  1404. }
  1405. return volume.Name, s.createVolume(ctx, volume)
  1406. }
  1407. }
  1408. return inspected.Name, nil
  1409. }
  1410. func (s *composeService) removeDivergedVolume(ctx context.Context, name string, volume types.VolumeConfig, project *types.Project) error {
  1411. // Remove services mounting divergent volume
  1412. var services []string
  1413. for _, service := range project.Services.Filter(func(config types.ServiceConfig) bool {
  1414. for _, cfg := range config.Volumes {
  1415. if cfg.Source == name {
  1416. return true
  1417. }
  1418. }
  1419. return false
  1420. }) {
  1421. services = append(services, service.Name)
  1422. }
  1423. err := s.stop(ctx, project.Name, api.StopOptions{
  1424. Services: services,
  1425. Project: project,
  1426. })
  1427. if err != nil {
  1428. return err
  1429. }
  1430. containers, err := s.getContainers(ctx, project.Name, oneOffExclude, true, services...)
  1431. if err != nil {
  1432. return err
  1433. }
  1434. // FIXME (ndeloof) we have to remove container so we can recreate volume
  1435. // but doing so we can't inherit anonymous volumes from previous instance
  1436. err = s.remove(ctx, containers, api.RemoveOptions{
  1437. Services: services,
  1438. Project: project,
  1439. })
  1440. if err != nil {
  1441. return err
  1442. }
  1443. return s.apiClient().VolumeRemove(ctx, volume.Name, true)
  1444. }
  1445. func (s *composeService) createVolume(ctx context.Context, volume types.VolumeConfig) error {
  1446. eventName := fmt.Sprintf("Volume %q", volume.Name)
  1447. w := progress.ContextWriter(ctx)
  1448. w.Event(progress.CreatingEvent(eventName))
  1449. hash, err := VolumeHash(volume)
  1450. if err != nil {
  1451. return err
  1452. }
  1453. volume.CustomLabels.Add(api.ConfigHashLabel, hash)
  1454. _, err = s.apiClient().VolumeCreate(ctx, volumetypes.CreateOptions{
  1455. Labels: mergeLabels(volume.Labels, volume.CustomLabels),
  1456. Name: volume.Name,
  1457. Driver: volume.Driver,
  1458. DriverOpts: volume.DriverOpts,
  1459. })
  1460. if err != nil {
  1461. w.Event(progress.ErrorEvent(eventName))
  1462. return err
  1463. }
  1464. w.Event(progress.CreatedEvent(eventName))
  1465. return nil
  1466. }