create.go 47 KB

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