create.go 15 KB

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