create.go 15 KB

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