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