create.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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 len(spec) == 0 {
  280. continue
  281. }
  282. if spec[0] == "container" {
  283. volumes = append(volumes, strings.Join(spec[1:], ":"))
  284. continue
  285. }
  286. serviceName := spec[0]
  287. services = append(services, serviceName)
  288. service, err := project.GetService(serviceName)
  289. if err != nil {
  290. return nil, nil, err
  291. }
  292. firstContainer := getContainerName(project.Name, service, 1)
  293. v := fmt.Sprintf("%s:%s", firstContainer, strings.Join(spec[1:], ":"))
  294. volumes = append(volumes, v)
  295. }
  296. return volumes, services, nil
  297. }
  298. func (s *composeService) buildContainerVolumes(ctx context.Context, p types.Project, service types.ServiceConfig,
  299. inherit *moby.Container) (map[string]struct{}, []string, []mount.Mount, error) {
  300. var mounts = []mount.Mount{}
  301. image := getImageName(service, p.Name)
  302. imgInspect, _, err := s.apiClient.ImageInspectWithRaw(ctx, image)
  303. if err != nil {
  304. return nil, nil, nil, err
  305. }
  306. mountOptions, err := buildContainerMountOptions(p, service, imgInspect, inherit)
  307. if err != nil {
  308. return nil, nil, nil, err
  309. }
  310. // filter binds and volumes mount targets
  311. volumeMounts := map[string]struct{}{}
  312. binds := []string{}
  313. for _, m := range mountOptions {
  314. if m.Type == mount.TypeVolume {
  315. volumeMounts[m.Target] = struct{}{}
  316. if m.Source != "" {
  317. binds = append(binds, fmt.Sprintf("%s:%s:rw", m.Source, m.Target))
  318. }
  319. }
  320. }
  321. for _, m := range mountOptions {
  322. if m.Type == mount.TypeBind || m.Type == mount.TypeTmpfs {
  323. mounts = append(mounts, m)
  324. }
  325. }
  326. return volumeMounts, binds, mounts, nil
  327. }
  328. func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
  329. var mounts = map[string]mount.Mount{}
  330. if inherit != nil {
  331. for _, m := range inherit.Mounts {
  332. if m.Type == "tmpfs" {
  333. continue
  334. }
  335. src := m.Source
  336. if m.Type == "volume" {
  337. src = m.Name
  338. }
  339. mounts[m.Destination] = mount.Mount{
  340. Type: m.Type,
  341. Source: src,
  342. Target: m.Destination,
  343. ReadOnly: !m.RW,
  344. }
  345. }
  346. }
  347. if img.ContainerConfig != nil {
  348. for k := range img.ContainerConfig.Volumes {
  349. mount, err := buildMount(p, types.ServiceVolumeConfig{
  350. Type: types.VolumeTypeVolume,
  351. Target: k,
  352. })
  353. if err != nil {
  354. return nil, err
  355. }
  356. mounts[k] = mount
  357. }
  358. }
  359. for _, v := range s.Volumes {
  360. mount, err := buildMount(p, v)
  361. if err != nil {
  362. return nil, err
  363. }
  364. mounts[mount.Target] = mount
  365. }
  366. secrets, err := buildContainerSecretMounts(p, s)
  367. if err != nil {
  368. return nil, err
  369. }
  370. for _, s := range secrets {
  371. if _, found := mounts[s.Target]; found {
  372. continue
  373. }
  374. mounts[s.Target] = s
  375. }
  376. values := make([]mount.Mount, 0, len(mounts))
  377. for _, v := range mounts {
  378. values = append(values, v)
  379. }
  380. return values, nil
  381. }
  382. func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
  383. var mounts = map[string]mount.Mount{}
  384. secretsDir := "/run/secrets"
  385. for _, secret := range s.Secrets {
  386. target := secret.Target
  387. if secret.Target == "" {
  388. target = filepath.Join(secretsDir, secret.Source)
  389. } else if !filepath.IsAbs(secret.Target) {
  390. target = filepath.Join(secretsDir, secret.Target)
  391. }
  392. definedSecret := p.Secrets[secret.Source]
  393. if definedSecret.External.External {
  394. return nil, fmt.Errorf("unsupported external secret %s", definedSecret.Name)
  395. }
  396. mount, err := buildMount(p, types.ServiceVolumeConfig{
  397. Type: types.VolumeTypeBind,
  398. Source: definedSecret.File,
  399. Target: target,
  400. ReadOnly: true,
  401. })
  402. if err != nil {
  403. return nil, err
  404. }
  405. mounts[target] = mount
  406. }
  407. values := make([]mount.Mount, 0, len(mounts))
  408. for _, v := range mounts {
  409. values = append(values, v)
  410. }
  411. return values, nil
  412. }
  413. func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {
  414. source := volume.Source
  415. if volume.Type == types.VolumeTypeBind && !filepath.IsAbs(source) {
  416. // volume source has already been prefixed with workdir if required, by compose-go project loader
  417. var err error
  418. source, err = filepath.Abs(source)
  419. if err != nil {
  420. return mount.Mount{}, err
  421. }
  422. }
  423. if volume.Type == types.VolumeTypeVolume {
  424. if volume.Source != "" {
  425. pVolume, ok := project.Volumes[volume.Source]
  426. if ok {
  427. source = pVolume.Name
  428. }
  429. }
  430. }
  431. return mount.Mount{
  432. Type: mount.Type(volume.Type),
  433. Source: source,
  434. Target: volume.Target,
  435. ReadOnly: volume.ReadOnly,
  436. Consistency: mount.Consistency(volume.Consistency),
  437. BindOptions: buildBindOption(volume.Bind),
  438. VolumeOptions: buildVolumeOptions(volume.Volume),
  439. TmpfsOptions: buildTmpfsOptions(volume.Tmpfs),
  440. }, nil
  441. }
  442. func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions {
  443. if bind == nil {
  444. return nil
  445. }
  446. return &mount.BindOptions{
  447. Propagation: mount.Propagation(bind.Propagation),
  448. // NonRecursive: false, FIXME missing from model ?
  449. }
  450. }
  451. func buildVolumeOptions(vol *types.ServiceVolumeVolume) *mount.VolumeOptions {
  452. if vol == nil {
  453. return nil
  454. }
  455. return &mount.VolumeOptions{
  456. NoCopy: vol.NoCopy,
  457. // Labels: , // FIXME missing from model ?
  458. // DriverConfig: , // FIXME missing from model ?
  459. }
  460. }
  461. func buildTmpfsOptions(tmpfs *types.ServiceVolumeTmpfs) *mount.TmpfsOptions {
  462. if tmpfs == nil {
  463. return nil
  464. }
  465. return &mount.TmpfsOptions{
  466. SizeBytes: tmpfs.Size,
  467. // Mode: , // FIXME missing from model ?
  468. }
  469. }
  470. func buildDefaultNetworkConfig(s types.ServiceConfig, networkMode container.NetworkMode, containerName string) *network.NetworkingConfig {
  471. config := map[string]*network.EndpointSettings{}
  472. net := string(networkMode)
  473. config[net] = &network.EndpointSettings{
  474. Aliases: append(getAliases(s, s.Networks[net]), containerName),
  475. }
  476. return &network.NetworkingConfig{
  477. EndpointsConfig: config,
  478. }
  479. }
  480. func getAliases(s types.ServiceConfig, c *types.ServiceNetworkConfig) []string {
  481. aliases := []string{s.Name}
  482. if c != nil {
  483. aliases = append(aliases, c.Aliases...)
  484. }
  485. return aliases
  486. }
  487. func getNetworkMode(p *types.Project, service types.ServiceConfig) container.NetworkMode {
  488. mode := service.NetworkMode
  489. if mode == "" {
  490. if len(p.Networks) > 0 {
  491. for name := range getNetworksForService(service) {
  492. return container.NetworkMode(p.Networks[name].Name)
  493. }
  494. }
  495. return container.NetworkMode("none")
  496. }
  497. // FIXME incomplete implementation
  498. if strings.HasPrefix(mode, "service:") {
  499. panic("Not yet implemented")
  500. }
  501. if strings.HasPrefix(mode, "container:") {
  502. panic("Not yet implemented")
  503. }
  504. return container.NetworkMode(mode)
  505. }
  506. func getNetworksForService(s types.ServiceConfig) map[string]*types.ServiceNetworkConfig {
  507. if len(s.Networks) > 0 {
  508. return s.Networks
  509. }
  510. return map[string]*types.ServiceNetworkConfig{"default": nil}
  511. }
  512. func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfig) error {
  513. _, err := s.apiClient.NetworkInspect(ctx, n.Name, moby.NetworkInspectOptions{})
  514. if err != nil {
  515. if errdefs.IsNotFound(err) {
  516. if n.External.External {
  517. return fmt.Errorf("network %s declared as external, but could not be found", n.Name)
  518. }
  519. createOpts := moby.NetworkCreate{
  520. // TODO NameSpace Labels
  521. Labels: n.Labels,
  522. Driver: n.Driver,
  523. Options: n.DriverOpts,
  524. Internal: n.Internal,
  525. Attachable: n.Attachable,
  526. }
  527. if n.Ipam.Driver != "" || len(n.Ipam.Config) > 0 {
  528. createOpts.IPAM = &network.IPAM{}
  529. }
  530. if n.Ipam.Driver != "" {
  531. createOpts.IPAM.Driver = n.Ipam.Driver
  532. }
  533. for _, ipamConfig := range n.Ipam.Config {
  534. config := network.IPAMConfig{
  535. Subnet: ipamConfig.Subnet,
  536. }
  537. createOpts.IPAM.Config = append(createOpts.IPAM.Config, config)
  538. }
  539. networkEventName := fmt.Sprintf("Network %q", n.Name)
  540. w := progress.ContextWriter(ctx)
  541. w.Event(progress.CreatingEvent(networkEventName))
  542. if _, err := s.apiClient.NetworkCreate(ctx, n.Name, createOpts); err != nil {
  543. w.Event(progress.ErrorEvent(networkEventName))
  544. return errors.Wrapf(err, "failed to create network %s", n.Name)
  545. }
  546. w.Event(progress.CreatedEvent(networkEventName))
  547. return nil
  548. }
  549. return err
  550. }
  551. return nil
  552. }
  553. func (s *composeService) ensureNetworkDown(ctx context.Context, networkID string, networkName string) error {
  554. w := progress.ContextWriter(ctx)
  555. eventName := fmt.Sprintf("Network %q", networkName)
  556. w.Event(progress.RemovingEvent(eventName))
  557. if err := s.apiClient.NetworkRemove(ctx, networkID); err != nil {
  558. w.Event(progress.ErrorEvent(eventName))
  559. return errors.Wrapf(err, fmt.Sprintf("failed to create network %s", networkID))
  560. }
  561. w.Event(progress.RemovedEvent(eventName))
  562. return nil
  563. }
  564. func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeConfig) error {
  565. // TODO could identify volume by label vs name
  566. _, err := s.apiClient.VolumeInspect(ctx, volume.Name)
  567. if err != nil {
  568. if !errdefs.IsNotFound(err) {
  569. return err
  570. }
  571. eventName := fmt.Sprintf("Volume %q", volume.Name)
  572. w := progress.ContextWriter(ctx)
  573. w.Event(progress.CreatingEvent(eventName))
  574. _, err := s.apiClient.VolumeCreate(ctx, volume_api.VolumeCreateBody{
  575. Labels: volume.Labels,
  576. Name: volume.Name,
  577. Driver: volume.Driver,
  578. DriverOpts: volume.DriverOpts,
  579. })
  580. if err != nil {
  581. w.Event(progress.ErrorEvent(eventName))
  582. return err
  583. }
  584. w.Event(progress.CreatedEvent(eventName))
  585. }
  586. return nil
  587. }