create.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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/container"
  23. "github.com/docker/docker/api/types/filters"
  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/pkg/errors"
  31. "github.com/sirupsen/logrus"
  32. "golang.org/x/sync/errgroup"
  33. "github.com/docker/compose-cli/api/compose"
  34. "github.com/docker/compose-cli/api/progress"
  35. convert "github.com/docker/compose-cli/local/moby"
  36. )
  37. func (s *composeService) Create(ctx context.Context, project *types.Project, opts compose.CreateOptions) error {
  38. err := s.ensureImagesExists(ctx, project)
  39. if err != nil {
  40. return err
  41. }
  42. prepareNetworks(project)
  43. err = prepareVolumes(project)
  44. if err != nil {
  45. return err
  46. }
  47. if err := s.ensureNetworks(ctx, project.Networks); err != nil {
  48. return err
  49. }
  50. if err := s.ensureProjectVolumes(ctx, project); err != nil {
  51. return err
  52. }
  53. var observedState Containers
  54. observedState, err = s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
  55. Filters: filters.NewArgs(
  56. projectFilter(project.Name),
  57. ),
  58. All: true,
  59. })
  60. if err != nil {
  61. return err
  62. }
  63. orphans := observedState.filter(isNotService(project.ServiceNames()...))
  64. if len(orphans) > 0 {
  65. if opts.RemoveOrphans {
  66. eg, _ := errgroup.WithContext(ctx)
  67. w := progress.ContextWriter(ctx)
  68. err := s.removeContainers(ctx, w, eg, orphans)
  69. if err != nil {
  70. return err
  71. }
  72. if eg.Wait() != nil {
  73. return err
  74. }
  75. } else {
  76. logrus.Warnf("Found orphan containers (%s) for this project. If "+
  77. "you removed or renamed this service in your compose "+
  78. "file, you can run this command with the "+
  79. "--remove-orphans flag to clean it up.", orphans.names())
  80. }
  81. }
  82. return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
  83. return s.ensureService(c, observedState, project, service)
  84. })
  85. }
  86. func prepareVolumes(p *types.Project) error {
  87. for i := range p.Services {
  88. volumesFrom, dependServices, err := getVolumesFrom(p, p.Services[i].VolumesFrom)
  89. if err != nil {
  90. return err
  91. }
  92. p.Services[i].VolumesFrom = volumesFrom
  93. if len(dependServices) > 0 {
  94. if p.Services[i].DependsOn == nil {
  95. p.Services[i].DependsOn = make(types.DependsOnConfig, len(dependServices))
  96. }
  97. for _, service := range p.Services {
  98. if contains(dependServices, service.Name) {
  99. p.Services[i].DependsOn[service.Name] = types.ServiceDependency{
  100. Condition: types.ServiceConditionStarted,
  101. }
  102. }
  103. }
  104. }
  105. }
  106. return nil
  107. }
  108. func prepareNetworks(project *types.Project) {
  109. for k, network := range project.Networks {
  110. network.Labels = network.Labels.Add(networkLabel, k)
  111. network.Labels = network.Labels.Add(projectLabel, project.Name)
  112. network.Labels = network.Labels.Add(versionLabel, ComposeVersion)
  113. project.Networks[k] = network
  114. }
  115. }
  116. func (s *composeService) ensureNetworks(ctx context.Context, networks types.Networks) error {
  117. for _, network := range networks {
  118. err := s.ensureNetwork(ctx, network)
  119. if err != nil {
  120. return err
  121. }
  122. }
  123. return nil
  124. }
  125. func (s *composeService) ensureProjectVolumes(ctx context.Context, project *types.Project) error {
  126. for k, volume := range project.Volumes {
  127. volume.Labels = volume.Labels.Add(volumeLabel, k)
  128. volume.Labels = volume.Labels.Add(projectLabel, project.Name)
  129. volume.Labels = volume.Labels.Add(versionLabel, ComposeVersion)
  130. err := s.ensureVolume(ctx, volume)
  131. if err != nil {
  132. return err
  133. }
  134. }
  135. return nil
  136. }
  137. func getImageName(service types.ServiceConfig, projectName string) string {
  138. imageName := service.Image
  139. if imageName == "" {
  140. imageName = projectName + "_" + service.Name
  141. }
  142. return imageName
  143. }
  144. func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project, service types.ServiceConfig, number int, inherit *moby.Container,
  145. autoRemove bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
  146. hash, err := jsonHash(service)
  147. if err != nil {
  148. return nil, nil, nil, err
  149. }
  150. labels := map[string]string{}
  151. for k, v := range service.Labels {
  152. labels[k] = v
  153. }
  154. labels[projectLabel] = p.Name
  155. labels[serviceLabel] = service.Name
  156. labels[versionLabel] = ComposeVersion
  157. if _, ok := service.Labels[oneoffLabel]; !ok {
  158. labels[oneoffLabel] = "False"
  159. }
  160. labels[configHashLabel] = hash
  161. labels[workingDirLabel] = p.WorkingDir
  162. labels[configFilesLabel] = strings.Join(p.ComposeFiles, ",")
  163. labels[containerNumberLabel] = strconv.Itoa(number)
  164. var (
  165. runCmd strslice.StrSlice
  166. entrypoint strslice.StrSlice
  167. )
  168. if len(service.Command) > 0 {
  169. runCmd = strslice.StrSlice(service.Command)
  170. }
  171. if len(service.Entrypoint) > 0 {
  172. entrypoint = strslice.StrSlice(service.Entrypoint)
  173. }
  174. var (
  175. tty = service.Tty
  176. stdinOpen = service.StdinOpen
  177. attachStdin = false
  178. )
  179. volumeMounts, binds, mounts, err := s.buildContainerVolumes(ctx, *p, service, inherit)
  180. if err != nil {
  181. return nil, nil, nil, err
  182. }
  183. containerConfig := container.Config{
  184. Hostname: service.Hostname,
  185. Domainname: service.DomainName,
  186. User: service.User,
  187. ExposedPorts: buildContainerPorts(service),
  188. Tty: tty,
  189. OpenStdin: stdinOpen,
  190. StdinOnce: true,
  191. AttachStdin: attachStdin,
  192. AttachStderr: true,
  193. AttachStdout: true,
  194. Cmd: runCmd,
  195. Image: getImageName(service, p.Name),
  196. WorkingDir: service.WorkingDir,
  197. Entrypoint: entrypoint,
  198. NetworkDisabled: service.NetworkMode == "disabled",
  199. MacAddress: service.MacAddress,
  200. Labels: labels,
  201. StopSignal: service.StopSignal,
  202. Env: convert.ToMobyEnv(service.Environment),
  203. Healthcheck: convert.ToMobyHealthCheck(service.HealthCheck),
  204. Volumes: volumeMounts,
  205. StopTimeout: convert.ToSeconds(service.StopGracePeriod),
  206. }
  207. portBindings := buildContainerPortBindingOptions(service)
  208. resources := getDeployResources(service)
  209. networkMode := getNetworkMode(p, service)
  210. hostConfig := container.HostConfig{
  211. AutoRemove: autoRemove,
  212. Binds: binds,
  213. Mounts: mounts,
  214. CapAdd: strslice.StrSlice(service.CapAdd),
  215. CapDrop: strslice.StrSlice(service.CapDrop),
  216. NetworkMode: networkMode,
  217. Init: service.Init,
  218. ReadonlyRootfs: service.ReadOnly,
  219. // ShmSize: , TODO
  220. Sysctls: service.Sysctls,
  221. PortBindings: portBindings,
  222. Resources: resources,
  223. VolumeDriver: service.VolumeDriver,
  224. VolumesFrom: service.VolumesFrom,
  225. }
  226. networkConfig := buildDefaultNetworkConfig(service, networkMode, getContainerName(p.Name, service, number))
  227. return &containerConfig, &hostConfig, networkConfig, nil
  228. }
  229. func getDeployResources(s types.ServiceConfig) container.Resources {
  230. resources := container.Resources{}
  231. if s.Deploy == nil {
  232. return resources
  233. }
  234. reservations := s.Deploy.Resources.Reservations
  235. if reservations == nil || len(reservations.Devices) == 0 {
  236. return resources
  237. }
  238. for _, device := range reservations.Devices {
  239. resources.DeviceRequests = append(resources.DeviceRequests, container.DeviceRequest{
  240. Capabilities: [][]string{device.Capabilities},
  241. Count: int(device.Count),
  242. DeviceIDs: device.IDs,
  243. Driver: device.Driver,
  244. })
  245. }
  246. return resources
  247. }
  248. func buildContainerPorts(s types.ServiceConfig) nat.PortSet {
  249. ports := nat.PortSet{}
  250. for _, p := range s.Ports {
  251. p := nat.Port(fmt.Sprintf("%d/%s", p.Target, p.Protocol))
  252. ports[p] = struct{}{}
  253. }
  254. return ports
  255. }
  256. func buildContainerPortBindingOptions(s types.ServiceConfig) nat.PortMap {
  257. bindings := nat.PortMap{}
  258. for _, port := range s.Ports {
  259. p := nat.Port(fmt.Sprintf("%d/%s", port.Target, port.Protocol))
  260. bind := []nat.PortBinding{}
  261. binding := nat.PortBinding{}
  262. if port.Published > 0 {
  263. binding.HostPort = fmt.Sprint(port.Published)
  264. }
  265. bind = append(bind, binding)
  266. bindings[p] = bind
  267. }
  268. return bindings
  269. }
  270. func getVolumesFrom(project *types.Project, volumesFrom []string) ([]string, []string, error) {
  271. var volumes = []string{}
  272. var services = []string{}
  273. // parse volumes_from
  274. if len(volumesFrom) == 0 {
  275. return volumes, services, nil
  276. }
  277. for _, vol := range volumesFrom {
  278. spec := strings.Split(vol, ":")
  279. if spec[0] == "container" {
  280. volumes = append(volumes, strings.Join(spec[1:], ":"))
  281. continue
  282. }
  283. serviceName := spec[0]
  284. services = append(services, serviceName)
  285. service, err := project.GetService(serviceName)
  286. if err != nil {
  287. return nil, nil, err
  288. }
  289. firstContainer := getContainerName(project.Name, service, 1)
  290. v := fmt.Sprintf("%s:%s", firstContainer, strings.Join(spec[1:], ":"))
  291. volumes = append(volumes, v)
  292. }
  293. return volumes, services, nil
  294. }
  295. func (s *composeService) buildContainerVolumes(ctx context.Context, p types.Project, service types.ServiceConfig,
  296. inherit *moby.Container) (map[string]struct{}, []string, []mount.Mount, error) {
  297. var mounts = []mount.Mount{}
  298. image := getImageName(service, p.Name)
  299. imgInspect, _, err := s.apiClient.ImageInspectWithRaw(ctx, image)
  300. if err != nil {
  301. return nil, nil, nil, err
  302. }
  303. mountOptions, err := buildContainerMountOptions(p, service, imgInspect, inherit)
  304. if err != nil {
  305. return nil, nil, nil, err
  306. }
  307. // filter binds and volumes mount targets
  308. volumeMounts := map[string]struct{}{}
  309. binds := []string{}
  310. for _, m := range mountOptions {
  311. if m.Type == mount.TypeVolume {
  312. volumeMounts[m.Target] = struct{}{}
  313. if m.Source != "" {
  314. binds = append(binds, fmt.Sprintf("%s:%s:rw", m.Source, m.Target))
  315. }
  316. }
  317. }
  318. for _, m := range mountOptions {
  319. if m.Type == mount.TypeBind || m.Type == mount.TypeTmpfs {
  320. mounts = append(mounts, m)
  321. }
  322. }
  323. return volumeMounts, binds, mounts, nil
  324. }
  325. func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
  326. var mounts = map[string]mount.Mount{}
  327. if inherit != nil {
  328. for _, m := range inherit.Mounts {
  329. if m.Type == "tmpfs" {
  330. continue
  331. }
  332. src := m.Source
  333. if m.Type == "volume" {
  334. src = m.Name
  335. }
  336. mounts[m.Destination] = mount.Mount{
  337. Type: m.Type,
  338. Source: src,
  339. Target: m.Destination,
  340. ReadOnly: !m.RW,
  341. }
  342. }
  343. }
  344. if img.ContainerConfig != nil {
  345. for k := range img.ContainerConfig.Volumes {
  346. mount, err := buildMount(p, types.ServiceVolumeConfig{
  347. Type: types.VolumeTypeVolume,
  348. Target: k,
  349. })
  350. if err != nil {
  351. return nil, err
  352. }
  353. mounts[k] = mount
  354. }
  355. }
  356. for _, v := range s.Volumes {
  357. mount, err := buildMount(p, v)
  358. if err != nil {
  359. return nil, err
  360. }
  361. mounts[mount.Target] = mount
  362. }
  363. secrets, err := buildContainerSecretMounts(p, s)
  364. if err != nil {
  365. return nil, err
  366. }
  367. for _, s := range secrets {
  368. if _, found := mounts[s.Target]; found {
  369. continue
  370. }
  371. mounts[s.Target] = s
  372. }
  373. values := make([]mount.Mount, 0, len(mounts))
  374. for _, v := range mounts {
  375. values = append(values, v)
  376. }
  377. return values, nil
  378. }
  379. func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
  380. var mounts = map[string]mount.Mount{}
  381. secretsDir := "/run/secrets"
  382. for _, secret := range s.Secrets {
  383. target := secret.Target
  384. if secret.Target == "" {
  385. target = filepath.Join(secretsDir, secret.Source)
  386. } else if !filepath.IsAbs(secret.Target) {
  387. target = filepath.Join(secretsDir, secret.Target)
  388. }
  389. definedSecret := p.Secrets[secret.Source]
  390. if definedSecret.External.External {
  391. return nil, fmt.Errorf("unsupported external secret %s", definedSecret.Name)
  392. }
  393. mount, err := buildMount(p, types.ServiceVolumeConfig{
  394. Type: types.VolumeTypeBind,
  395. Source: definedSecret.File,
  396. Target: target,
  397. ReadOnly: true,
  398. })
  399. if err != nil {
  400. return nil, err
  401. }
  402. mounts[target] = mount
  403. }
  404. values := make([]mount.Mount, 0, len(mounts))
  405. for _, v := range mounts {
  406. values = append(values, v)
  407. }
  408. return values, nil
  409. }
  410. func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {
  411. source := volume.Source
  412. if volume.Type == types.VolumeTypeBind && !filepath.IsAbs(source) {
  413. // volume source has already been prefixed with workdir if required, by compose-go project loader
  414. var err error
  415. source, err = filepath.Abs(source)
  416. if err != nil {
  417. return mount.Mount{}, err
  418. }
  419. }
  420. if volume.Type == types.VolumeTypeVolume {
  421. if volume.Source != "" {
  422. pVolume, ok := project.Volumes[volume.Source]
  423. if ok {
  424. source = pVolume.Name
  425. }
  426. }
  427. }
  428. return mount.Mount{
  429. Type: mount.Type(volume.Type),
  430. Source: source,
  431. Target: volume.Target,
  432. ReadOnly: volume.ReadOnly,
  433. Consistency: mount.Consistency(volume.Consistency),
  434. BindOptions: buildBindOption(volume.Bind),
  435. VolumeOptions: buildVolumeOptions(volume.Volume),
  436. TmpfsOptions: buildTmpfsOptions(volume.Tmpfs),
  437. }, nil
  438. }
  439. func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions {
  440. if bind == nil {
  441. return nil
  442. }
  443. return &mount.BindOptions{
  444. Propagation: mount.Propagation(bind.Propagation),
  445. // NonRecursive: false, FIXME missing from model ?
  446. }
  447. }
  448. func buildVolumeOptions(vol *types.ServiceVolumeVolume) *mount.VolumeOptions {
  449. if vol == nil {
  450. return nil
  451. }
  452. return &mount.VolumeOptions{
  453. NoCopy: vol.NoCopy,
  454. // Labels: , // FIXME missing from model ?
  455. // DriverConfig: , // FIXME missing from model ?
  456. }
  457. }
  458. func buildTmpfsOptions(tmpfs *types.ServiceVolumeTmpfs) *mount.TmpfsOptions {
  459. if tmpfs == nil {
  460. return nil
  461. }
  462. return &mount.TmpfsOptions{
  463. SizeBytes: tmpfs.Size,
  464. // Mode: , // FIXME missing from model ?
  465. }
  466. }
  467. func buildDefaultNetworkConfig(s types.ServiceConfig, networkMode container.NetworkMode, containerName string) *network.NetworkingConfig {
  468. config := map[string]*network.EndpointSettings{}
  469. net := string(networkMode)
  470. config[net] = &network.EndpointSettings{
  471. Aliases: append(getAliases(s, s.Networks[net]), containerName),
  472. }
  473. return &network.NetworkingConfig{
  474. EndpointsConfig: config,
  475. }
  476. }
  477. func getAliases(s types.ServiceConfig, c *types.ServiceNetworkConfig) []string {
  478. aliases := []string{s.Name}
  479. if c != nil {
  480. aliases = append(aliases, c.Aliases...)
  481. }
  482. return aliases
  483. }
  484. func getNetworkMode(p *types.Project, service types.ServiceConfig) container.NetworkMode {
  485. mode := service.NetworkMode
  486. if mode == "" {
  487. if len(p.Networks) > 0 {
  488. for name := range getNetworksForService(service) {
  489. return container.NetworkMode(p.Networks[name].Name)
  490. }
  491. }
  492. return container.NetworkMode("none")
  493. }
  494. // FIXME incomplete implementation
  495. if strings.HasPrefix(mode, "service:") {
  496. panic("Not yet implemented")
  497. }
  498. if strings.HasPrefix(mode, "container:") {
  499. panic("Not yet implemented")
  500. }
  501. return container.NetworkMode(mode)
  502. }
  503. func getNetworksForService(s types.ServiceConfig) map[string]*types.ServiceNetworkConfig {
  504. if len(s.Networks) > 0 {
  505. return s.Networks
  506. }
  507. return map[string]*types.ServiceNetworkConfig{"default": nil}
  508. }
  509. func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfig) error {
  510. _, err := s.apiClient.NetworkInspect(ctx, n.Name, moby.NetworkInspectOptions{})
  511. if err != nil {
  512. if errdefs.IsNotFound(err) {
  513. if n.External.External {
  514. return fmt.Errorf("network %s declared as external, but could not be found", n.Name)
  515. }
  516. createOpts := moby.NetworkCreate{
  517. // TODO NameSpace Labels
  518. Labels: n.Labels,
  519. Driver: n.Driver,
  520. Options: n.DriverOpts,
  521. Internal: n.Internal,
  522. Attachable: n.Attachable,
  523. }
  524. if n.Ipam.Driver != "" || len(n.Ipam.Config) > 0 {
  525. createOpts.IPAM = &network.IPAM{}
  526. }
  527. if n.Ipam.Driver != "" {
  528. createOpts.IPAM.Driver = n.Ipam.Driver
  529. }
  530. for _, ipamConfig := range n.Ipam.Config {
  531. config := network.IPAMConfig{
  532. Subnet: ipamConfig.Subnet,
  533. }
  534. createOpts.IPAM.Config = append(createOpts.IPAM.Config, config)
  535. }
  536. networkEventName := fmt.Sprintf("Network %q", n.Name)
  537. w := progress.ContextWriter(ctx)
  538. w.Event(progress.CreatingEvent(networkEventName))
  539. if _, err := s.apiClient.NetworkCreate(ctx, n.Name, createOpts); err != nil {
  540. w.Event(progress.ErrorEvent(networkEventName))
  541. return errors.Wrapf(err, "failed to create network %s", n.Name)
  542. }
  543. w.Event(progress.CreatedEvent(networkEventName))
  544. return nil
  545. }
  546. return err
  547. }
  548. return nil
  549. }
  550. func (s *composeService) ensureNetworkDown(ctx context.Context, networkID string, networkName string) error {
  551. w := progress.ContextWriter(ctx)
  552. eventName := fmt.Sprintf("Network %q", networkName)
  553. w.Event(progress.RemovingEvent(eventName))
  554. if err := s.apiClient.NetworkRemove(ctx, networkID); err != nil {
  555. w.Event(progress.ErrorEvent(eventName))
  556. return errors.Wrapf(err, fmt.Sprintf("failed to create network %s", networkID))
  557. }
  558. w.Event(progress.RemovedEvent(eventName))
  559. return nil
  560. }
  561. func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeConfig) error {
  562. // TODO could identify volume by label vs name
  563. _, err := s.apiClient.VolumeInspect(ctx, volume.Name)
  564. if err != nil {
  565. if !errdefs.IsNotFound(err) {
  566. return err
  567. }
  568. eventName := fmt.Sprintf("Volume %q", volume.Name)
  569. w := progress.ContextWriter(ctx)
  570. w.Event(progress.CreatingEvent(eventName))
  571. _, err := s.apiClient.VolumeCreate(ctx, volume_api.VolumeCreateBody{
  572. Labels: volume.Labels,
  573. Name: volume.Name,
  574. Driver: volume.Driver,
  575. DriverOpts: volume.DriverOpts,
  576. })
  577. if err != nil {
  578. w.Event(progress.ErrorEvent(eventName))
  579. return err
  580. }
  581. w.Event(progress.CreatedEvent(eventName))
  582. }
  583. return nil
  584. }