mirror of
https://github.com/docker/compose.git
synced 2025-07-07 13:54:34 +02:00
Merge pull request #1530 from aiordache/recreate_after_build
This commit is contained in:
commit
cd4a08d0d6
@ -99,8 +99,15 @@ func runImages(ctx context.Context, opts imageOptions, services []string) error
|
|||||||
for _, img := range images {
|
for _, img := range images {
|
||||||
id := stringid.TruncateID(img.ID)
|
id := stringid.TruncateID(img.ID)
|
||||||
size := units.HumanSizeWithPrecision(float64(img.Size), 3)
|
size := units.HumanSizeWithPrecision(float64(img.Size), 3)
|
||||||
|
repo := img.Repository
|
||||||
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", img.ContainerName, img.Repository, img.Tag, id, size)
|
if repo == "" {
|
||||||
|
repo = "<none>"
|
||||||
|
}
|
||||||
|
tag := img.Tag
|
||||||
|
if tag == "" {
|
||||||
|
tag = "<none>"
|
||||||
|
}
|
||||||
|
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", img.ContainerName, repo, tag, id, size)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Container", "Repository", "Tag", "Image Id", "Size")
|
"Container", "Repository", "Tag", "Image Id", "Size")
|
||||||
|
@ -32,7 +32,6 @@ import (
|
|||||||
"github.com/docker/buildx/util/progress"
|
"github.com/docker/buildx/util/progress"
|
||||||
cliconfig "github.com/docker/cli/cli/config"
|
cliconfig "github.com/docker/cli/cli/config"
|
||||||
moby "github.com/docker/docker/api/types"
|
moby "github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/errdefs"
|
|
||||||
bclient "github.com/moby/buildkit/client"
|
bclient "github.com/moby/buildkit/client"
|
||||||
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
|
|
||||||
@ -73,7 +72,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := s.build(ctx, project, opts, Containers{}, options.Progress)
|
_, err := s.build(ctx, project, opts, Containers{}, options.Progress)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if len(imagesToBuild) > 0 && !options.Quiet {
|
if len(imagesToBuild) > 0 && !options.Quiet {
|
||||||
utils.DisplayScanSuggestMsg()
|
utils.DisplayScanSuggestMsg()
|
||||||
@ -84,28 +83,60 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) ensureImagesExists(ctx context.Context, project *types.Project, observedState Containers, quietPull bool) error {
|
func (s *composeService) ensureImagesExists(ctx context.Context, project *types.Project, observedState Containers, quietPull bool) error {
|
||||||
|
images, err := s.getImageDigests(ctx, project)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
mode := progress.PrinterModeAuto
|
||||||
|
if quietPull {
|
||||||
|
mode = progress.PrinterModeQuiet
|
||||||
|
}
|
||||||
|
opts, imagesToBuild, err := s.getBuildOptions(project, images)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
builtImages, err := s.build(ctx, project, opts, observedState, mode)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(imagesToBuild) > 0 {
|
||||||
|
utils.DisplayScanSuggestMsg()
|
||||||
|
}
|
||||||
|
for name, digest := range builtImages {
|
||||||
|
images[name] = digest
|
||||||
|
}
|
||||||
|
// set digest as service.Image
|
||||||
|
for i, service := range project.Services {
|
||||||
|
digest, ok := images[getImageName(service, project.Name)]
|
||||||
|
if ok {
|
||||||
|
project.Services[i].Image = digest
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *composeService) getBuildOptions(project *types.Project, images map[string]string) (map[string]build.Options, []string, error) {
|
||||||
opts := map[string]build.Options{}
|
opts := map[string]build.Options{}
|
||||||
imagesToBuild := []string{}
|
imagesToBuild := []string{}
|
||||||
for _, service := range project.Services {
|
for _, service := range project.Services {
|
||||||
if service.Image == "" && service.Build == nil {
|
if service.Image == "" && service.Build == nil {
|
||||||
return fmt.Errorf("invalid service %q. Must specify either image or build", service.Name)
|
return nil, nil, fmt.Errorf("invalid service %q. Must specify either image or build", service.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
imageName := getImageName(service, project.Name)
|
imageName := getImageName(service, project.Name)
|
||||||
localImagePresent, err := s.localImagePresent(ctx, imageName)
|
_, localImagePresent := images[imageName]
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if service.Build != nil {
|
if service.Build != nil {
|
||||||
if localImagePresent && service.PullPolicy != types.PullPolicyBuild {
|
if localImagePresent && service.PullPolicy != types.PullPolicyBuild {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
imagesToBuild = append(imagesToBuild, imageName)
|
imagesToBuild = append(imagesToBuild, imageName)
|
||||||
opts[imageName], err = s.toBuildOptions(service, project.WorkingDir, imageName)
|
opt, err := s.toBuildOptions(service, project.WorkingDir, imageName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
opts[imageName] = opt
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if service.Image != "" {
|
if service.Image != "" {
|
||||||
@ -113,7 +144,6 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Buildx has no command to "just pull", see
|
// Buildx has no command to "just pull", see
|
||||||
// so we bake a temporary dockerfile that will just pull and export pulled image
|
// so we bake a temporary dockerfile that will just pull and export pulled image
|
||||||
opts[service.Name] = build.Options{
|
opts[service.Name] = build.Options{
|
||||||
@ -127,57 +157,54 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
return opts, imagesToBuild, nil
|
||||||
|
|
||||||
mode := progress.PrinterModeAuto
|
|
||||||
if quietPull {
|
|
||||||
mode = progress.PrinterModeQuiet
|
|
||||||
}
|
|
||||||
|
|
||||||
err := s.build(ctx, project, opts, observedState, mode)
|
|
||||||
if err == nil {
|
|
||||||
if len(imagesToBuild) > 0 {
|
|
||||||
utils.DisplayScanSuggestMsg()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) localImagePresent(ctx context.Context, imageName string) (bool, error) {
|
func (s *composeService) getImageDigests(ctx context.Context, project *types.Project) (map[string]string, error) {
|
||||||
_, _, err := s.apiClient.ImageInspectWithRaw(ctx, imageName)
|
imageNames := []string{}
|
||||||
|
for _, s := range project.Services {
|
||||||
|
imgName := getImageName(s, project.Name)
|
||||||
|
if !utils.StringContains(imageNames, imgName) {
|
||||||
|
imageNames = append(imageNames, imgName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
imgs, err := s.getImages(ctx, imageNames)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errdefs.IsNotFound(err) {
|
return nil, err
|
||||||
return false, nil
|
|
||||||
}
|
}
|
||||||
return false, err
|
images := map[string]string{}
|
||||||
|
for name, info := range imgs {
|
||||||
|
images[name] = info.ID
|
||||||
}
|
}
|
||||||
return true, nil
|
return images, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options, observedState Containers, mode string) error {
|
func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options, observedState Containers, mode string) (map[string]string, error) {
|
||||||
info, err := s.apiClient.Info(ctx)
|
info, err := s.apiClient.Info(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.OSType == "windows" {
|
if info.OSType == "windows" {
|
||||||
// no support yet for Windows container builds in Buildkit
|
// no support yet for Windows container builds in Buildkit
|
||||||
// https://docs.docker.com/develop/develop-images/build_enhancements/#limitations
|
// https://docs.docker.com/develop/develop-images/build_enhancements/#limitations
|
||||||
err := s.windowsBuild(opts, mode)
|
err := s.windowsBuild(opts, mode)
|
||||||
return metrics.WrapCategorisedComposeError(err, metrics.BuildFailure)
|
return nil, metrics.WrapCategorisedComposeError(err, metrics.BuildFailure)
|
||||||
}
|
}
|
||||||
if len(opts) == 0 {
|
if len(opts) == 0 {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
const drivername = "default"
|
const drivername = "default"
|
||||||
|
|
||||||
configFile, err := cliconfig.Load(config.Dir())
|
configFile, err := cliconfig.Load(config.Dir())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
d, err := driver.GetDriver(ctx, drivername, nil, s.apiClient, configFile, nil, nil, "", nil, nil, project.WorkingDir)
|
d, err := driver.GetDriver(ctx, drivername, nil, s.apiClient, configFile, nil, nil, "", nil, nil, project.WorkingDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
driverInfo := []build.DriverInfo{
|
driverInfo := []build.DriverInfo{
|
||||||
{
|
{
|
||||||
@ -194,13 +221,13 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts
|
|||||||
w := progress.NewPrinter(progressCtx, os.Stdout, mode)
|
w := progress.NewPrinter(progressCtx, os.Stdout, mode)
|
||||||
|
|
||||||
// We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
|
// We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
|
||||||
_, err = build.Build(ctx, driverInfo, opts, nil, nil, w)
|
response, err := build.Build(ctx, driverInfo, opts, nil, nil, w)
|
||||||
errW := w.Wait()
|
errW := w.Wait()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = errW
|
err = errW
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return metrics.WrapCategorisedComposeError(err, metrics.BuildFailure)
|
return nil, metrics.WrapCategorisedComposeError(err, metrics.BuildFailure)
|
||||||
}
|
}
|
||||||
|
|
||||||
cw := composeprogress.ContextWriter(ctx)
|
cw := composeprogress.ContextWriter(ctx)
|
||||||
@ -209,13 +236,25 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts
|
|||||||
if c.Image == imageName {
|
if c.Image == imageName {
|
||||||
err = s.removeContainers(ctx, cw, []moby.Container{c}, nil)
|
err = s.removeContainers(ctx, cw, []moby.Container{c}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
imagesBuilt := map[string]string{}
|
||||||
|
for name, img := range response {
|
||||||
|
if img == nil || len(img.ExporterResponse) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
digest, ok := img.ExporterResponse["containerimage.digest"]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
imagesBuilt[name] = digest
|
||||||
|
}
|
||||||
|
|
||||||
|
return imagesBuilt, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath string, imageTag string) (build.Options, error) {
|
func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath string, imageTag string) (build.Options, error) {
|
||||||
|
@ -24,6 +24,7 @@ import (
|
|||||||
|
|
||||||
moby "github.com/docker/docker/api/types"
|
moby "github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
|
"github.com/docker/docker/errdefs"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
|
|
||||||
"github.com/docker/compose-cli/api/compose"
|
"github.com/docker/compose-cli/api/compose"
|
||||||
@ -57,25 +58,7 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
|
|||||||
imageIDs = append(imageIDs, c.ImageID)
|
imageIDs = append(imageIDs, c.ImageID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
images, err := s.getImages(ctx, imageIDs)
|
||||||
images := map[string]moby.ImageInspect{}
|
|
||||||
l := sync.Mutex{}
|
|
||||||
eg, ctx := errgroup.WithContext(ctx)
|
|
||||||
for _, img := range imageIDs {
|
|
||||||
img := img
|
|
||||||
eg.Go(func() error {
|
|
||||||
inspect, _, err := s.apiClient.ImageInspectWithRaw(ctx, img)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
l.Lock()
|
|
||||||
images[img] = inspect
|
|
||||||
l.Unlock()
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
err = eg.Wait()
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -85,24 +68,47 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("failed to retrieve image for container %s", getCanonicalContainerName(container))
|
return nil, fmt.Errorf("failed to retrieve image for container %s", getCanonicalContainerName(container))
|
||||||
}
|
}
|
||||||
if len(img.RepoTags) == 0 {
|
|
||||||
return nil, fmt.Errorf("no image tag found for %s", img.ID)
|
summary[i] = img
|
||||||
|
summary[i].ContainerName = getCanonicalContainerName(container)
|
||||||
|
}
|
||||||
|
return summary, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *composeService) getImages(ctx context.Context, images []string) (map[string]compose.ImageSummary, error) {
|
||||||
|
summary := map[string]compose.ImageSummary{}
|
||||||
|
l := sync.Mutex{}
|
||||||
|
eg, ctx := errgroup.WithContext(ctx)
|
||||||
|
for _, img := range images {
|
||||||
|
img := img
|
||||||
|
eg.Go(func() error {
|
||||||
|
inspect, _, err := s.apiClient.ImageInspectWithRaw(ctx, img)
|
||||||
|
if err != nil {
|
||||||
|
if errdefs.IsNotFound(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
tag := ""
|
tag := ""
|
||||||
repository := ""
|
repository := ""
|
||||||
repotag := strings.Split(img.RepoTags[0], ":")
|
if len(inspect.RepoTags) > 0 {
|
||||||
|
|
||||||
|
repotag := strings.Split(inspect.RepoTags[0], ":")
|
||||||
repository = repotag[0]
|
repository = repotag[0]
|
||||||
if len(repotag) > 1 {
|
if len(repotag) > 1 {
|
||||||
tag = repotag[1]
|
tag = repotag[1]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
summary[i] = compose.ImageSummary{
|
l.Lock()
|
||||||
ID: img.ID,
|
summary[img] = compose.ImageSummary{
|
||||||
ContainerName: getCanonicalContainerName(container),
|
ID: inspect.ID,
|
||||||
Repository: repository,
|
Repository: repository,
|
||||||
Tag: tag,
|
Tag: tag,
|
||||||
Size: img.Size,
|
Size: inspect.Size,
|
||||||
}
|
}
|
||||||
|
l.Unlock()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return summary, nil
|
return summary, eg.Wait()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user