Repository: docker/compose
File: pkg/compose/generate.go (Go)

Code before:

		}
		service.Scale = increment(service.Scale)

		inspect, err := s.apiClient().ContainerInspect(context.Background(), c.ID)
		if err != nil {
			services[serviceLabel] = service
			continue
		}
		s.extractComposeConfiguration(&service, inspect, volumes, secrets, networks)
		service.Labels = cleanDockerPreviousLabels(service.Labels)
		services[serviceLabel] = service
	}

	project.Services = services
	project.Networks = networks
	project.Volumes = volumes
	project.Secrets = secrets
	project.Configs = configs
	return project, nil
}

func (s *composeService) extractComposeConfiguration(service *types.ServiceConfig, inspect moby.ContainerJSON, volumes types.Volumes, secrets types.Secrets, networks types.Networks) {
	service.Environment = types.NewMappingWithEquals(inspect.Config.Env)
	if inspect.Config.Healthcheck != nil {
		healthConfig := inspect.Config.Healthcheck
		service.HealthCheck = s.toComposeHealthCheck(healthConfig)
	}
	if len(inspect.Mounts) > 0 {
		detectedVolumes, volumeConfigs, detectedSecrets, secretsConfigs := s.toComposeVolumes(inspect.Mounts)
		service.Volumes = append(service.Volumes, volumeConfigs...)
		service.Secrets = append(service.Secrets, secretsConfigs...)
		maps.Copy(volumes, detectedVolumes)
		maps.Copy(secrets, detectedSecrets)
	}
	if len(inspect.NetworkSettings.Networks) > 0 {
		detectedNetworks, networkConfigs := s.toComposeNetwork(inspect.NetworkSettings.Networks)
		service.Networks = networkConfigs
		maps.Copy(networks, detectedNetworks)
	}
	if len(inspect.HostConfig.PortBindings) > 0 {
		for key, portBindings := range inspect.HostConfig.PortBindings {
			for _, portBinding := range portBindings {
				service.Ports = append(service.Ports, types.ServicePortConfig{
					Target:    uint32(key.Int()),
					Published: portBinding.HostPort,
					Protocol:  key.Proto(),
				})
			}
		}
	}

Review comment (suggestion):
do you know how container runs with env files get handled? do those values just get mapped to the `Config.Env`? i had this as a question / todo in my own work.

Code after:
				Image:  c.Image,
				Labels: c.Labels,
			}

		}
		service.Scale = increment(service.Scale)

		inspect, err := s.apiClient().ContainerInspect(context.Background(), c.ID)
		if err != nil {
			services[serviceLabel] = service
			continue
		}
		s.extractComposeConfiguration(&service, inspect, volumes, secrets, networks)
		service.Labels = cleanDockerPreviousLabels(service.Labels)
		services[serviceLabel] = service
	}

	project.Services = services
	project.Networks = networks
	project.Volumes = volumes
	project.Secrets = secrets
	return project, nil
}

func (s *composeService) extractComposeConfiguration(service *types.ServiceConfig, inspect moby.ContainerJSON, volumes types.Volumes, secrets types.Secrets, networks types.Networks) {
	service.Environment = types.NewMappingWithEquals(inspect.Config.Env)
	if inspect.Config.Healthcheck != nil {
		healthConfig := inspect.Config.Healthcheck
		service.HealthCheck = s.toComposeHealthCheck(healthConfig)
	}
	if len(inspect.Mounts) > 0 {
		detectedVolumes, volumeConfigs, detectedSecrets, secretsConfigs := s.toComposeVolumes(inspect.Mounts)
		service.Volumes = append(service.Volumes, volumeConfigs...)
		service.Secrets = append(service.Secrets, secretsConfigs...)
		maps.Copy(volumes, detectedVolumes)
		maps.Copy(secrets, detectedSecrets)
	}
	if len(inspect.NetworkSettings.Networks) > 0 {
		detectedNetworks, networkConfigs := s.toComposeNetwork(inspect.NetworkSettings.Networks)
		service.Networks = networkConfigs
		maps.Copy(networks, detectedNetworks)
	}
	if len(inspect.HostConfig.PortBindings) > 0 {
		for key, portBindings := range inspect.HostConfig.PortBindings {
			for _, portBinding := range portBindings {
				service.Ports = append(service.Ports, types.ServicePortConfig{
					Target:    uint32(key.Int()),
					Published: portBinding.HostPort,
					Protocol:  key.Proto(),
					HostIP:    portBinding.HostIP,
				})