MCPcopy
hub / github.com/docker/compose / createProjectFromContainers

Method createProjectFromContainers

pkg/compose/generate.go:68–110  ·  view source on GitHub ↗
(containers []container.Summary, projectName string)

Source from the content-addressed store, hash-verified

66}
67
68func (s *composeService) createProjectFromContainers(containers []container.Summary, projectName string) (*types.Project, error) {
69 project := &types.Project{}
70 services := types.Services{}
71 networks := types.Networks{}
72 volumes := types.Volumes{}
73 secrets := types.Secrets{}
74
75 if projectName != "" {
76 project.Name = projectName
77 }
78
79 for _, c := range containers {
80 // if the container is from a previous Compose application, use the existing service name
81 serviceLabel, ok := c.Labels[api.ServiceLabel]
82 if !ok {
83 serviceLabel = getCanonicalContainerName(c)
84 }
85 service, ok := services[serviceLabel]
86 if !ok {
87 service = types.ServiceConfig{
88 Name: serviceLabel,
89 Image: c.Image,
90 Labels: c.Labels,
91 }
92 }
93 service.Scale = increment(service.Scale)
94
95 inspect, err := s.apiClient().ContainerInspect(context.Background(), c.ID, client.ContainerInspectOptions{})
96 if err != nil {
97 services[serviceLabel] = service
98 continue
99 }
100 s.extractComposeConfiguration(&service, inspect.Container, volumes, secrets, networks)
101 service.Labels = cleanDockerPreviousLabels(service.Labels)
102 services[serviceLabel] = service
103 }
104
105 project.Services = services
106 project.Networks = networks
107 project.Volumes = volumes
108 project.Secrets = secrets
109 return project, nil
110}
111
112func (s *composeService) extractComposeConfiguration(service *types.ServiceConfig, inspect container.InspectResponse, volumes types.Volumes, secrets types.Secrets, networks types.Networks) {
113 service.Environment = types.NewMappingWithEquals(inspect.Config.Env)

Callers 1

GenerateMethod · 0.95

Calls 6

apiClientMethod · 0.95
incrementFunction · 0.85
ContainerInspectMethod · 0.45

Tested by

no test coverage detected