create.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. "context"
  16. "fmt"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. "github.com/compose-spec/compose-go/types"
  21. moby "github.com/docker/docker/api/types"
  22. "github.com/docker/docker/api/types/blkiodev"
  23. "github.com/docker/docker/api/types/container"
  24. "github.com/docker/docker/api/types/mount"
  25. "github.com/docker/docker/api/types/network"
  26. "github.com/docker/docker/api/types/strslice"
  27. volume_api "github.com/docker/docker/api/types/volume"
  28. "github.com/docker/docker/errdefs"
  29. "github.com/docker/go-connections/nat"
  30. "github.com/docker/go-units"
  31. "github.com/pkg/errors"
  32. "github.com/sirupsen/logrus"
  33. "github.com/docker/compose-cli/api/compose"
  34. "github.com/docker/compose-cli/api/progress"
  35. "github.com/docker/compose-cli/internal"
  36. convert "github.com/docker/compose-cli/local/moby"
  37. "github.com/docker/compose-cli/utils"
  38. )
  39. func (s *composeService) Create(ctx context.Context, project *types.Project, options compose.CreateOptions) error {
  40. return progress.Run(ctx, func(ctx context.Context) error {
  41. return s.create(ctx, project, options)
  42. })
  43. }
  44. func (s *composeService) create(ctx context.Context, project *types.Project, options compose.CreateOptions) error {
  45. if len(options.Services) == 0 {
  46. options.Services = project.ServiceNames()
  47. }
  48. var observedState Containers
  49. observedState, err := s.getContainers(ctx, project.Name, oneOffInclude, true)
  50. if err != nil {
  51. return err
  52. }
  53. containerState := NewContainersState(observedState)
  54. ctx = context.WithValue(ctx, ContainersKey{}, containerState)
  55. err = s.ensureImagesExists(ctx, project, observedState, options.QuietPull)
  56. if err != nil {
  57. return err
  58. }
  59. prepareNetworks(project)
  60. err = prepareVolumes(project)
  61. if err != nil {
  62. return err
  63. }
  64. if err := s.ensureNetworks(ctx, project.Networks); err != nil {
  65. return err
  66. }
  67. if err := s.ensureProjectVolumes(ctx, project); err != nil {
  68. return err
  69. }
  70. allServices := project.AllServices()
  71. allServiceNames := []string{}
  72. for _, service := range allServices {
  73. allServiceNames = append(allServiceNames, service.Name)
  74. }
  75. orphans := observedState.filter(isNotService(allServiceNames...))
  76. if len(orphans) > 0 {
  77. if options.RemoveOrphans {
  78. w := progress.ContextWriter(ctx)
  79. err := s.removeContainers(ctx, w, orphans, nil)
  80. if err != nil {
  81. return err
  82. }
  83. } else {
  84. logrus.Warnf("Found orphan containers (%s) for this project. If "+
  85. "you removed or renamed this service in your compose "+
  86. "file, you can run this command with the "+
  87. "--remove-orphans flag to clean it up.", orphans.names())
  88. }
  89. }
  90. prepareServicesDependsOn(project)
  91. return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
  92. if utils.StringContains(options.Services, service.Name) {
  93. return s.ensureService(c, project, service, options.Recreate, options.Inherit, options.Timeout)
  94. }
  95. return s.ensureService(c, project, service, options.RecreateDependencies, options.Inherit, options.Timeout)
  96. })
  97. }
  98. func prepareVolumes(p *types.Project) error {
  99. for i := range p.Services {
  100. volumesFrom, dependServices, err := getVolumesFrom(p, p.Services[i].VolumesFrom)
  101. if err != nil {
  102. return err
  103. }
  104. p.Services[i].VolumesFrom = volumesFrom
  105. if len(dependServices) > 0 {
  106. if p.Services[i].DependsOn == nil {
  107. p.Services[i].DependsOn = make(types.DependsOnConfig, len(dependServices))
  108. }
  109. for _, service := range p.Services {
  110. if utils.StringContains(dependServices, service.Name) {
  111. p.Services[i].DependsOn[service.Name] = types.ServiceDependency{
  112. Condition: types.ServiceConditionStarted,
  113. }
  114. }
  115. }
  116. }
  117. }
  118. return nil
  119. }
  120. func prepareNetworks(project *types.Project) {
  121. for k, network := range project.Networks {
  122. network.Labels = network.Labels.Add(compose.NetworkLabel, k)
  123. network.Labels = network.Labels.Add(compose.ProjectLabel, project.Name)
  124. network.Labels = network.Labels.Add(compose.VersionLabel, internal.Version)
  125. project.Networks[k] = network
  126. }
  127. }
  128. func prepareServicesDependsOn(p *types.Project) {
  129. outLoop:
  130. for i := range p.Services {
  131. networkDependency := getDependentServiceFromMode(p.Services[i].NetworkMode)
  132. ipcDependency := getDependentServiceFromMode(p.Services[i].Ipc)
  133. pidDependency := getDependentServiceFromMode(p.Services[i].Pid)
  134. if networkDependency == "" && ipcDependency == "" && pidDependency == "" {
  135. continue
  136. }
  137. if p.Services[i].DependsOn == nil {
  138. p.Services[i].DependsOn = make(types.DependsOnConfig)
  139. }
  140. for _, service := range p.Services {
  141. if service.Name == networkDependency || service.Name == ipcDependency || service.Name == pidDependency {
  142. p.Services[i].DependsOn[service.Name] = types.ServiceDependency{
  143. Condition: types.ServiceConditionStarted,
  144. }
  145. continue outLoop
  146. }
  147. }
  148. }
  149. }
  150. func (s *composeService) ensureNetworks(ctx context.Context, networks types.Networks) error {
  151. for _, network := range networks {
  152. err := s.ensureNetwork(ctx, network)
  153. if err != nil {
  154. return err
  155. }
  156. }
  157. return nil
  158. }
  159. func (s *composeService) ensureProjectVolumes(ctx context.Context, project *types.Project) error {
  160. for k, volume := range project.Volumes {
  161. volume.Labels = volume.Labels.Add(compose.VolumeLabel, k)
  162. volume.Labels = volume.Labels.Add(compose.ProjectLabel, project.Name)
  163. volume.Labels = volume.Labels.Add(compose.VersionLabel, internal.Version)
  164. err := s.ensureVolume(ctx, volume)
  165. if err != nil {
  166. return err
  167. }
  168. }
  169. return nil
  170. }
  171. func getImageName(service types.ServiceConfig, projectName string) string {
  172. imageName := service.Image
  173. if imageName == "" {
  174. imageName = projectName + "_" + service.Name
  175. }
  176. return imageName
  177. }
  178. func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project, service types.ServiceConfig, number int, inherit *moby.Container,
  179. autoRemove bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
  180. hash, err := utils.ServiceHash(service)
  181. if err != nil {
  182. return nil, nil, nil, err
  183. }
  184. labels := map[string]string{}
  185. for k, v := range service.Labels {
  186. labels[k] = v
  187. }
  188. labels[compose.ProjectLabel] = p.Name
  189. labels[compose.ServiceLabel] = service.Name
  190. labels[compose.VersionLabel] = internal.Version
  191. if _, ok := service.Labels[compose.OneoffLabel]; !ok {
  192. labels[compose.OneoffLabel] = "False"
  193. }
  194. labels[compose.ConfigHashLabel] = hash
  195. labels[compose.WorkingDirLabel] = p.WorkingDir
  196. labels[compose.ConfigFilesLabel] = strings.Join(p.ComposeFiles, ",")
  197. labels[compose.ContainerNumberLabel] = strconv.Itoa(number)
  198. var (
  199. runCmd strslice.StrSlice
  200. entrypoint strslice.StrSlice
  201. )
  202. if len(service.Command) > 0 {
  203. runCmd = strslice.StrSlice(service.Command)
  204. }
  205. if len(service.Entrypoint) > 0 {
  206. entrypoint = strslice.StrSlice(service.Entrypoint)
  207. }
  208. var (
  209. tty = service.Tty
  210. stdinOpen = service.StdinOpen
  211. attachStdin = false
  212. )
  213. volumeMounts, binds, mounts, err := s.buildContainerVolumes(ctx, *p, service, inherit)
  214. if err != nil {
  215. return nil, nil, nil, err
  216. }
  217. containerConfig := container.Config{
  218. Hostname: service.Hostname,
  219. Domainname: service.DomainName,
  220. User: service.User,
  221. ExposedPorts: buildContainerPorts(service),
  222. Tty: tty,
  223. OpenStdin: stdinOpen,
  224. StdinOnce: attachStdin && stdinOpen,
  225. AttachStdin: attachStdin,
  226. AttachStderr: true,
  227. AttachStdout: true,
  228. Cmd: runCmd,
  229. Image: getImageName(service, p.Name),
  230. WorkingDir: service.WorkingDir,
  231. Entrypoint: entrypoint,
  232. NetworkDisabled: service.NetworkMode == "disabled",
  233. MacAddress: service.MacAddress,
  234. Labels: labels,
  235. StopSignal: service.StopSignal,
  236. Env: convert.ToMobyEnv(service.Environment),
  237. Healthcheck: convert.ToMobyHealthCheck(service.HealthCheck),
  238. Volumes: volumeMounts,
  239. StopTimeout: convert.ToSeconds(service.StopGracePeriod),
  240. }
  241. portBindings := buildContainerPortBindingOptions(service)
  242. resources := getDeployResources(service)
  243. networkMode, err := getMode(ctx, service.Name, service.NetworkMode)
  244. if err != nil {
  245. return nil, nil, nil, err
  246. }
  247. if networkMode == "" {
  248. networkMode = getDefaultNetworkMode(p, service)
  249. }
  250. var networkConfig *network.NetworkingConfig
  251. for _, id := range service.NetworksByPriority() {
  252. net := p.Networks[id]
  253. config := service.Networks[id]
  254. networkConfig = &network.NetworkingConfig{
  255. EndpointsConfig: map[string]*network.EndpointSettings{
  256. net.Name: {
  257. Aliases: getAliases(service, config),
  258. },
  259. },
  260. }
  261. break
  262. }
  263. ipcmode, err := getMode(ctx, service.Name, service.Ipc)
  264. if err != nil {
  265. return nil, nil, nil, err
  266. }
  267. tmpfs := map[string]string{}
  268. for _, t := range service.Tmpfs {
  269. if arr := strings.SplitN(t, ":", 2); len(arr) > 1 {
  270. tmpfs[arr[0]] = arr[1]
  271. } else {
  272. tmpfs[arr[0]] = ""
  273. }
  274. }
  275. var logConfig container.LogConfig
  276. if service.Logging != nil {
  277. logConfig = container.LogConfig{
  278. Type: service.Logging.Driver,
  279. Config: service.Logging.Options,
  280. }
  281. }
  282. hostConfig := container.HostConfig{
  283. AutoRemove: autoRemove,
  284. Binds: binds,
  285. Mounts: mounts,
  286. CapAdd: strslice.StrSlice(service.CapAdd),
  287. CapDrop: strslice.StrSlice(service.CapDrop),
  288. NetworkMode: container.NetworkMode(networkMode),
  289. Init: service.Init,
  290. IpcMode: container.IpcMode(ipcmode),
  291. ReadonlyRootfs: service.ReadOnly,
  292. RestartPolicy: getRestartPolicy(service),
  293. ShmSize: int64(service.ShmSize),
  294. Sysctls: service.Sysctls,
  295. PortBindings: portBindings,
  296. Resources: resources,
  297. VolumeDriver: service.VolumeDriver,
  298. VolumesFrom: service.VolumesFrom,
  299. DNS: service.DNS,
  300. DNSSearch: service.DNSSearch,
  301. DNSOptions: service.DNSOpts,
  302. ExtraHosts: service.ExtraHosts,
  303. SecurityOpt: service.SecurityOpt,
  304. UsernsMode: container.UsernsMode(service.UserNSMode),
  305. Privileged: service.Privileged,
  306. PidMode: container.PidMode(service.Pid),
  307. Tmpfs: tmpfs,
  308. Isolation: container.Isolation(service.Isolation),
  309. LogConfig: logConfig,
  310. }
  311. return &containerConfig, &hostConfig, networkConfig, nil
  312. }
  313. func getDefaultNetworkMode(project *types.Project, service types.ServiceConfig) string {
  314. mode := "none"
  315. if len(project.Networks) > 0 {
  316. for name := range getNetworksForService(service) {
  317. mode = project.Networks[name].Name
  318. break
  319. }
  320. }
  321. return mode
  322. }
  323. func getRestartPolicy(service types.ServiceConfig) container.RestartPolicy {
  324. var restart container.RestartPolicy
  325. if service.Restart != "" {
  326. split := strings.Split(service.Restart, ":")
  327. var attempts int
  328. if len(split) > 1 {
  329. attempts, _ = strconv.Atoi(split[1])
  330. }
  331. restart = container.RestartPolicy{
  332. Name: split[0],
  333. MaximumRetryCount: attempts,
  334. }
  335. }
  336. if service.Deploy != nil && service.Deploy.RestartPolicy != nil {
  337. policy := *service.Deploy.RestartPolicy
  338. var attempts int
  339. if policy.MaxAttempts != nil {
  340. attempts = int(*policy.MaxAttempts)
  341. }
  342. restart = container.RestartPolicy{
  343. Name: policy.Condition,
  344. MaximumRetryCount: attempts,
  345. }
  346. }
  347. return restart
  348. }
  349. func getDeployResources(s types.ServiceConfig) container.Resources {
  350. var swappiness *int64
  351. if s.MemSwappiness != 0 {
  352. val := int64(s.MemSwappiness)
  353. swappiness = &val
  354. }
  355. resources := container.Resources{
  356. CgroupParent: s.CgroupParent,
  357. Memory: int64(s.MemLimit),
  358. MemorySwap: int64(s.MemSwapLimit),
  359. MemorySwappiness: swappiness,
  360. MemoryReservation: int64(s.MemReservation),
  361. CPUCount: s.CPUCount,
  362. CPUPeriod: s.CPUPeriod,
  363. CPUQuota: s.CPUQuota,
  364. CPURealtimePeriod: s.CPURTPeriod,
  365. CPURealtimeRuntime: s.CPURTRuntime,
  366. CPUShares: s.CPUShares,
  367. CPUPercent: int64(s.CPUS * 100),
  368. CpusetCpus: s.CPUSet,
  369. }
  370. setBlkio(s.BlkioConfig, &resources)
  371. if s.Deploy != nil {
  372. setLimits(s.Deploy.Resources.Limits, &resources)
  373. setReservations(s.Deploy.Resources.Reservations, &resources)
  374. }
  375. for _, device := range s.Devices {
  376. // FIXME should use docker/cli parseDevice, unfortunately private
  377. src := ""
  378. dst := ""
  379. permissions := "rwm"
  380. arr := strings.Split(device, ":")
  381. switch len(arr) {
  382. case 3:
  383. permissions = arr[2]
  384. fallthrough
  385. case 2:
  386. dst = arr[1]
  387. fallthrough
  388. case 1:
  389. src = arr[0]
  390. }
  391. resources.Devices = append(resources.Devices, container.DeviceMapping{
  392. PathOnHost: src,
  393. PathInContainer: dst,
  394. CgroupPermissions: permissions,
  395. })
  396. }
  397. for name, u := range s.Ulimits {
  398. soft := u.Single
  399. if u.Soft != 0 {
  400. soft = u.Soft
  401. }
  402. hard := u.Single
  403. if u.Hard != 0 {
  404. hard = u.Hard
  405. }
  406. resources.Ulimits = append(resources.Ulimits, &units.Ulimit{
  407. Name: name,
  408. Hard: int64(hard),
  409. Soft: int64(soft),
  410. })
  411. }
  412. return resources
  413. }
  414. func setReservations(reservations *types.Resource, resources *container.Resources) {
  415. if reservations == nil {
  416. return
  417. }
  418. for _, device := range reservations.Devices {
  419. resources.DeviceRequests = append(resources.DeviceRequests, container.DeviceRequest{
  420. Capabilities: [][]string{device.Capabilities},
  421. Count: int(device.Count),
  422. DeviceIDs: device.IDs,
  423. Driver: device.Driver,
  424. })
  425. }
  426. }
  427. func setLimits(limits *types.Resource, resources *container.Resources) {
  428. if limits == nil {
  429. return
  430. }
  431. if limits.MemoryBytes != 0 {
  432. resources.Memory = int64(limits.MemoryBytes)
  433. }
  434. if limits.NanoCPUs != "" {
  435. i, _ := strconv.ParseInt(limits.NanoCPUs, 10, 64)
  436. resources.NanoCPUs = i
  437. }
  438. }
  439. func setBlkio(blkio *types.BlkioConfig, resources *container.Resources) {
  440. if blkio == nil {
  441. return
  442. }
  443. resources.BlkioWeight = blkio.Weight
  444. for _, b := range blkio.WeightDevice {
  445. resources.BlkioWeightDevice = append(resources.BlkioWeightDevice, &blkiodev.WeightDevice{
  446. Path: b.Path,
  447. Weight: b.Weight,
  448. })
  449. }
  450. for _, b := range blkio.DeviceReadBps {
  451. resources.BlkioDeviceReadBps = append(resources.BlkioDeviceReadBps, &blkiodev.ThrottleDevice{
  452. Path: b.Path,
  453. Rate: b.Rate,
  454. })
  455. }
  456. for _, b := range blkio.DeviceReadIOps {
  457. resources.BlkioDeviceReadIOps = append(resources.BlkioDeviceReadIOps, &blkiodev.ThrottleDevice{
  458. Path: b.Path,
  459. Rate: b.Rate,
  460. })
  461. }
  462. for _, b := range blkio.DeviceWriteBps {
  463. resources.BlkioDeviceWriteBps = append(resources.BlkioDeviceWriteBps, &blkiodev.ThrottleDevice{
  464. Path: b.Path,
  465. Rate: b.Rate,
  466. })
  467. }
  468. for _, b := range blkio.DeviceWriteIOps {
  469. resources.BlkioDeviceWriteIOps = append(resources.BlkioDeviceWriteIOps, &blkiodev.ThrottleDevice{
  470. Path: b.Path,
  471. Rate: b.Rate,
  472. })
  473. }
  474. }
  475. func buildContainerPorts(s types.ServiceConfig) nat.PortSet {
  476. ports := nat.PortSet{}
  477. for _, p := range s.Ports {
  478. p := nat.Port(fmt.Sprintf("%d/%s", p.Target, p.Protocol))
  479. ports[p] = struct{}{}
  480. }
  481. return ports
  482. }
  483. func buildContainerPortBindingOptions(s types.ServiceConfig) nat.PortMap {
  484. bindings := nat.PortMap{}
  485. for _, port := range s.Ports {
  486. p := nat.Port(fmt.Sprintf("%d/%s", port.Target, port.Protocol))
  487. bind := bindings[p]
  488. binding := nat.PortBinding{
  489. HostIP: port.HostIP,
  490. }
  491. if port.Published > 0 {
  492. binding.HostPort = fmt.Sprint(port.Published)
  493. }
  494. bind = append(bind, binding)
  495. bindings[p] = bind
  496. }
  497. return bindings
  498. }
  499. func getVolumesFrom(project *types.Project, volumesFrom []string) ([]string, []string, error) {
  500. var volumes = []string{}
  501. var services = []string{}
  502. // parse volumes_from
  503. if len(volumesFrom) == 0 {
  504. return volumes, services, nil
  505. }
  506. for _, vol := range volumesFrom {
  507. spec := strings.Split(vol, ":")
  508. if len(spec) == 0 {
  509. continue
  510. }
  511. if spec[0] == "container" {
  512. volumes = append(volumes, strings.Join(spec[1:], ":"))
  513. continue
  514. }
  515. serviceName := spec[0]
  516. services = append(services, serviceName)
  517. service, err := project.GetService(serviceName)
  518. if err != nil {
  519. return nil, nil, err
  520. }
  521. firstContainer := getContainerName(project.Name, service, 1)
  522. v := fmt.Sprintf("%s:%s", firstContainer, strings.Join(spec[1:], ":"))
  523. volumes = append(volumes, v)
  524. }
  525. return volumes, services, nil
  526. }
  527. func getDependentServiceFromMode(mode string) string {
  528. if strings.HasPrefix(mode, types.NetworkModeServicePrefix) {
  529. return mode[len(types.NetworkModeServicePrefix):]
  530. }
  531. return ""
  532. }
  533. func (s *composeService) buildContainerVolumes(ctx context.Context, p types.Project, service types.ServiceConfig,
  534. inherit *moby.Container) (map[string]struct{}, []string, []mount.Mount, error) {
  535. var mounts = []mount.Mount{}
  536. image := getImageName(service, p.Name)
  537. imgInspect, _, err := s.apiClient.ImageInspectWithRaw(ctx, image)
  538. if err != nil {
  539. return nil, nil, nil, err
  540. }
  541. mountOptions, err := buildContainerMountOptions(p, service, imgInspect, inherit)
  542. if err != nil {
  543. return nil, nil, nil, err
  544. }
  545. volumeMounts := map[string]struct{}{}
  546. binds := []string{}
  547. MOUNTS:
  548. for _, m := range mountOptions {
  549. volumeMounts[m.Target] = struct{}{}
  550. // `Bind` API is used when host path need to be created if missing, `Mount` is preferred otherwise
  551. if m.Type == mount.TypeBind || m.Type == mount.TypeNamedPipe {
  552. for _, v := range service.Volumes {
  553. if v.Target == m.Target && v.Bind != nil && v.Bind.CreateHostPath {
  554. mode := "rw"
  555. if m.ReadOnly {
  556. mode = "ro"
  557. }
  558. binds = append(binds, fmt.Sprintf("%s:%s:%s", m.Source, m.Target, mode))
  559. continue MOUNTS
  560. }
  561. }
  562. }
  563. mounts = append(mounts, m)
  564. }
  565. return volumeMounts, binds, mounts, nil
  566. }
  567. func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
  568. var mounts = map[string]mount.Mount{}
  569. if inherit != nil {
  570. for _, m := range inherit.Mounts {
  571. if m.Type == "tmpfs" {
  572. continue
  573. }
  574. src := m.Source
  575. if m.Type == "volume" {
  576. src = m.Name
  577. }
  578. mounts[m.Destination] = mount.Mount{
  579. Type: m.Type,
  580. Source: src,
  581. Target: m.Destination,
  582. ReadOnly: !m.RW,
  583. }
  584. }
  585. }
  586. if img.ContainerConfig != nil {
  587. for k := range img.ContainerConfig.Volumes {
  588. m, err := buildMount(p, types.ServiceVolumeConfig{
  589. Type: types.VolumeTypeVolume,
  590. Target: k,
  591. })
  592. if err != nil {
  593. return nil, err
  594. }
  595. mounts[k] = m
  596. }
  597. }
  598. mounts, err := fillBindMounts(p, s, mounts)
  599. if err != nil {
  600. return nil, err
  601. }
  602. values := make([]mount.Mount, 0, len(mounts))
  603. for _, v := range mounts {
  604. values = append(values, v)
  605. }
  606. return values, nil
  607. }
  608. func fillBindMounts(p types.Project, s types.ServiceConfig, m map[string]mount.Mount) (map[string]mount.Mount, error) {
  609. for _, v := range s.Volumes {
  610. bindMount, err := buildMount(p, v)
  611. if err != nil {
  612. return nil, err
  613. }
  614. m[bindMount.Target] = bindMount
  615. }
  616. secrets, err := buildContainerSecretMounts(p, s)
  617. if err != nil {
  618. return nil, err
  619. }
  620. for _, s := range secrets {
  621. if _, found := m[s.Target]; found {
  622. continue
  623. }
  624. m[s.Target] = s
  625. }
  626. configs, err := buildContainerConfigMounts(p, s)
  627. if err != nil {
  628. return nil, err
  629. }
  630. for _, c := range configs {
  631. if _, found := m[c.Target]; found {
  632. continue
  633. }
  634. m[c.Target] = c
  635. }
  636. return m, nil
  637. }
  638. func buildContainerConfigMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
  639. var mounts = map[string]mount.Mount{}
  640. configsBaseDir := "/"
  641. for _, config := range s.Configs {
  642. target := config.Target
  643. if config.Target == "" {
  644. target = configsBaseDir + config.Source
  645. } else if !isUnixAbs(config.Target) {
  646. target = configsBaseDir + config.Target
  647. }
  648. definedConfig := p.Configs[config.Source]
  649. if definedConfig.External.External {
  650. return nil, fmt.Errorf("unsupported external config %s", definedConfig.Name)
  651. }
  652. bindMount, err := buildMount(p, types.ServiceVolumeConfig{
  653. Type: types.VolumeTypeBind,
  654. Source: definedConfig.File,
  655. Target: target,
  656. ReadOnly: true,
  657. })
  658. if err != nil {
  659. return nil, err
  660. }
  661. mounts[target] = bindMount
  662. }
  663. values := make([]mount.Mount, 0, len(mounts))
  664. for _, v := range mounts {
  665. values = append(values, v)
  666. }
  667. return values, nil
  668. }
  669. func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
  670. var mounts = map[string]mount.Mount{}
  671. secretsDir := "/run/secrets/"
  672. for _, secret := range s.Secrets {
  673. target := secret.Target
  674. if secret.Target == "" {
  675. target = secretsDir + secret.Source
  676. } else if !isUnixAbs(secret.Target) {
  677. target = secretsDir + secret.Target
  678. }
  679. definedSecret := p.Secrets[secret.Source]
  680. if definedSecret.External.External {
  681. return nil, fmt.Errorf("unsupported external secret %s", definedSecret.Name)
  682. }
  683. mount, err := buildMount(p, types.ServiceVolumeConfig{
  684. Type: types.VolumeTypeBind,
  685. Source: definedSecret.File,
  686. Target: target,
  687. ReadOnly: true,
  688. })
  689. if err != nil {
  690. return nil, err
  691. }
  692. mounts[target] = mount
  693. }
  694. values := make([]mount.Mount, 0, len(mounts))
  695. for _, v := range mounts {
  696. values = append(values, v)
  697. }
  698. return values, nil
  699. }
  700. func isUnixAbs(path string) bool {
  701. return strings.HasPrefix(path, "/")
  702. }
  703. func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {
  704. source := volume.Source
  705. // on windows, filepath.IsAbs(source) is false for unix style abs path like /var/run/docker.sock.
  706. // do not replace these with filepath.Abs(source) that will include a default drive.
  707. if volume.Type == types.VolumeTypeBind && !filepath.IsAbs(source) && !strings.HasPrefix(source, "/") {
  708. // volume source has already been prefixed with workdir if required, by compose-go project loader
  709. var err error
  710. source, err = filepath.Abs(source)
  711. if err != nil {
  712. return mount.Mount{}, err
  713. }
  714. }
  715. if volume.Type == types.VolumeTypeVolume {
  716. if volume.Source != "" {
  717. pVolume, ok := project.Volumes[volume.Source]
  718. if ok {
  719. source = pVolume.Name
  720. }
  721. }
  722. }
  723. bind, vol, tmpfs := buildMountOptions(volume)
  724. return mount.Mount{
  725. Type: mount.Type(volume.Type),
  726. Source: source,
  727. Target: volume.Target,
  728. ReadOnly: volume.ReadOnly,
  729. Consistency: mount.Consistency(volume.Consistency),
  730. BindOptions: bind,
  731. VolumeOptions: vol,
  732. TmpfsOptions: tmpfs,
  733. }, nil
  734. }
  735. func buildMountOptions(volume types.ServiceVolumeConfig) (*mount.BindOptions, *mount.VolumeOptions, *mount.TmpfsOptions) {
  736. switch volume.Type {
  737. case "bind":
  738. if volume.Volume != nil {
  739. logrus.Warnf("mount of type `bind` should not define `volume` option")
  740. }
  741. if volume.Tmpfs != nil {
  742. logrus.Warnf("mount of type `tmpfs` should not define `tmpfs` option")
  743. }
  744. return buildBindOption(volume.Bind), nil, nil
  745. case "volume":
  746. if volume.Bind != nil {
  747. logrus.Warnf("mount of type `volume` should not define `bind` option")
  748. }
  749. if volume.Tmpfs != nil {
  750. logrus.Warnf("mount of type `volume` should not define `tmpfs` option")
  751. }
  752. return nil, buildVolumeOptions(volume.Volume), nil
  753. case "tmpfs":
  754. if volume.Bind != nil {
  755. logrus.Warnf("mount of type `tmpfs` should not define `bind` option")
  756. }
  757. if volume.Tmpfs != nil {
  758. logrus.Warnf("mount of type `tmpfs` should not define `volumeZ` option")
  759. }
  760. return nil, nil, buildTmpfsOptions(volume.Tmpfs)
  761. }
  762. return nil, nil, nil
  763. }
  764. func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions {
  765. if bind == nil {
  766. return nil
  767. }
  768. return &mount.BindOptions{
  769. Propagation: mount.Propagation(bind.Propagation),
  770. // NonRecursive: false, FIXME missing from model ?
  771. }
  772. }
  773. func buildVolumeOptions(vol *types.ServiceVolumeVolume) *mount.VolumeOptions {
  774. if vol == nil {
  775. return nil
  776. }
  777. return &mount.VolumeOptions{
  778. NoCopy: vol.NoCopy,
  779. // Labels: , // FIXME missing from model ?
  780. // DriverConfig: , // FIXME missing from model ?
  781. }
  782. }
  783. func buildTmpfsOptions(tmpfs *types.ServiceVolumeTmpfs) *mount.TmpfsOptions {
  784. if tmpfs == nil {
  785. return nil
  786. }
  787. return &mount.TmpfsOptions{
  788. SizeBytes: tmpfs.Size,
  789. // Mode: , // FIXME missing from model ?
  790. }
  791. }
  792. func getAliases(s types.ServiceConfig, c *types.ServiceNetworkConfig) []string {
  793. aliases := []string{s.Name}
  794. if c != nil {
  795. aliases = append(aliases, c.Aliases...)
  796. }
  797. return aliases
  798. }
  799. func getMode(ctx context.Context, serviceName string, mode string) (string, error) {
  800. cState, err := GetContextContainerState(ctx)
  801. if err != nil {
  802. return "", nil
  803. }
  804. observedState := cState.GetContainers()
  805. depService := getDependentServiceFromMode(mode)
  806. if depService != "" {
  807. depServiceContainers := observedState.filter(isService(depService))
  808. if len(depServiceContainers) > 0 {
  809. return types.NetworkModeContainerPrefix + depServiceContainers[0].ID, nil
  810. }
  811. return "", fmt.Errorf(`no containers started for %q in service %q -> %v`,
  812. mode, serviceName, observedState)
  813. }
  814. return mode, nil
  815. }
  816. func getNetworksForService(s types.ServiceConfig) map[string]*types.ServiceNetworkConfig {
  817. if len(s.Networks) > 0 {
  818. return s.Networks
  819. }
  820. if s.NetworkMode != "" {
  821. return nil
  822. }
  823. return map[string]*types.ServiceNetworkConfig{"default": nil}
  824. }
  825. func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfig) error {
  826. _, err := s.apiClient.NetworkInspect(ctx, n.Name, moby.NetworkInspectOptions{})
  827. if err != nil {
  828. if errdefs.IsNotFound(err) {
  829. if n.External.External {
  830. return fmt.Errorf("network %s declared as external, but could not be found", n.Name)
  831. }
  832. createOpts := moby.NetworkCreate{
  833. // TODO NameSpace Labels
  834. Labels: n.Labels,
  835. Driver: n.Driver,
  836. Options: n.DriverOpts,
  837. Internal: n.Internal,
  838. Attachable: n.Attachable,
  839. }
  840. if n.Ipam.Driver != "" || len(n.Ipam.Config) > 0 {
  841. createOpts.IPAM = &network.IPAM{}
  842. }
  843. if n.Ipam.Driver != "" {
  844. createOpts.IPAM.Driver = n.Ipam.Driver
  845. }
  846. for _, ipamConfig := range n.Ipam.Config {
  847. config := network.IPAMConfig{
  848. Subnet: ipamConfig.Subnet,
  849. }
  850. createOpts.IPAM.Config = append(createOpts.IPAM.Config, config)
  851. }
  852. networkEventName := fmt.Sprintf("Network %s", n.Name)
  853. w := progress.ContextWriter(ctx)
  854. w.Event(progress.CreatingEvent(networkEventName))
  855. if _, err := s.apiClient.NetworkCreate(ctx, n.Name, createOpts); err != nil {
  856. w.Event(progress.ErrorEvent(networkEventName))
  857. return errors.Wrapf(err, "failed to create network %s", n.Name)
  858. }
  859. w.Event(progress.CreatedEvent(networkEventName))
  860. return nil
  861. }
  862. return err
  863. }
  864. return nil
  865. }
  866. func (s *composeService) removeNetwork(ctx context.Context, networkID string, networkName string) error {
  867. w := progress.ContextWriter(ctx)
  868. eventName := fmt.Sprintf("Network %s", networkName)
  869. w.Event(progress.RemovingEvent(eventName))
  870. if err := s.apiClient.NetworkRemove(ctx, networkID); err != nil {
  871. w.Event(progress.ErrorEvent(eventName))
  872. return errors.Wrapf(err, fmt.Sprintf("failed to remove network %s", networkID))
  873. }
  874. w.Event(progress.RemovedEvent(eventName))
  875. return nil
  876. }
  877. func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeConfig) error {
  878. // TODO could identify volume by label vs name
  879. _, err := s.apiClient.VolumeInspect(ctx, volume.Name)
  880. if err != nil {
  881. if !errdefs.IsNotFound(err) {
  882. return err
  883. }
  884. eventName := fmt.Sprintf("Volume %q", volume.Name)
  885. w := progress.ContextWriter(ctx)
  886. w.Event(progress.CreatingEvent(eventName))
  887. _, err := s.apiClient.VolumeCreate(ctx, volume_api.VolumeCreateBody{
  888. Labels: volume.Labels,
  889. Name: volume.Name,
  890. Driver: volume.Driver,
  891. DriverOpts: volume.DriverOpts,
  892. })
  893. if err != nil {
  894. w.Event(progress.ErrorEvent(eventName))
  895. return err
  896. }
  897. w.Event(progress.CreatedEvent(eventName))
  898. }
  899. return nil
  900. }