create.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. convert "github.com/docker/compose-cli/local/moby"
  21. "github.com/docker/compose-cli/progress"
  22. "github.com/compose-spec/compose-go/types"
  23. moby "github.com/docker/docker/api/types"
  24. "github.com/docker/docker/api/types/container"
  25. "github.com/docker/docker/api/types/mount"
  26. "github.com/docker/docker/api/types/network"
  27. "github.com/docker/docker/api/types/strslice"
  28. volume_api "github.com/docker/docker/api/types/volume"
  29. "github.com/docker/docker/errdefs"
  30. "github.com/docker/go-connections/nat"
  31. "github.com/pkg/errors"
  32. )
  33. func (s *composeService) Create(ctx context.Context, project *types.Project) error {
  34. err := s.ensureImagesExists(ctx, project)
  35. if err != nil {
  36. return err
  37. }
  38. if err := s.ensureProjectNetworks(ctx, project); err != nil {
  39. return err
  40. }
  41. if err := s.ensureProjectNetworks(ctx, project); err != nil {
  42. return err
  43. }
  44. return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
  45. return s.ensureService(c, project, service)
  46. })
  47. }
  48. func (s *composeService) ensureProjectNetworks(ctx context.Context, project *types.Project) error {
  49. for k, network := range project.Networks {
  50. if !network.External.External && network.Name != "" {
  51. network.Name = fmt.Sprintf("%s_%s", project.Name, k)
  52. project.Networks[k] = network
  53. }
  54. network.Labels = network.Labels.Add(networkLabel, k)
  55. network.Labels = network.Labels.Add(projectLabel, project.Name)
  56. network.Labels = network.Labels.Add(versionLabel, ComposeVersion)
  57. err := s.ensureNetwork(ctx, network)
  58. if err != nil {
  59. return err
  60. }
  61. }
  62. return nil
  63. }
  64. func (s *composeService) ensureProjectVolumes(ctx context.Context, project *types.Project) error {
  65. for k, volume := range project.Volumes {
  66. if !volume.External.External && volume.Name != "" {
  67. volume.Name = fmt.Sprintf("%s_%s", project.Name, k)
  68. project.Volumes[k] = volume
  69. }
  70. volume.Labels = volume.Labels.Add(volumeLabel, k)
  71. volume.Labels = volume.Labels.Add(projectLabel, project.Name)
  72. volume.Labels = volume.Labels.Add(versionLabel, ComposeVersion)
  73. err := s.ensureVolume(ctx, volume)
  74. if err != nil {
  75. return err
  76. }
  77. }
  78. return nil
  79. }
  80. func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number int, inherit *moby.Container) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
  81. hash, err := jsonHash(s)
  82. if err != nil {
  83. return nil, nil, nil, err
  84. }
  85. labels := map[string]string{}
  86. for k, v := range s.Labels {
  87. labels[k] = v
  88. }
  89. labels[projectLabel] = p.Name
  90. labels[serviceLabel] = s.Name
  91. labels[versionLabel] = ComposeVersion
  92. if _, ok := s.Labels[oneoffLabel]; ok {
  93. labels[oneoffLabel] = s.Labels[oneoffLabel]
  94. labels[slugLabel] = s.Labels[slugLabel]
  95. } else {
  96. labels[oneoffLabel] = "False"
  97. }
  98. labels[configHashLabel] = hash
  99. labels[workingDirLabel] = p.WorkingDir
  100. labels[configFilesLabel] = strings.Join(p.ComposeFiles, ",")
  101. labels[containerNumberLabel] = strconv.Itoa(number)
  102. var (
  103. runCmd strslice.StrSlice
  104. entrypoint strslice.StrSlice
  105. )
  106. if len(s.Command) > 0 {
  107. runCmd = strslice.StrSlice(s.Command)
  108. }
  109. if len(s.Entrypoint) > 0 {
  110. entrypoint = strslice.StrSlice(s.Entrypoint)
  111. }
  112. image := s.Image
  113. if s.Image == "" {
  114. image = fmt.Sprintf("%s_%s", p.Name, s.Name)
  115. }
  116. var (
  117. tty = s.Tty
  118. stdinOpen = s.StdinOpen
  119. attachStdin = false
  120. )
  121. containerConfig := container.Config{
  122. Hostname: s.Hostname,
  123. Domainname: s.DomainName,
  124. User: s.User,
  125. ExposedPorts: buildContainerPorts(s),
  126. Tty: tty,
  127. OpenStdin: stdinOpen,
  128. StdinOnce: true,
  129. AttachStdin: attachStdin,
  130. AttachStderr: true,
  131. AttachStdout: true,
  132. Cmd: runCmd,
  133. Image: image,
  134. WorkingDir: s.WorkingDir,
  135. Entrypoint: entrypoint,
  136. NetworkDisabled: s.NetworkMode == "disabled",
  137. MacAddress: s.MacAddress,
  138. Labels: labels,
  139. StopSignal: s.StopSignal,
  140. Env: convert.ToMobyEnv(s.Environment),
  141. Healthcheck: convert.ToMobyHealthCheck(s.HealthCheck),
  142. // Volumes: // FIXME unclear to me the overlap with HostConfig.Mounts
  143. StopTimeout: convert.ToSeconds(s.StopGracePeriod),
  144. }
  145. mountOptions, err := buildContainerMountOptions(*p, s, inherit)
  146. if err != nil {
  147. return nil, nil, nil, err
  148. }
  149. bindings := buildContainerBindingOptions(s)
  150. networkMode := getNetworkMode(p, s)
  151. hostConfig := container.HostConfig{
  152. Mounts: mountOptions,
  153. CapAdd: strslice.StrSlice(s.CapAdd),
  154. CapDrop: strslice.StrSlice(s.CapDrop),
  155. NetworkMode: networkMode,
  156. Init: s.Init,
  157. ReadonlyRootfs: s.ReadOnly,
  158. // ShmSize: , TODO
  159. Sysctls: s.Sysctls,
  160. PortBindings: bindings,
  161. }
  162. networkConfig := buildDefaultNetworkConfig(s, networkMode)
  163. return &containerConfig, &hostConfig, networkConfig, nil
  164. }
  165. func buildContainerPorts(s types.ServiceConfig) nat.PortSet {
  166. ports := nat.PortSet{}
  167. for _, p := range s.Ports {
  168. p := nat.Port(fmt.Sprintf("%d/%s", p.Target, p.Protocol))
  169. ports[p] = struct{}{}
  170. }
  171. return ports
  172. }
  173. func buildContainerBindingOptions(s types.ServiceConfig) nat.PortMap {
  174. bindings := nat.PortMap{}
  175. for _, port := range s.Ports {
  176. p := nat.Port(fmt.Sprintf("%d/%s", port.Target, port.Protocol))
  177. bind := []nat.PortBinding{}
  178. binding := nat.PortBinding{}
  179. if port.Published > 0 {
  180. binding.HostPort = fmt.Sprint(port.Published)
  181. }
  182. bind = append(bind, binding)
  183. bindings[p] = bind
  184. }
  185. return bindings
  186. }
  187. func buildContainerMountOptions(p types.Project, s types.ServiceConfig, inherit *moby.Container) ([]mount.Mount, error) {
  188. mounts := []mount.Mount{}
  189. var inherited []string
  190. if inherit != nil {
  191. for _, m := range inherit.Mounts {
  192. if m.Type == "tmpfs" {
  193. continue
  194. }
  195. src := m.Source
  196. if m.Type == "volume" {
  197. src = m.Name
  198. }
  199. mounts = append(mounts, mount.Mount{
  200. Type: m.Type,
  201. Source: src,
  202. Target: m.Destination,
  203. ReadOnly: !m.RW,
  204. })
  205. inherited = append(inherited, m.Destination)
  206. }
  207. }
  208. for _, v := range s.Volumes {
  209. if contains(inherited, v.Target) {
  210. continue
  211. }
  212. mount, err := buildMount(p, v)
  213. if err != nil {
  214. return nil, err
  215. }
  216. mounts = append(mounts, mount)
  217. }
  218. return mounts, nil
  219. }
  220. func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {
  221. source := volume.Source
  222. if volume.Type == types.VolumeTypeBind && !filepath.IsAbs(source) {
  223. // volume source has already been prefixed with workdir if required, by compose-go project loader
  224. var err error
  225. source, err = filepath.Abs(source)
  226. if err != nil {
  227. return mount.Mount{}, err
  228. }
  229. }
  230. if volume.Type == types.VolumeTypeVolume {
  231. pVolume, ok := project.Volumes[volume.Source]
  232. if ok {
  233. source = pVolume.Name
  234. }
  235. }
  236. return mount.Mount{
  237. Type: mount.Type(volume.Type),
  238. Source: source,
  239. Target: volume.Target,
  240. ReadOnly: volume.ReadOnly,
  241. Consistency: mount.Consistency(volume.Consistency),
  242. BindOptions: buildBindOption(volume.Bind),
  243. VolumeOptions: buildVolumeOptions(volume.Volume),
  244. TmpfsOptions: buildTmpfsOptions(volume.Tmpfs),
  245. }, nil
  246. }
  247. func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions {
  248. if bind == nil {
  249. return nil
  250. }
  251. return &mount.BindOptions{
  252. Propagation: mount.Propagation(bind.Propagation),
  253. // NonRecursive: false, FIXME missing from model ?
  254. }
  255. }
  256. func buildVolumeOptions(vol *types.ServiceVolumeVolume) *mount.VolumeOptions {
  257. if vol == nil {
  258. return nil
  259. }
  260. return &mount.VolumeOptions{
  261. NoCopy: vol.NoCopy,
  262. // Labels: , // FIXME missing from model ?
  263. // DriverConfig: , // FIXME missing from model ?
  264. }
  265. }
  266. func buildTmpfsOptions(tmpfs *types.ServiceVolumeTmpfs) *mount.TmpfsOptions {
  267. if tmpfs == nil {
  268. return nil
  269. }
  270. return &mount.TmpfsOptions{
  271. SizeBytes: tmpfs.Size,
  272. // Mode: , // FIXME missing from model ?
  273. }
  274. }
  275. func buildDefaultNetworkConfig(s types.ServiceConfig, networkMode container.NetworkMode) *network.NetworkingConfig {
  276. config := map[string]*network.EndpointSettings{}
  277. net := string(networkMode)
  278. config[net] = &network.EndpointSettings{
  279. Aliases: getAliases(s, s.Networks[net]),
  280. }
  281. return &network.NetworkingConfig{
  282. EndpointsConfig: config,
  283. }
  284. }
  285. func getAliases(s types.ServiceConfig, c *types.ServiceNetworkConfig) []string {
  286. aliases := []string{s.Name}
  287. if c != nil {
  288. aliases = append(aliases, c.Aliases...)
  289. }
  290. return aliases
  291. }
  292. func getNetworkMode(p *types.Project, service types.ServiceConfig) container.NetworkMode {
  293. mode := service.NetworkMode
  294. if mode == "" {
  295. if len(p.Networks) > 0 {
  296. for name := range getNetworksForService(service) {
  297. return container.NetworkMode(p.Networks[name].Name)
  298. }
  299. }
  300. return container.NetworkMode("none")
  301. }
  302. // FIXME incomplete implementation
  303. if strings.HasPrefix(mode, "service:") {
  304. panic("Not yet implemented")
  305. }
  306. if strings.HasPrefix(mode, "container:") {
  307. panic("Not yet implemented")
  308. }
  309. return container.NetworkMode(mode)
  310. }
  311. func getNetworksForService(s types.ServiceConfig) map[string]*types.ServiceNetworkConfig {
  312. if len(s.Networks) > 0 {
  313. return s.Networks
  314. }
  315. return map[string]*types.ServiceNetworkConfig{"default": nil}
  316. }
  317. func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfig) error {
  318. _, err := s.apiClient.NetworkInspect(ctx, n.Name, moby.NetworkInspectOptions{})
  319. if err != nil {
  320. if errdefs.IsNotFound(err) {
  321. if n.External.External {
  322. return fmt.Errorf("network %s declared as external, but could not be found", n.Name)
  323. }
  324. createOpts := moby.NetworkCreate{
  325. // TODO NameSpace Labels
  326. Labels: n.Labels,
  327. Driver: n.Driver,
  328. Options: n.DriverOpts,
  329. Internal: n.Internal,
  330. Attachable: n.Attachable,
  331. }
  332. if n.Ipam.Driver != "" || len(n.Ipam.Config) > 0 {
  333. createOpts.IPAM = &network.IPAM{}
  334. }
  335. if n.Ipam.Driver != "" {
  336. createOpts.IPAM.Driver = n.Ipam.Driver
  337. }
  338. for _, ipamConfig := range n.Ipam.Config {
  339. config := network.IPAMConfig{
  340. Subnet: ipamConfig.Subnet,
  341. }
  342. createOpts.IPAM.Config = append(createOpts.IPAM.Config, config)
  343. }
  344. networkEventName := fmt.Sprintf("Network %q", n.Name)
  345. w := progress.ContextWriter(ctx)
  346. w.Event(progress.CreatingEvent(networkEventName))
  347. if _, err := s.apiClient.NetworkCreate(ctx, n.Name, createOpts); err != nil {
  348. w.Event(progress.ErrorEvent(networkEventName))
  349. return errors.Wrapf(err, "failed to create network %s", n.Name)
  350. }
  351. w.Event(progress.CreatedEvent(networkEventName))
  352. return nil
  353. }
  354. return err
  355. }
  356. return nil
  357. }
  358. func (s *composeService) ensureNetworkDown(ctx context.Context, networkID string, networkName string) error {
  359. w := progress.ContextWriter(ctx)
  360. eventName := fmt.Sprintf("Network %q", networkName)
  361. w.Event(progress.RemovingEvent(eventName))
  362. if err := s.apiClient.NetworkRemove(ctx, networkID); err != nil {
  363. w.Event(progress.ErrorEvent(eventName))
  364. return errors.Wrapf(err, fmt.Sprintf("failed to create network %s", networkID))
  365. }
  366. w.Event(progress.RemovedEvent(eventName))
  367. return nil
  368. }
  369. func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeConfig) error {
  370. // TODO could identify volume by label vs name
  371. _, err := s.apiClient.VolumeInspect(ctx, volume.Name)
  372. if err != nil {
  373. if !errdefs.IsNotFound(err) {
  374. return err
  375. }
  376. eventName := fmt.Sprintf("Volume %q", volume.Name)
  377. w := progress.ContextWriter(ctx)
  378. w.Event(progress.CreatingEvent(eventName))
  379. // TODO we miss support for driver_opts and labels
  380. _, err := s.apiClient.VolumeCreate(ctx, volume_api.VolumeCreateBody{
  381. Labels: volume.Labels,
  382. Name: volume.Name,
  383. Driver: volume.Driver,
  384. DriverOpts: volume.DriverOpts,
  385. })
  386. if err != nil {
  387. w.Event(progress.ErrorEvent(eventName))
  388. return err
  389. }
  390. w.Event(progress.CreatedEvent(eventName))
  391. }
  392. return nil
  393. }